Kurt Keller's Blog

tech notes and more

autocomplete

How often have you written a script and then, during debugging, you found typos in variable names or function names? Yes, I know there are complete, huge IDE’s with syntax highlighting, autocomplete suggestion, integrated help and more bells and whistles than you’d ever care for. But I love my vim. Basta! And my vim does have syntax highlighting and autocomplete, too.

It’s very simple. While in insert mode, type the start of a variable name you’ve already used somewhere and then type CTRL-p. Vim will show you a popup with words previously used which start with the same characters you’ve already typed. If you want to search from words after the current position, the command is CTRL-n.

With the popup list open use CTRL-p and CTRL-n (or UP/DOWN) to move up or down the list. You can also type BS (backspace) as often as needed and then more characters to narrow down the possible matches. Type any character which does no longer match (eg. space) to finish the selection.

The matches, by the way, can possibly come from all the files you’ve currently open in the editor.

CTRL-p (in insert mode) autocomplete from previous matches
CTRL-n (in insert mode) autocomplete from following (next) matches

The following probably sounds rather confusing. Memorizing CTRL-p and CTRL-n only is fine. If you want to go a step further, I recommend you first get used to CTRL-p and CTRL-n and once you got the hang of it, try the following with some sample file.

You can also easily copy the words which come after the source of your matching expansion by following your selection immediately with CTRL-x CTRL-n or CTRL-x CTRL-p.

Or if your match is from the first word on a line, you can insert a copy of the whole line with CTRL-x CTRL-l.

CTRL-x CTRL-p (in insert mode right after autocomplete) copy the word following the previous autocomplete source
CTRL-x CTRL-n (in insert mode right after autocomplete) copy the word following the next autocomplete source
CTRL-x CTRL-l (in insert mode right after autocomplete) copy the whole line from the matched autocomplete source, but only if the autocomplete source starts a line
Share

Leave a Reply