Skip to content

Instantly share code, notes, and snippets.

@mannih
Last active August 29, 2015 14:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mannih/be96d66d829c624e2256 to your computer and use it in GitHub Desktop.
Save mannih/be96d66d829c624e2256 to your computer and use it in GitHub Desktop.
Automatic variable highlighting in vim the easy way
" Vim plugin to highlight variables in Perl.
" I now created a repository from this: https://github.com/mannih/vim-perl-variable-highlighter
function! s:hlvar()
if ( exists( "w:current_match" ) )
call matchdelete( w:current_match )
unlet w:current_match
endif
let l:old_iskeyword = &iskeyword
set iskeyword=@,$,%,_,48-57,192-255,@-@
let l:word = expand( '<cword>' )
let &iskeyword = l:old_iskeyword
" we only care about words starting with a sigil
if ( -1 == match( l:word, '^[%$@]' ) )
return
endif
" build up the match by replacing the sigil with the character class [$%@]
" and appending a word-boundary:
let l:match = substitute( l:word, '^[$@%]', '[$@%]', '' ) . '\>'
" do the highlighting
let w:current_match = matchadd( 'PerlVarHiLight', l:match )
endfunction
if ( ! exists( "g:hlvarnoauto" ) || g:hlvarnoauto == 1 )
augroup HighlightVar
autocmd!
au FileType perl :au CursorMoved * call <SID>hlvar()
au FileType perl :au CursorMovedI * call <SID>hlvar()
augroup END
" only add the highlight group if it doesn't already exist.
" this way, users can define their own highlighting with their
" favorite colors by having a "highlight PerlVarHiLight ..." statement
" in their .vimrc
if ( ! hlexists( 'PerlVarHiLight' ) )
highlight PerlVarHiLight ctermbg=black guifg=#ff0000 guibg=#000000 ctermfg=LightRed gui=bold
endif
command! HlVar :call <SID>hlvar()
endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment