Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run command when vim enters visual mode

Tags:

vim

triggers

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?

like image 233
Saucier Avatar asked Mar 22 '13 01:03

Saucier


People also ask

How do I enter visual mode in Vim?

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.

How do I Master Vim?

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.

How do I select a specific line in Vim?

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

How do I move the cursor in Vim?

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.


1 Answers

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:

  1. Remap relevant key bindings to call an "enter-visual-function"
  2. While entering visual mode the function sets updatetime=0 for CursorHold events
  3. Call a "leave-visual-function" by autocmd CursorHold
  4. While leaving visual mode the function resets updatetime=4000 for CursorHold events
like image 187
Saucier Avatar answered Oct 02 '22 22:10

Saucier