Skip to content

Instantly share code, notes, and snippets.



Last active Mar 20, 2019
What would you like to do?
vimscript to highlight visual selection or string under cursor
" script modified from:
" ctermbg color code ref:
" Usage
" -----
" Add the following to your .vimrc:
" :map <silent> <C-a> :normal! gv"zy<CR>
" Use '\1' to '\9' for different color highlighting. '\0' clears all highlighting.
" In general, a visual selection followed by 1. Esc and 2. ^a will be highlighted.
" If there is no visual selection, the word under cursor will be highlighted.
function! HiInterestingWord(n) " {{{2
" Save our location.
normal! mz
" Yank the current word into the z register.
"normal! "zyiw
" Patched to highlight visual selection, if copied to register "z
:if @z == ""
normal! "zyiw
" Calculate an arbitrary match ID. Hopefully nothing else is using it.
let mid = 77750 + a:n
" Clear existing matches, but don't worry if they don't exist.
"silent! call matchdelete(mid)
call matchdelete(mid)
catch 'E803'
" Construct a literal pattern that has to match at boundaries.
"let pat = '\V\<' . escape(@z, '\') . '\>'
" Patched to match substrings
let pat = escape(@z, '\')
" Actually match the strings.
call matchadd("InterestingWord" . a:n, pat, 1, mid)
" Move back to our original location.
:let @z = ""
normal! `z
"clear all highlighting
function! ClearAllHi()
for i in range(1,9)
let mid = 77750 + i
silent! call matchdelete(mid)
nnoremap <silent> <leader>0 :call ClearAllHi()<cr>
nnoremap <silent> <leader>1 :call HiInterestingWord(1)<cr>
nnoremap <silent> <leader>2 :call HiInterestingWord(2)<cr>
nnoremap <silent> <leader>3 :call HiInterestingWord(3)<cr>
nnoremap <silent> <leader>4 :call HiInterestingWord(4)<cr>
nnoremap <silent> <leader>5 :call HiInterestingWord(5)<cr>
nnoremap <silent> <leader>6 :call HiInterestingWord(6)<cr>
nnoremap <silent> <leader>7 :call HiInterestingWord(7)<cr>
nnoremap <silent> <leader>8 :call HiInterestingWord(8)<cr>
nnoremap <silent> <leader>9 :call HiInterestingWord(9)<cr>
nnoremap <silent> <leader>a :call HiInterestingWord(10)<cr>
hi def InterestingWord1 guifg=#000000 ctermfg=16 guibg=#ffa724 ctermbg=214
hi def InterestingWord2 guifg=#000000 ctermfg=16 guibg=#aeee00 ctermbg=154
hi def InterestingWord3 guifg=#000000 ctermfg=16 guibg=#8cffba ctermbg=121
hi def InterestingWord4 guifg=#000000 ctermfg=16 guibg=#b88853 ctermbg=137
hi def InterestingWord5 guifg=#000000 ctermfg=16 guibg=#ff9eb8 ctermbg=211
hi def InterestingWord6 guifg=#000000 ctermfg=16 guibg=#ff2c4b ctermbg=249
hi def InterestingWord7 guifg=#000000 ctermfg=16 guibg=#ff5f00 ctermbg=202
hi def InterestingWord8 guifg=#000000 ctermfg=16 guibg=#d75fd7 ctermbg=170
hi def InterestingWord9 guifg=#000000 ctermfg=16 guibg=#87d7ff ctermbg=117
hi def InterestingWord10 guifg=#000000 ctermfg=16 guibg=#f2f2f2 ctermbg=130

This comment has been minimized.

Copy link
Owner Author

@jarun jarun commented Mar 31, 2018

I needed something that works like the notepad++ multi-highlight feature (that supports substring matches). This one can highlight the visually selected text and if there's no selection, the string under the cursor.

There is a scope for improvement. Can someone add a patch to search the current highlighted string under the cursor forward and backward with n and p?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.