开发者

Notepad++ like "multi editing" in Vim?

开发者 https://www.devze.com 2022-12-29 10:04 出处:网络
I’m switching from Notepad+开发者_StackOverflow中文版+ 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 yo

I’m switching from Notepad+开发者_StackOverflow中文版+ 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.


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›, '.')
    


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


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


ib's response and the multi select vim plugin are interesting, but the following is a suggestion that does not require a special function or plugin.

Temporarily set foldmethod=manual, then mark the blocks you want to operate on with zf.

Finally, use the ex command :folddoclosed to do ex commands on the folded blocks.

For example: :folddoclosed norm Iinsert some text at the front

Note, you can use :folddoclosed on any folded groups of lines, so you could use other foldmethods... but usually it makes sense to manually create the folds.

You can also use visual markers, followed by :norm which gives you :'<,'>norm... But visual markers only let you select a continuous range of lines. Using folds and :folddoclosed you can operate on multiple ranges of lines at once.

Another tip... to save time having to type out :folddoclosed, I will type :fo<shifttab><shifttab><shifttab>

0

精彩评论

暂无评论...
验证码 换一张
取 消