Skip to content

Instantly share code, notes, and snippets.

@cohalz
Last active August 29, 2015 14:04
Show Gist options
  • Save cohalz/07b6d6c618f9a54c4f68 to your computer and use it in GitHub Desktop.
Save cohalz/07b6d6c618f9a54c4f68 to your computer and use it in GitHub Desktop.
" カーソル下の単語をハイライトする
" http://d.hatena.ne.jp/osyo-manga/20140121/1390309901
" 元との違いはカーソル下が記号の場合にうまく行ってなかったものを、空白区切りの単位でハイライトするようにしたこと
" 例えば >>= という記号列なども問題なくハイライトできるように
" 1 が設定されていれば有効になる
let g:enable_highlight_cursor_word = 1
augroup highlight-cursor-word
autocmd!
autocmd CursorMoved * call s:hl_cword()
autocmd ColorScheme * highlight CursorWord gui=underline guifg=orange
autocmd BufLeave * call s:hl_clear()
autocmd WinLeave * call s:hl_clear()
autocmd InsertEnter * call s:hl_clear()
augroup END
function! s:hl_clear()
if exists("b:highlight_cursor_word_id") && exists("b:highlight_cursor_word")
silent! call matchdelete(b:highlight_cursor_word_id)
unlet b:highlight_cursor_word_id
unlet b:highlight_cursor_word
endif
endfunction
function! s:hl_cword()
let cursorChar = matchstr(getline('.'), '.', col('.')-1)
if matchstr(cursorChar,"[a-zA-Z0-9_'.]") != ""
let word = expand('<cword>')
else
let word = expand('<cWORD>')
endif
if word == ""
return
endif
" 記号列のせいで全文字がハイライトされないようにする
if matchstr(word,"[*]") != ""
return
endif
if get(b:, "highlight_cursor_word", "") ==# word
return
endif
call s:hl_clear()
if !g:enable_highlight_cursor_word
return
endif
if !empty(filter(split(word, '\zs'), "strlen(v:val) > 1"))
return
endif
silent! let b:highlight_cursor_word_id = matchadd("CursorWord", word)
let b:highlight_cursor_word = word
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment