How to run a vim plugin function from vimrc?

2,327 views
Skip to first unread message

wolfv

unread,
Oct 22, 2015, 2:21:33 AM10/22/15
to vim_use
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")

The first answer on http://vi.stackexchange.com/questions/2791/how-to-design-a-command-in-a-plugin-that-can-be-called-from-vimrc has a similar solution, but I couldn't get it to work.

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

The plugin's markHL.vim file is in my ~/.vim/plugin/ directory.

The ":function" command lists:

function HLMarks(group)

The plugin is described on http://www.vim.org/scripts/script.php?script_id=3394
and down loaded from http://www.vim.org/scripts/download_script.php?src_id=21611

Tony Mechelynck

unread,
Oct 22, 2015, 3:54:12 AM10/22/15
to vim...@googlegroups.com
If you want the function to run every time you open a file, you
shouldn't put a bare call statement to it in your vimrc (which would
try to call the function exactly once, at startup, and then no more).
What you need is an autocommand, which will run whenever a file is
open. You can set this up in your vimrc, for instance as follows:

autocmd BufReadPost * call HLMarks("Marks")

Since global plugins are sourced after the vimrc, but before opening
any buffers, this ought to take care that the function is defined
before it is called.


Best regards,
Tony.

Gary Johnson

unread,
Oct 22, 2015, 4:25:35 AM10/22/15
to vim_use
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

wolfv

unread,
Oct 22, 2015, 9:09:46 AM10/22/15
to vim_use

Thanks Tony and Gary. Adding this line in vimrc worked:
autocmd BufReadPost * call HLMarks("Marks")

Reply all
Reply to author
Forward
0 new messages