Automatic variable highlighting in vim

After viewing an interesting post in Reddit about programming in color (syntax highlighting that focuses on data flow instead of syntax), I saw that one user had developed a very similar feature for PHP. The idea is to highlight the variable that your cursor is currently on. I liked what he wrote, so I modified it for Perl.

Neat, huh? I'm going to experiment with this.

If you like it, save the following as .vim/plugin/vawa.vim:

19 Comments

Komodo IDE from ActiveState does this too. I consider it extremely useful. I guess Komodo Edit (the free variant of Komodo IDE) can do this too.

Nice job. I have considered writing something similar for emacs, although I haven't amassed enough round tuits yet.

I might suggest adding a "hold" hotkey, to keep the current symbol highlighted until you turn it off, in case you want to navigate somewhere else or type something else while still having the marked symbol active.

Thanks, that's pretty neat.

I had to make a fix for matching on just the sigil char alone. It was matching which is weird for Perl, but also blowing up on '*' in POD.

Changed if statement "(s:varend == 0)" to simply return.

which (indeed) is available in Emacs for some time now ;)

install package 'highlight'

add this to your .emacs file

(require 'highlight)
(setq hlt-auto-faces-flag 1)
(setq highlight-symbol-idle-delay 1)
(add-hook 'cperl-mode-hook 'highlight-symbol-mode)

This would be a cool optional feature in the Perl support for vim. https://github.com/vim-perl/vim-perl

It also nukes any match commands as a result of calling clearmatches() at the top.

Workaround could be to call a function where the user could define their matches, which would be reapplied after clearmatches().

You may have perl scripts that do not have an extension but still are picked up as &ft==perl.

If so, you can use this diff:


$ diff -i .vim/plugin/vawa.vim-orig .vim/plugin/vawa.vim
60,65c60,61
vawa()
vawa()
vawa()
vawa()
vawa()
vawa()
---
>             au FileType perl :au CursorMoved  * call vawa()
>             au FileType perl :au CursorMovedi * call vawa()

looks like the diff above was mangled by some XSS filtering for the input form. That's a good thing!

Basically, you can replace the CursorMoved lines that match common file extensions for perl with some calls that use the FileType matching you probably are using already:

   au FileType perl :au CursorMoved  * call vawa()
   au FileType perl :au CursorMovedi * call vawa()

Awesome!!! Anyway to change the cursor color? It highlights the variable in a dark black and then when I cursor over it it's a dark grey, so it's hard to see where I am on the variable.

I have spoken to Sandeep, the author of the plugin for PHP, and to Ovid, and I have put together a repository for this. You can find it at https://github.com/pjcj/vim-hl-var

The main change I have made is to stop the checks happening on every cursor move, which is really rather slow. I also fixed the clearmatches bug, which is what first annoyed me and caused me to spend far too much time on this today ;-)

Feel free to try it out and send pull requests for anything that needs to be done.

I'd be pleased if someone wanted to write some help text ...

Tried highlight.el but it didn't work for me (I'm on Debian Stable 7.5, Emacs 23.4.1).

What works is:

(load "~/.emacs.d/auto-highlight-symbol")
(add-hook 'cperl-mode-hook 'auto-highlight-symbol-mode)
(setq ahs-idle-interval 0.35)

The .el files are at https://github.com/emacsmirror/auto-highlight-symbol

weird that it didn't work for you... I'm on GNU Emacs 24.3.1 (x86_64-apple-darwin13.1.0, NS apple-appkit-1265.19) of 2014-03-18

Also, highlight has a lot of more stuff than auto-highlight-symbol. So you might want to debug that, or at least file a bug report ?

I notice that if I put the cursor on %something, it won't highlight an instance of $something{foo}.

nice

Only big thing missing is sigil negotiation.

So @list and $list[0] = 5; both @list and $list[ are highlighted when over either one. Same for hashes.

Otherwise awesome extra bit of Perl sugar in vim thanks!

I'm still using auto-highlight-symbol-mode (instead of highlight). And am wondering if I can get Emacs to switch between highlighting %hash and all $hash{foo}, or just highlighting a specific $hash{foo}.

Robin, Ultimatto, confuseAcat fixes the sigil negotiation and does it with much less and simpler code: https://blogs.perl.org/users/confuseacat/2014/05/automatic-variable-highlighting-in-vim-the-easy-way.html

Damian Conway has implemented a typically awesome plugin for automatic variable highlighting (and more):

https://github.com/thoughtstream/Damian-Conway-s-Vim-Setup/blob/master/plugin/trackperlvars.vim

About Ovid

user-pic Freelance Perl/Testing/Agile consultant and trainer. See http://www.allaroundtheworld.fr/ for our services. If you have a problem with Perl, we will solve it for you. And don't forget to buy my book! http://www.amazon.com/Beginning-Perl-Curtis-Poe/dp/1118013840/