Vim Tips Wiki

Completion using a syntax file

Revision as of 10:53, September 23, 2009 by JohnBot (Talk | contribs)

1,619pages on
this wiki

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 498 Printable Monobook Previous Next

created 2003 · complexity intermediate · author Daniel Molina · version 6.0

To use the Vim completion you can use a dictionary.

:set complete=k

As a dictionary you can use a syntax file (which are in the /syntax/ directory), so you can complete the reserved words.

Inserting in your vimrc a line like this:

autocmd Syntax * exec('set dict=/usr/share/vim/syntax/' .expand('<amatch>') .'.vim')

you can avoid to select the dictionary for each filetype.

Don't forget to use your correct syntax directory for this.



This is not working. I also tried to use my old dictionary file in the 'set dict=' statement for testing, no success.

A 'set cpt=k/path/to/your/dictionary/file' is working

It is working. The condition is that you have to do :syn on first on your vimrc.

The map I hacked is

syn on
au Syntax * exe("set dict+=".$VIMRUNTIME."/syntax/".expand('<amatch>').".vim")

> How to make it work without "syn on" first?

If you are using filetype detection (i.e., you have 'filetype on' or 'filetype plugin on' in your .vimrc) you can use 'au FileType' in place of 'au Syntax'. Also, I would suggest using 'setlocal' in place of 'set' and using += in place of just =, so that the change is local to the current buffer and file is added to the existing 'dict' list instead of replacing it.

autocmd FileType * exec('setlocal dict+=/usr/share/vim/syntax/' .expand('<amatch>') .'.vim')

My final version in my .vimrc, for both PC and unix:

autocmd FileType * exec('setlocal dict+='.$VIMRUNTIME.'/syntax/'.expand('<amatch>').'.vim')

> autocmd FileType * exec('setlocal dict+='.$VIMRUNTIME.'/syntax/'.expand('<amatch>').'.vim')

This works. I replaced $VIMRUNTIME with its value for testing and it stops working, so this was the problem.

I've just started a general keyword completion file (not filetype dependent)

set cpt=kc:/cygwin/home/davidr/vimfiles/dictionary.txt
set cpt+=kc:/cygwin/home/davidr/vimfiles/syntax/cf.vim (couldn't get the auto stuff to work)

On the PC I had to change the reference to $VIMRUNTIME to escape('$VIMRUNTIME',' ') to get this to work. My vim path had spaces in, so I needed to escape both space and backslash characters.

If the above example doesn't work for you (as it didn't for me) try this

autocmd FileType * exe "setlocal dict+=".escape($VIMRUNTIME.'\syntax' .&filetype.'.vim',' \$,')

Requires that you are using filetypes.

Thanks for inspiration of &filetype. Now, I have one-line version (72 chars) in my vimrc:

au FileType * exe('setl dict+='.$VIMRUNTIME.'/syntax/'.&filetype.'.vim')

I tested it and it worked for me on W2K, cygwin console, and Unix. (I tested by checking :set dict?)

The problem is not files are not exhaustive, it's just their lines are too long to be parsed by the dictionary parser which is limited to 511 char per line. I had this problem with the php language file and had to split the lines. To do it, I used this replacement patern you'll have to adapt for other language, beware that ^M are obtained by typing <C-V> then return and ^I by typing the tab key.

:s/^\(syn keyword\tphpFunctions\t.\{460,490\}\) \(.*\)$/\1contained^Msyn keywords^IphpFunctions^I\2/g

Around Wikia's network

Random Wiki