Skip to content

Commands at cursors

mg979 edited this page Apr 11, 2019 · 3 revisions

As you've already seen, you can run vim-surround commands at cursors. But you can run any command:

  • normal commands: recursive or not, in cursor mode
  • visual commands: mostly recursively, in extend mode
  • ex-commands: in cursor mode
  • macros: in cursor mode

Visual commands can only be run in extend mode. Other commands can be run in extend mode, but selections are collapsed to cursors before the commands are run. Note that mappings mentioned in pictures/descriptions may be different (they have changed since the pictures were made).


Mappings

Most commands that you can run at regions start with g:VM_leader. Mappings:

. Run Dot
leader-@ Run Macro
leader-z Run Normal
leader-v Run Visual
leader-x Run Ex
leader-Z/kbd> Run Last Normal
leader-V Run Last Visual
leader-X Run Last Ex

Dot command

Imgur

The dot command works under certain conditions. If you edit something outside of VM, then enter VM and press ., the last edit is repeated. This works as in vim, because you're essentially running a macro.

What doesn't work, is to repeat a change that you did inside VM, because most VM commands are split into multiple edits and as such are not repeatable. One exception is the delete command run at cursors (dw etc), that is repeatable. The way it should be used is anyway the first one: make a change outside VM, then you can repeat it at cursors with dot (.).


Normal commands

Imgur

Pressing leader-z you will be asked for a normal command to run. It can be any mapping you have defined, also from plugins. Mappings that include special key should be entered with C-v or C-k followed by the keypress. Pressing leader-Z will repeat the last entered command.

In the example I'm using cr_ from vim-abolish, after having selected a pattern with S-left, created a visual selection and pressed ctrl-f to find occurrences inside of it, then press leader-z to run command on current regions/cursors. Instead of creating a visual selection + ctrl-f, I could have pressed mip (find operator: 'match in paragraph') to achieve the same effect.

Note that if run from extend mode, regions are collapsed first, so the command will run as if executed at the start of the region (or at the end if you pressed o to invert facing). Pressing be to select the word, as it's show in the example, isn't necessary, but it would have been if you wanted to run a visual command instead (see below).


Visual commands

Imgur

Pressing leader-v you will be asked for a visual command to run. Press leader-V to repeat the last entered command.

Here I'm using alt-ctrl-down to create cursors, } to navigate to second cursor, Q to remove it, e to select until end of the word, finally leader-v and the vim-surround visual command to be run.


Ex commands

Pressing leader-x you will be asked for an ex command to run. Press leader-z to repeat the last entered command. [count] is accepted.

You can define custom aliases/ custom commands that can be used in this command line.


Macros

Press leader-@ followed by a register, to run that macro. It's advised that you don't run macros that are too complex, that include line changes, non-standard keys (such as arrow keys). Double check the results to be sure... But that's often the case with macros, in my own experience.

See Customization for functions that you can call before macro/commands execution.


Other commands

These commands also use the g:VM_leader key:

leader-- Shrink regions by 1
leader-+ Enlarge regions by 1
leader-< Align by Char
leader-> Align with Regex
leader-n Numbers (insert)
leader-N Numbers (append)
leader-0n Numbers (insert) (start from 0)
leader-0N Numbers (append) (start from 0)

Shrink/enlarge: decrease/increase the width by 1 from both sides. Other commands will be discussed later.