sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #498: Completation using the syntax file

 tip karma   Rating 54/17, Viewed by 3481 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   June 29, 2003 7:00      complexity:   intermediate
author:   Daniel Molina      as of Vim:   5.7

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 that

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).

To get more info:

:help autocmd
:help complete

 rate this tip  Life Changing Helpful Unfulfilling 

<< Replacing a string with default buffer contents | View character class >>

Additional Notes

[email protected], June 29, 2003 12:19

It is cool.

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

(most of time, I don't use syntax color)

Thanks
[email protected], June 30, 2003 4:58
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
[email protected], June 30, 2003 10:12

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")

(Anyone has a simpler solution? How to get rid of :syn on from .vimrc?)

Thanks
dwsharp at hotmail dot com, June 30, 2003 12:25
> 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')
[email protected], June 30, 2003 22:57

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

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


[email protected], July 1, 2003 0:43
> 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.
[email protected], July 1, 2003 8:56
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)
[email protected], July 1, 2003 12:10
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.
[email protected], July 4, 2003 8:14
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.

[With a little help from VIM guru Michael Geddes]
[email protected], July 4, 2003 8:59
Hi, I was the person who put the tip originally.
I want to thanks  to people who have made better the tip, I didn't know enough to do better, but I am learning :-).
[email protected], July 4, 2003 20:20

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?)

(Sorry for those who have space in $VIMRUNTIME.
I don't know it worked or not, because I tried to avoid such situation)




[email protected], July 5, 2003 1:53
Pro Noblemo Dude!

I have just noticed however that these syntax files don't have a lot of keywords in them.

Does anyone know where one can download 'pre-rolled' dictionary files for C++, Javascript etc?
pov at removethis at club dash internet dot fr, July 21, 2004 7:20
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
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
SourceForge.net Logo