Last active
August 29, 2015 14:01
-
-
Save mannih/be96d66d829c624e2256 to your computer and use it in GitHub Desktop.
Automatic variable highlighting in vim the easy way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" 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