在vim中自动完成搜索词

时间:2015-08-18 09:45:58

标签: vim autocomplete

如何在Vim中自动填写搜索词?

我有以下代码,我想搜索单词pgnExists_f

if(hj1939stack->pgnArray[index] == newPgn)
{
  /* newPgn already exists in the pgnArray */
  pgnExists_f = TRUE;
}

在搜索命令中,我在键入pgn后按下了TAB,希望该单词会自动完成到pgnExists_f。但是,跟随pgn的是^I

/pgn[TAB]导致了

/pgn^I

4 个答案:

答案 0 :(得分:4)

是的,有办法。

  • 这是常用的方法,将光标移到单词pgnExists_f上,然后按*#

  • 使用命令行窗口。在正常模式下按q/,然后i进入插入模式,输入pgn,然后ctrl-nctrl-p您将看到弹出窗口,您可以选择单词想要搜索。

答案 1 :(得分:1)

plugin SearchComplete可用,允许这样做。

答案 2 :(得分:0)

我认为这可以通过插入函数返回的cmap来实现:

function! SearchCompletions()
    return "SearchCompletion"
endfunction

cnoremap <Tab> <c-r>=SearchCompletions()<cr>

现在在命令行中按Tab将插入文本&#34; SearchCompletion&#34;。 现在的问题是编写一个SearchCompletions()函数,它可以提前搜索当前缓冲区中的匹配项。这不应该太难,但问题是它会有多慢。

另一个问题是,只要光标位于命令行上,映射就会起作用,而不仅仅是搜索提示。我不知道如何进一步限制它。

答案 3 :(得分:0)

看看这个插件:

Add completion in command line for '/', '?' and ':.../'

https://github.com/vim-scripts/sherlock.vim

By default, it use <C-Tab> for forward completion, and <C-S-Tab> for backward completion.

For example:
:%s/tes<C-Tab> list all word which begin with 'tes' after current cursor position;
:%s/tes<C-S-Tab> list all word which begin with 'tes' before current cursor position;
/tes<C-Tab> list all word which begin with 'tes' after current cursor position;
/tes<C-S-Tab> list all word which begin with 'tes' before current cursor position;
?tes<C-Tab> list all word which begin with 'tes' after current cursor position;
?tes<C-S-Tab> list all word which begin with 'tes' before current cursor position;

In ':' mode, completion is available only after a '/'.

When the right string is in command line, you can:
1) Validate it with <Enter>, or type <Esc> to go away from command line if you are in '?' or '/' mode;
2) Validate it with <Enter> or '/', or type <Esc> to go away from command line if you are in ':' mode.

You can override the default mapping with something like that in your .vimrc:
cnoremap <Whatever you want here> <C-\>esherlock#completeBackward()<CR>
cnoremap <Whatever you want here> <C-\>esherlock#completeForward()<CR>