1

I have used this script to be able to highlight results from test reports: https://vim.fandom.com/wiki/Highlight_multiple_words

When using it in Vim, it works fine when I type :Highlight 1 FAILED (For some reason the numpad-keys don't work).

Unfortunately, this is not session persistent, so I tried to add it to my .vimrc:

" auto highlight PASSED, FAILED, ERROR, SKIPPED
:Highlight 1 FAILED
:Highlight 4 ERROR
:Highlight 2 SKIPPED
:Highlight 3 PASSED

I would have expected this to work, but it doesn't. I get these errors when starting Vim:

Error detected while processing /home/wouter/.vimrc:
line  174:
E492: Not an editor command: :Highlight 1 FAILED
line  175:
E492: Not an editor command: :Highlight 4 ERROR
line  176:
E492: Not an editor command: :Highlight 2 SKIPPED
line  177:
E492: Not an editor command: :Highlight 3 PASSED
Press ENTER or type command to continue

What am I doing wrong here?

3
  • 1
    The script that defines the :Highlight command is not yet sourced by the time the vimrc file is loaded. Simple way to fix it: put those :Highlight commands into a function and let that be run on an VimEnter autocommand. Commented Apr 4, 2022 at 11:45
  • @ChristianBrabandt I think I get what you're saying in an abstract way (I guess it's similar to how you would define function prototypes in C, to define them at the bottom of the file?), but I'm not sure how to do it in practice. I'm very new to writing my own vimrc code. Could you show a basic example in an answer? It would probably be a good candidate for an accepted answer.
    – Opifex
    Commented Apr 4, 2022 at 12:25
  • You might want to read the :help startup page to get an idea of when code is executed. Then take a look at :help function and :help autocmd for those.
    – D. Ben Knoble
    Commented Apr 4, 2022 at 14:47

1 Answer 1

2

This happens, because by the time your .vimrc file is being loaded other startup plugin files have not been loaded yet (see :h startup), so Vim does not really know the `:Highlight' command yet.

There are two ways around it:

  1. Defer definition of the custom highlighting until the VimEnter autocommand has been triggered. By that time, your custom plugins are already sourced. For that case you would create a customer function like this:
    function MyHighlighting()
      "auto highlight PASSED, FAILED, ERROR, SKIPPED
      :Highlight 1 FAILED
      :Highlight 4 ERROR
      :Highlight 2 SKIPPED
      :Highlight 3 PASSED
    endfu
    
    augroup MyStartup
      au!
      au VimEnter * ++once :call MyHighlighting()
    augroup END

A slightly similar approach is to make use of timer instead of the VimEnter autocommand. But I prefer the simplicity of the VimEnter autocommand.

  1. Make use of the :runtime or :source commands to force loading the plugin before:
" Force loading the runtime script:
" Either use :source /absolute/path/to/your/script.vim
" Or use :runtime plugin/script.vim
:ru plugin/highlights.vim
:Highlight 1 FAILED
[...]

This can get a bit more complex, if you want to check that the script plugin/highlights.vim really exists and only if the script exists want to execute those :Highlight commands (e.g. if you use vim on different systems where you not have the script installed everywhere).

You could use an :if glob() (See :h glob()) statement around this to only execute those commands conditionally, when the script actually exists.

Note: you can use the :scriptnames command to check if a script is being sourced.

1
  • This is a great answer that also taught me a lot more about Vim, regardless of the highlight context! Thanks!
    – Opifex
    Commented Apr 5, 2022 at 8:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.