19

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.

4
  • 3
    I downloaded the latest version of NP++ and nothing happens when I ctrl-click. I don't get two cursors. Commented May 9, 2010 at 3:22
  • 3
    yep, that has to be enabled under preferences/editing Commented May 9, 2010 at 9:49
  • 2
    Wow, that's kinda cool. I did not know that feature. Commented Nov 2, 2010 at 23:47
  • Nice! I didn't know about that feature either. Commented May 24, 2012 at 13:40

4 Answers 4

6

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›, '.') 
Sign up to request clarification or add additional context in comments.

2 Comments

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!)
Interesting solution using marks. Check out my solution that does not require a special function. (using :folddoclosed)
5

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

Comments

4

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

Comments

3

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>

1 Comment

Using manual folds as markers is a very nice idea! However, in contrast to the attempt with marks, here it is not possible to store particular positions on those lines, since folds are line-wise objects. Anyway, multiple cursors do not match well with Vim editing model.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.