Highlight if-else-endif structures in Vim

102 views
Skip to first unread message

Fabian Nick

unread,
Aug 26, 2015, 3:16:53 PM8/26/15
to vim_use
Hi all,

ss there a way to highlight if-else-endif structures in Vim on demand based on their 'level' in the overall program hierarchy? I.e. I want something like

if <<< highlight in red
foo
else <<<< red
if <<< blue
baz
bar
endif <<< blue
endif <<< red

I'm mostly editing FORTRAN code, but I guess this feature would be pretty generic, as long as the syntax highlighting works for the given language...

Cheers,
Fabian

Eric Christopherson

unread,
Aug 26, 2015, 10:16:23 PM8/26/15
to vim...@googlegroups.com
That sounds like the various "rainbow parentheses" plugins, except using
if/else/endif instead of parentheses. I wonder if you could adapt one of
those for your use.

--
Eric Christopherson

Fabian Nick

unread,
Sep 30, 2015, 6:48:00 PM9/30/15
to vim_use
I wrote my own (first) Vim plugin that does this (at least for FORTRAN, which is my use-case): https://github.com/fpnick/flowhighlight
Feel free to give it a try and/or extend it to your needs.

Charles E Campbell

unread,
Dec 3, 2015, 5:20:01 PM12/3/15
to vim...@googlegroups.com
Try Rainbow.vim from my website:
http://www.drchip.org/astronaut/vim/index.html#RAINBOW . It currently
handles c, c++, matlab, Latex, javascript, and vim.

Regards,
Chip Campbell

Gary Johnson

unread,
Dec 3, 2015, 6:48:24 PM12/3/15
to vim...@googlegroups.com
On 2015-12-03, Charles E Campbell wrote:

> Try Rainbow.vim from my website:
> http://www.drchip.org/astronaut/vim/index.html#RAINBOW . It currently
> handles c, c++, matlab, Latex, javascript, and vim.

I just did that, following the link above, but ~/.vim/Rainbow.vba.gz
does not contain a rainbow.vvim file for vim. It's version 2s. The
various files have different dates, but the most recent is Dec 03,
2015, for autoload/Rainbow.vim.

Another issue is that I just executed ":Rainbow" in a C file and got
the following messages:

DechoTab(mode=1) a:0=1
"Decho Tab" [Not edited] --No lines in buffer--
Error detected while processing function Rainbow#Rainbow:
line 34:
E484: Can't open file /home/gary/.vim/before/after/syntax/c/rainbow.vvim

It looks like line 34 is trying to source that rainbow.vvim assuming
that the after directory is below the first directory in
'runtimepath'. In my case, there are several directories ahead of
~/.vim in my 'runtimepath' (newlines and spaces added for
readability):

runtimepath=~/.vim/before,
~/.vim/netrw,
~/.vim/splice,
~/.vim/vim-latex,
~/.vim/vim-bookmarks,
~/.vim/linediff,
~/.vim,
/usr/local/share/vim/vimfiles,
/usr/local/share/vim/vim74,
/usr/local/share/vim/vimfiles/after,
~/.vim/after

Couldn't you use ":runtime syntax/c/rainbow.vvim" instead of :source
there?

Here is where the Rainbow files are installed:

$ find ~/.vim -follow -iname \*rainbow\*
/home/gary/.vim/Rainbow.vba
/home/gary/.vim/after/syntax/matlab/rainbow.vvim
/home/gary/.vim/after/syntax/cpp/rainbow.vvim
/home/gary/.vim/after/syntax/tex/rainbow.vvim
/home/gary/.vim/after/syntax/lisp/rainbow.vvim
/home/gary/.vim/after/syntax/c/rainbow.vvim
/home/gary/.vim/autoload/Rainbow.vim
/home/gary/.vim/doc/Rainbow.txt
/home/gary/.vim/plugin/RainbowPlugin.vim

Regards,
Gary

Charles E Campbell

unread,
Dec 4, 2015, 10:15:26 AM12/4/15
to vim...@googlegroups.com
Gary Johnson wrote:
> On 2015-12-03, Charles E Campbell wrote:
>
>> Try Rainbow.vim from my website:
>> http://www.drchip.org/astronaut/vim/index.html#RAINBOW . It currently
>> handles c, c++, matlab, Latex, javascript, and vim.
> I just did that, following the link above, but ~/.vim/Rainbow.vba.gz
> does not contain a rainbow.vvim file for vim. It's version 2s. The
> various files have different dates, but the most recent is Dec 03,
> 2015, for autoload/Rainbow.vim.
Sorry -- I forgot to update my uploading script -- so
after/syntax/vim/Rainbow.vvim is now included.
OK, I've also changed Rainbow to use runtime instead of source and
commented out the DechoTabOn.

Please try v2t (http://www.drchip.org/astronaut/vim/index.html#RAINBOW).

Thank you for the feedback,
Chip Campbell

Gary Johnson

unread,
Dec 4, 2015, 12:14:53 PM12/4/15
to vim...@googlegroups.com
Nice. Thank you. It works well for C, especially after executing
":syn sync fromstart". For Vim, it works well unless there are
keywords within comments--those throw it off.

Regards,
Gary

Fabian Nick

unread,
Dec 7, 2015, 3:33:35 AM12/7/15
to vim_use

Thanks,
I'll definitely try this for my LaTeX work!

Charles E Campbell

unread,
Dec 7, 2015, 12:37:51 PM12/7/15
to vim...@googlegroups.com
You might like the :SplitRainbow command, too -- it will show the
regular highlighted version of your file in a lefthand window and the
rainbow'd version in a righthand window.

Regards,
Chip Campbell

Reply all
Reply to author
Forward
0 new messages