tags:

views:

593

answers:

3

I'm switching from Notepad++ to VIM as main text editor.

Notepad++ can have multiple cursors by holding down ctrl and clicking anywhere in the text, so if you type, the text appears in multiple locations.

Is it possible in vim? Something like insert after selecting multiple rows in visual mode, but with the possibility to have cursors anywhere in the text.

It's a feature I rarely use, also it's quite easily avoidable, I'm just curious, since its the only one I could't find a replacement for in vim yet.

+3  A: 

There is no built-in feature of that kind.

Here I suggest a function that repeats command (for example '.' repeating last change command) at the positions of given marks. Both marks and command are specified as strings. Marks specified in the way ranges in regexp or scanf format specifier defined. For example, 'za-dx' means marks 'z', 'a', 'b', 'c', 'd', 'x'.

function! MarksRepeat(marks, command)
    let pos = 0 
    let len = strlen(a:marks)
    let alpha = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    let beta =  '1234567899bcdefghijklmnopqrstuvwxyzzBCDEFGHIJKLMNOPQRSTUVWXYZZ'
    while pos < len 
        if a:marks[pos + 1] != '-' 
            execute 'normal `' . a:marks[pos] . a:command
            let pos += 1
        elseif a:marks[pos] <= a:marks[pos + 2]
            let mark = a:marks[pos]
            let stop = a:marks[pos + 2]
            if mark =~ '[0-9a-zA-Z]' && stop =~ '[0-9a-zA-Z]'
                while 1
                    execute 'normal `' . mark . a:command
                    if mark == stop
                        break
                    endif
                    let mark = tr(mark, alpha, beta)
                endwhile
            endif
            let pos += 3
        endif
    endwhile
endfunction

So the use-case you asking for is following:

  1. Mark places (except one) to insert simultaneously using Vim marks ('m' command).
  2. Actually insert text in the place you did not mark.
  3. :call MarksRepeat(‹marks›, '.')

The last step, of course, could be simplified by definition of custom command and/or mapping.

ib
thanks, works fine. Im thinking of re-writing it, without using marks(im a complete newb to programming vim so its gonna take some time, im not sure if its even possible, but thanks for getting me started!)
proto-n
+2  A: 

Check multi select vim plugin: http://www.vim.org/scripts/script.php?script_id=953

Maxim Kim
A: 

You could insert the text in one place, in a single operation, then use '.' to repeat that insertion at each other place you want the text.

It's the converse of what you asked for, because you wanted to mark the locations before entering the text, but it gives you the same result in the same number of keystrokes :).

Andrew Aylett