Re: How does this VimScript work? Why does it throw an error?
- On 5/30/08, 703designs <thomasmallen@...> wrote:
>The error is thrown because the function is defined with "function"
> Posted again; the first one appears to have been deleted immediately;
> weird bug.
> This is from a Yahoo! Vim for PHP presentation. They present this
> 48 " Insert <Tab> or complete identifier
> 49 " if the cursor is after a keyword character
> 50 function MyTabOrComplete()
> 51 let col = col('.')-1
> 52 if !col || getline('.')[col-1] !~ '\k'
> 53 return "\<tab>"
> 54 else
> 55 return "\<C-N>"
> 56 endif
> 57 endfunction
> inoremap <Tab> <C-R>=MyTabOrComplete()<CR>
> Which throws the following error when I source .vimrc, although it
> does work fine:
> Error detected while processing /Users/myusername/.vimrc:
> line 57:
> E122: Function MyTabOrComplete already exists, add ! to replace it
> I've included the line #s for reference. Can someone explain how this
> script works? I'd actually like to have it tab-complete filenames,
> Ctrl-X Ctrl-F, instead of keywords. And why am I getting that error?
instead of "function!" which means that it will throw an error if a
function of that name is already defined. Using "function!" will force
it to define the function even if it is already defined. Since you
placed this in your vimrc, the function will be defined when you start
Vim, and if you source the vimrc again, it tries to define it a second
time. See :help :function
It works as follows:
1. check to see if the letter before the cursor is a keyword character
(that's all the col(), getline(), and \k stuff)
2. If it is not a keyword character, just return the TAB key so that
it acts as normal (inoremap uses the unmapped functionality of the
3. If it is a keyword character, return the C-N (control+N) key, which
brings up the completion menu.
So, to make it filename completion instead, replace the C-N with C-X
Note that a better mapping would probably make use of the <expr> map
capability rather than <C-R>=. See :help :map-<expr>
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php