Thursday, October 22, 2015

Re: How to run a vim plugin function from vimrc?

On 2015-10-21, wolfv wrote:
> A plugin defines a function named HLMarks():
>
> hi Marks term=reverse ctermfg=0 ctermbg=40 guibg=Grey40
>
> function! HLMarks(group)
> call clearmatches()
> let index = char2nr('a')
> while index < char2nr('z')
> call matchadd( a:group, '\%'.line( "'".nr2char(index)).'l')
> let index = index + 1
> endwhile
> endfunction
>
> I want the HLMarks() function to run automatically every time vim
> opens a file.
> The function works when I call it manually:
>
> :call HLMarks("Marks")
>
> I added these two lines to the end of my vimrc:
>
> runtime plugin/markHL.vim
> HLMarks("Marks")
>
> which got this error:
>
> E492: Not an editor command: HLMarks("Marks")

You got that error because you tried to execute the function as a
command instead of as a function. A command can be executed by
putting its name and any required arguments on a line. A function
must be called. Just as you used the call command to call the
function "manually", you must use the call command to call the
function in a vimrc file, e.g.,

call HLMarks("Marks")

> How to automatically call the HLMarks("Marks") function when a
> file is opened?

To call a function every time a file is opened, you must use an
autocommand. Which event you use to trigger the autocommand depends
on when in the process of opening a file the function should be
called. The earliest event would be BufReadPre, but that is often
too early. The next earliest events would be BufRead for existing
files and BufNewFile for new files. The latest event would be
BufWinEnter. Assuming you want to wait until after all other
processing of the buffer has taken place before calling your
function, the autocommand would be:

au BufWinEnter * call HLMarks("Marks")

See:

:help autocommand

Regards,
Gary

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: