AnalogClock






Tuesday, March 24, 2009

Vim auto completion

I responded to a comment on the Daily Vim about auto completion. I decided to post it here as well as the comment ended up being kind of long. The comment by Casey asks if there is a way to turn off omni auto completion for perl as it goes through the entire POD collection evey time. I provide several solutions. Read the original article for a quicj intro to auto completion in insert mode in Vim.

The original article points out that there is built in auto completion in Vim. All you have to do is use it is type Ctrl-x Ctrl-o in insert mode and vim will try to complete what you have started typing or pop up a list of all completions if the cursor is not immediately preceded by anything.

Original comment follows:

Try this command to change the auto completion to the simpler syntax highlighting hints as the auto completer.

:setlocal omnifunc=syntaxcomplete#Complete

If that is what you always want for Perl add this to your .vimrc after all your other auto commands.

autocmd Filetype perl setlocal omnifunc=syntaxcomplete#Complete

If you want syntax completion rather than omni completion for all file types use * for the file type. If you want to fail over to syntax completion for file types that don't have omni completion put this in your .vimrc after your other auto commands.

if has("autocmd") && exists("+omnifunc")
autocmd Filetype *
\ if &omnifunc == "" |
\ setlocal omnifunc=syntaxcomplete#Complete |
\ endif
endif

You can also do something a little more complex which just disable the perlPOD part of the perl filetype omni complete.

Try this command:

let g:omni_syntax_group_exclude_perl = 'perlPOD'

generically it's:

let g:omni_syntax_group_exclude_{filetype} = 'comma,separated,list'

And you can get the complete list of syntax groups while in a file with the filetype in question with this command:

:syntax list

Then just add that command to your .vimrc to make it permanent.

Check out this help topic for more info:

:h ft-syntax-omni

*Updated

And for the complete low down on the idiosyncrasies of each filetype check out:

:h compl-omni-filetypes

And here's another tip I picked up Ctrl-o : in insert mode will allow you to run a command and then return to insert mode. Neatly avoiding the Esc : ... i or a syndrome.

*Updated again

FYI Pressing Ctrl-o in insert mode puts you in normal mode for one command. You are not limited to using : to get to the ex command line. You can use p to put or y to yank etc.

*Further updates

For those times when you just want to save keystrokes or avoid typos:

Ctrl-P completes from previous words in document
Ctrl-N completes from following words in document
Ctrl-X Ctrl-F completes file names
0 Ctrl-D removes all indent for the current line

No comments: