2

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!

1

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.

  • Sweet! vmap I I<F12> along with your script does the job. Do you know if there's a way to make the cursor appear on the line while the insert's happening? – JoshuaD Jan 4 '16 at 5:41
  • This page appears to offer a solution to the cursor problem: vim.wikia.com/wiki/… – Jim K Jan 4 '16 at 5:49
  • Yes, it works -- add this to make the cursor appear on the line: imap <F12>ignore-me <Nop> – Jim K Jan 4 '16 at 5:53
  • That doesn't work for me. – JoshuaD Jan 5 '16 at 6:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.