0

This comment convinced me that I don't need multiple cursors, however, it's missing one feature that multi-cursor can do, which is to perform actions on multiple regex matches on the same line.

:g is very close to what I wanted, but it only works line-wise.

For example, I want to add 10 to all numbers in this line:

padding: "4px 8px 16px 16px"

The regex to match all numbers in this line is /\d\+ , let's say there's such a hypothetical command called multi. Then I can do :multi/\d\+/norm 10^A to perform what I wanted. (^A is inserted by pressing Ctrl-v then Ctrl-a)

So, is there a command that behaves like multi in Vim?

Here's a video reference of what I wanted to achieve (done in Helix):

https://asciinema.org/a/JYrK862VJn3nzFlM3zEjDobSV

Cross-posted from: https://www.reddit.com/r/neovim/comments/11rmgky/apply_normal_command_to_all_regex_matches_on_the/

1 Answer 1

3

So, is there a command that behaves like multi in Vim?

No.

But, for this specific use case, there is :help :s and :help sub-replace-\=:

:s/\d\+/\=10+submatch(0)/g

which is not that more complicated than your tentative:

:multi/\d\+/norm 10^A

If you think about it, the normal mode edits you might want to perform on those matches can pretty much all be expressed that way, and more:

  • decrement?

    :s/\d\+/\=submatch(0)-10/g
    
  • multiply? (not possible in normal mode)

    :s/\d\+/\=submatch(0)*10/g
    
  • prepend some arbitrary text?

    :s/\d\+/foo&/g
    
  • append some arbitrary text?

    :s/\d\+/&foo/g
    
  • replace with some arbitrary text?

    :s/\d\+/foo/g
    
  • surround with quotes?

    :s/\d\+/'&'/g
    
  • delete?

    :s/\d\+//g
    
  • uppercase? (yes, it is silly)

    :s/\d\+/\U&/g
    
  • split before?

    :s/\d\+/\r&/g
    
  • etc.

So, I kind of get the novelty of the mechanism you documented in that "video" (and the appeal of "multi-cursor-first" editors like Kakoune and Helix) but the expressiveness of Ex commands like :s goes a very long way.


Non-modal multiple cursors, modal repeat, substitutions, and modal multiple cursors are all different and valid approaches to a same problem.

With non-modal multiple cursors, you place cursors at different locations, and then you add or remove text at each cursor at the same time because that's all you can do at the cursor in a non-modal text editor.

With modal repeat, you perform a change, then you move the cursor to another place, then you repeat the change, then you move again, etc. because you only have one cursor.

With substitution, as in this answer, the entire problem is treated in an abstract way, without moving the cursor or selecting things. It is incredibly powerful but too abstract for many.

With "modal multiple cursors" (is there an official name for that?), you kind of combine the good parts of the other approaches, which is good, I guess.

All of those approaches are valid and fit different usage patterns and use cases so one should probably refrain from deciding that multiple cursors is not for them. If it solves your problem better and you have it (or can have it), then why not use it?

2
  • I'm not looking for the specific case of substitution, it was merely an example of multi cursor. Anyway, is there a way where I can perform normal action in the substitute command? If so that would answer my question Mar 17, 2023 at 1:51
  • That is literally the second line of my answer.
    – romainl
    Mar 17, 2023 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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