I use a little script to trigger insert modes in order to change the line number color:
function! CursorLineNrColorInsert(mode)
" Insert mode: blue
if a:mode == "i"
highlight CursorLineNr ctermfg=4
highlight CursorLineNr guifg=#268bd2
" Replace mode: red
elseif a:mode == "r"
highlight CursorLineNr ctermfg=1
highlight CursorLineNr guifg=#dc322f
else
highlight CursorLineNr ctermfg=0
highlight CursorLineNr guifg=#073642
endif
endfunction
autocmd InsertEnter * call CursorLineNrColorInsert(v:insertmode)
autocmd InsertLeave * highlight CursorLineNr ctermfg=0
autocmd InsertLeave * highlight CursorLineNr guifg=#073642
That works pretty fine and changes my line number instantly when I enter any insert mode and reverts back to the original color in normal mode.
I would like to do the same for the visual mode:
function! CursorLineNrColorVisual(mode)
" Visual mode: orange
if mode()=~#"^[vV\<C-v>]"
highlight CursorLineNr ctermfg=9
highlight CursorLineNr guifg=#cb4b16
else
highlight CursorLineNr ctermfg=0
highlight CursorLineNr guifg=#073642
endif
endfunction
autocmd CursorMoved * call CursorLineNrColorVisual(mode())
Basically that works but not instantly since the function is triggered on CursorMoved
. How could I fire CursorLineNrColorVisual()
instantly as soon as I would activate any visual mode?
You can enter visual mode from insert mode. In insert mode, you can execute a normal mode command with Ctrl-o. While you are in this normal-mode-ready, run visual mode key v. If you notice, on the bottom left, it will say -- (insert) VISUAL--. This works with any visual mode key: v/V/Ctrl-v. In addition to visual mode, vim has a select mode.
To master vim, you must master all of its modes. Let's start with the easiest basic mode: visual mode. This article will show you the good parts of visual mode. I hope you will find this helpful on your journey to become vim master.
Press v to enter visual mode, this will also mark a starting selection point Move the cursor to the desired end selection point; vim will provide a visual highlight of the text selection Visual mode also has the following variants: V to enter visual line mode, this will make text selections by line
Press V and watch Vim selects the entire line your cursor is on. Just like character-wise visual mode, if you run gU, Vim uppercases the highlighted characters. Block-wise visual mode works with rows and columns. It gives you more freedom of movement than the other two modes.
After spending some time in :help
I ended with the following setup:
" Colorize line numbers in insert and visual modes
" ------------------------------------------------
function! SetCursorLineNrColorInsert(mode)
" Insert mode: blue
if a:mode == "i"
highlight CursorLineNr ctermfg=4 guifg=#268bd2
" Replace mode: red
elseif a:mode == "r"
highlight CursorLineNr ctermfg=1 guifg=#dc322f
endif
endfunction
function! SetCursorLineNrColorVisual()
set updatetime=0
" Visual mode: orange
highlight CursorLineNr cterm=none ctermfg=9 guifg=#cb4b16
endfunction
function! ResetCursorLineNrColor()
set updatetime=4000
highlight CursorLineNr cterm=none ctermfg=0 guifg=#073642
endfunction
vnoremap <silent> <expr> <SID>SetCursorLineNrColorVisual SetCursorLineNrColorVisual()
nnoremap <silent> <script> v v<SID>SetCursorLineNrColorVisual
nnoremap <silent> <script> V V<SID>SetCursorLineNrColorVisual
nnoremap <silent> <script> <C-v> <C-v><SID>SetCursorLineNrColorVisual
augroup CursorLineNrColorSwap
autocmd!
autocmd InsertEnter * call SetCursorLineNrColorInsert(v:insertmode)
autocmd InsertLeave * call ResetCursorLineNrColor()
autocmd CursorHold * call ResetCursorLineNrColor()
augroup END
In order to restore the color of line numbers after leaving the visual mode I had to do the following steps:
updatetime=0
for CursorHold
eventsautocmd CursorHold
updatetime=4000
for CursorHold
eventsIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With