Autocomplete (Suggestions) feature in Vim

While writing programs, you will be using the function names, variable names and keywords (language specific like in C/C++ , int, void, float  are commonly used keywords) again and again. The best feature for any editor is to have an auto-complete option. If you wonder what is an auto-complete option, it is the option to get suggestions while typing a word. Suppose sea, search, seal are some of the words in a dictionary, the moment you type sea, you get suggestions like search, seal so that you need not type it fully. Just make the choice and the word will be selected (typed completely)

Vim also has the feature of auto-completion. There are two ways by which you can make use of the auto-completion feature.

  1. ctrl+p : Search suggestions based on Previously typed words
  2. ctrl+n: Search suggestions based on Upcoming (already) typed words. This occurs when you type words in the middle of the file

How to remember the shortcuts?

  1. ctrl+p: previous – suggest based on previous typed words
  2. ctrl+n: next – suggest based on the next coming  words

Where does this feature work?

This works in the insert mode of Vim i.e. the mode where we do the regular typing ( After pressing i in ESC mode). Type few letters of a word and press CTRL+p. If there are previously typed words starting with those letters. You can see some suggestions. If there is only one suggestion, the word will be auto complete and you can continue editing.

How to make a selection when multiple suggestions are shown?

When pressing you are ctrl+p (ctrl+n), you are shown multiple suggestions and you want to make use of nth  suggestion, press ctrl+p (ctrl+n) n-1 times and continue editing. That is if you are shown five suggestions, and you want to use third suggestion, press ctrl+p two times and you can continue editing.

How not to make use of given suggestions?

When pressing you are ctrl+p (ctrl+n), you are shown n suggestions and you couldn’t find what you are looking for. Press ctrl+p (ctrl+n) n times and you can continue editing. Press ctrl+p (ctrl+n) will undo showing any specified suggestion.

Check more Vim tips.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s