VIM highlight matching begin/end

user3719021 picture user3719021 · Dec 16, 2014 · Viewed 8.6k times · Source

I'm trying to find a plugin that will highlight the matching begin/end statements with Verilog. VIM has it working with curly braces /brackets but it does not work with its begin/end. I want VIM to highlight the correct begin to the correct end.

Answer

Vitor picture Vitor · Dec 18, 2014

In my opinion, your best bet is using matchit. This script is part of vim runtime and can easily be loaded by adding the following line to your .vimrc:

runtime macros/matchit.vim

The standard Verilog filetype plugin already includes the matchit configuration you require:

" Let the matchit plugin know what items can be matched.
if exists("loaded_matchit")
  let b:match_ignorecase=0
  let b:match_words=
    \ '\<begin\>:\<end\>,' .
    \ '\<case\>\|\<casex\>\|\<casez\>:\<endcase\>,' .
    \ '\<module\>:\<endmodule\>,' .
    \ '\<if\>:\<else\>,' .
    \ '\<function\>:\<endfunction\>,' .
    \ '`ifdef\>:`else\>:`endif\>,' .
    \ '\<task\>:\<endtask\>,' .
    \ '\<specify\>:\<endspecify\>'
endif

This way you can match the begin/end using % key, as you probably already do for parentheses and such.

This is not exactly what you were looking for, in the sense that although it allows you to find the matching end of a begin it does not highlight it for you. I did some research and apparently there's a code snippet around for that; and there's someone who already transformed that code into a plugin, which is named hl_matchit. Don't forget to check this plugin's help page:

:help hl_matchit.txt

Please note that the Verilog filetype plugin included in vim installation does not support the ifndef and elsif clauses introduced in Verilog 2001. If you require this then I suggest that you also install the verilog_systemverilog.vim plugin already mentioned before, but use the fork I am improving which includes the afore mentioned updates, as well as other fixes/improvements.