Tab completion in Vim while in insert mode

I like tab completion in Linux shell, and I have occasionally used Ctrl-n while in insert mode in Vim. However, Ctrl-n requires 2 fingers, which makes it clumsy.

Today I stumbled a script, inside Vim’s help file, that enables Tab completion while in insert mode. It is pretty smart in that it will insert a Tab if you are at a whitespace while pressing the key. Otherwise it will present you with words already exist inside your document. Keep pressing Tab to toggle through them if there are more than one candidates.

Just put the script below in your _vimrc file and enjoy. You can do :helpgrep CleverTab to find it in your help file. Not sure if it is present in versions prior to 7.1:

function! CleverTab()
if strpart( getline(‘.’), 0, col(‘.’)-1 ) =~ ‘^\s*$’
return “\
else
return “\
endfunction
inoremap =CleverTab()

Enjoy! Note: Get it from the help file. The brackets and back slashes are not displayed properly here.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.