3

How to autocomplete a search word in Vim?

I've the following piece of code and I want to search for the word pgnExists_f.

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

In the search command I pressed TAB after typing pgn hoping that the word would autocomplete to pgnExists_f. But, what followed pgn was ^I.

/pgn[TAB] resulted in

/pgn^I
Sagar Jain
  • 7,475
  • 12
  • 47
  • 83

4 Answers4

7

Yes, there are ways.

  • this would be the common way, move your cursor on the word pgnExists_f, then press * or #

  • using command-line window. In normal mode press q/ then i enter insert mode, type pgn, then ctrl-n or ctrl-p you will see the popup, you can select the word you want to search.

Kent
  • 189,393
  • 32
  • 233
  • 301
1

There is a plugin SearchComplete available, that allows this.

Christian Brabandt
  • 8,038
  • 1
  • 28
  • 32
0

I think this could be achieved using a cmap which inserts the return of a function:

function! SearchCompletions()
    return "SearchCompletion"
endfunction

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

Now pressing Tab in the command line will insert the text "SearchCompletion". The problem now is to write a SearchCompletions() function which can search ahead for matches in the current buffer. This shouldn't be too difficult, but the question is how slow it will be.

The other problem is that the mapping works whenever the cursor is on the command line, not just the search prompt. I don't know how to restrict it further.

Prince Goulash
  • 15,295
  • 2
  • 46
  • 47
0

Look at this plugin:

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>
Naga Kiran
  • 8,585
  • 5
  • 43
  • 53