Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notepad++ like "multi editing" in Vim?

Tags:

vim

notepad++

I’m switching from Notepad++ to Vim as my main text editor.

In Notepad++, you can have multiple cursors by holding down Ctrl and clicking anywhere in the text, so that 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, and it’s also quite easily avoidable; I’m just curious, since it’s the only one I could’t find a replacement for in Vim yet.

like image 238
proto-n Avatar asked May 08 '10 21:05

proto-n


People also ask

Does Notepad support Vim?

it is a plug-in for notepad++, which make it possible to edit as vi/vim in notepad++. ViSimulator simulates/emulates most frequently-used vi/vim commands to provide more powerful editing capability for notepad++.

How do I edit the same words in Notepad++?

To Multi-Edit. You first need to enable this feature in Notepad++. Do this by going to Settings → Preferences → Editing and then enable Multi-Line Edit. Now when you hold Ctrl and click around your text, a cursor will be left at the location of each click.

How do you edit multiple rows in Notepad++?

Multiple line editing in Notepad++ Now Check Enable (Ctrl+Mouse click/selection) under Multi-Editing Settings. Now keep pressing Ctrl Button and Click on all places where you want the Cursor to Edit text simultaneously. Once you are done you can start editing at multiple places on the same file.


2 Answers

There is not a built-in feature of that kind.

Let me 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 string arguments. Marks specified in the way ranges in regular expressions or scanf-format specifier are 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] != '-'
            exe 'norm `' . 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
                    exe 'norm `' . mark . a:command
                    if mark == stop
                        break
                    endif
                    let mark = tr(mark, alpha, beta)
                endwhile
            endif
            let pos += 3
        endif
    endwhile
endfunction

In your case, the function could be used as follows.

  1. Mark all places for simultaneous insertions (except one) using Vim marks (by means of m command).
  2. Actually insert text in the one place that has not been marked.
  3. Run the function:

    :call MarksRepeat(‹marks›, '.')
    
like image 126
ib. Avatar answered Oct 03 '22 15:10

ib.


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 :).

like image 37
Andrew Aylett Avatar answered Oct 03 '22 15:10

Andrew Aylett