Vim – Is there a way to get immediate visual feedback during Insert in Visual Block Mode

vivim

I often press:

  • <Ctrl> V to go into visual block mode and highlight a column
  • hit <shift>-i (to insert at the beginning of the line), type a few things (at this point, I see my changes on the first line, but not any others).
  • and then hit <Esc> to go back to normal mode.

After I hit <Esc>, I see the stuff I inserted get put in each of the other lines.

What I would like is to visually see my changes on each line, as I type, rather than only on the first line. Is that possible?

Go into Visual Block, select a bunch of lines
enter image description here

Hit <Shift> i
enter image description here

Add some text – This is the part I'd like to be changed. I'd like for the spaces to be immediately visible on all lines, not just the first line.
enter image description here

Hit Escape – Now the spaces are added on every line. I'd like for this to happen sooner.
enter image description here

I know that I could have done what I did in this example with >>. I chose a simplified example to illustrate the thing I'd like to change.

Thanks!

Best Answer

Try adding this to your vimrc:

nmap <buffer> <silent> <expr> <F12> InsertCol()
imap <buffer> <silent> <expr> <F12> InsertCol()

function! InsertCol()
    let w:first_call = exists('w:first_call') ? 0 : 1
    "if w:first_call
    "    startinsert
    "endif
    try
        let char = getchar()
    catch /^Vim:Interrupt$/
        let char = "\<Esc>"
    endtry
    if char == '^\d\+$' || type(char) == 0
        let char = nr2char(char)
    endif " It is the ascii code.
    if char == "\<Esc>"
        unlet w:first_call
        return char
    endif
    redraw
    if w:first_call
        return char."\<Esc>gvA\<C-R>=Redraw()\<CR>\<F12>"
    else
        return char."\<Esc>gvlA\<C-R>=Redraw()\<CR>\<F12>"
    endif
endfunction

function! Redraw()
    redraw
    return ''
endfunction

Then press Ctrl-vI as usual, and then press F12. It will insert and show all lines changed for each keystroke.

Note: The script would be better if pressing F12 would go into insert mode, instead of requiring you to press I each time. Maybe the startinsert command in combination with vnoremap could do this.

Related Question