Vim Tips Wiki
(Remove html character entities)
(restore tip template)
 
(9 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{duplicate|765}}
 
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=498
 
|id=498
|previous=497
+
|previous=495
 
|next=502
 
|next=502
|created=June 29, 2003
+
|created=2003
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Daniel Molina
 
|author=Daniel Molina
|version=5.7
+
|version=6.0
 
|rating=54/17
 
|rating=54/17
 
|category1=Completion
 
|category1=Completion
 
|category2=Syntax
 
|category2=Syntax
 
}}
 
}}
To use the Vim completion you can use a dictionary.
+
To complete syntax words, you can use omnicompletion:
  +
setlocal omnifunc=syntaxcomplete#Complete
<pre>
 
  +
And then CTRL-X CTRL-O to activate completion.
:set complete=k
 
</pre>
 
   
  +
A more robust solution from {{help|ft-syntax-omni}}
As a dictionary you can use a syntax file (which are in the /syntax/ directory), so you can complete the reserved words.
 
   
  +
if has("autocmd") && exists("+omnifunc")
Inserting in your [[vimrc]] a line like this:
 
  +
autocmd Filetype *
<pre>
 
  +
\ if &omnifunc == "" |
autocmd Syntax * exec('set dict=/usr/share/vim/syntax/' .expand('<amatch>') .'.vim')
 
  +
\ setlocal omnifunc=syntaxcomplete#Complete |
</pre>
 
  +
\ endif
  +
endif
   
  +
This version will avoid changing omnifunc if it's already set (presumably to something more comprehensive).
you can avoid to select the dictionary for each filetype.
 
   
  +
==See Also==
Don't forget to use your correct syntax directory for this.
 
 
*{{help|ft-syntax-omni}}
 
==References==
 
*{{help|:autocmd}}
 
 
*{{help|complete}}
 
*{{help|complete}}
   
 
==Comments==
 
==Comments==
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 <tt>:syn on</tt> first on your vimrc.
 
 
The map I hacked is
 
 
<pre>
 
syn on
 
au Syntax * exe("set dict+=".$VIMRUNTIME."/syntax/".expand('<amatch>').".vim")
 
</pre>
 
 
----
 
> 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
 
 
----
 

Latest revision as of 21:53, 4 December 2014

Tip 498 Printable Monobook Previous Next

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


To complete syntax words, you can use omnicompletion:

setlocal omnifunc=syntaxcomplete#Complete

And then CTRL-X CTRL-O to activate completion.

A more robust solution from :help ft-syntax-omni

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

This version will avoid changing omnifunc if it's already set (presumably to something more comprehensive).

See Also[]

Comments[]