Skip to main content

Movement-action Submodes

Movement-actions are actions that have to be used with Core Movements and Selection Modes.

Movement-actions should be considered as the submodes of the Normal mode.

To return back to normal mode from one of these movement-action modes, press esc.

1. Exchange

Keybinding: x
Memory aid: exchange

In the Exchange submode, every core movement means:

exchange with <movement>.

For example, using the following Javascript code:

f(x, 1 + 1);

Suppose:

  • The current selection mode is Syntax Node
  • The current selection is x
  • The current submode is Exchange

... then executing Next swaps the first argument of f with its second argument:

f(1 + 1, x);

Tips

Since exchange works with every core movement, it can be used with Jump and Syntax Node to swap two distant expressions.

For example, using the following Rust code:

if x > 0 {
println!("Yes")
}
else {
x += 1;
println!("no")
}

...we can swap the body of the if-else expression by:

  1. Set selection mode to Syntax Node by pressing s
  2. Jump to the body of if by pressing f {, then press the letter that appears on top of the first {.
  3. Enter Exchange submode by pressing x
  4. Press f {, then press the letter that appears on top of the second {
  5. Done

2. Multi-cursor

Keybinding: q
Reason: q is used to start recording a macro in Vim, but I realized 80% of the time what I need is multi-cursors, not a macro.

In the Multi-cursor submode, every core movement means:

Add cursor with <movement>

Use the following text as an example:

hello ki, hello vim, hello helix

Suppose:

  • The current selection mode is Find Literal "hello"
  • The current selection is the first hello
  • The current submode is Multi-cursor

... then executing Next adds a new cursor to the second hello.

3. Replace

Keybinding: ; [^2]

In the Replace submode, every core movement means:

Replace current selection until <movement>

Unlike Exchange and Multi-cursor, this submode is not essential, it is a kind of shortcut for certain operations.

Suppose you have the following text:

"hello     world";

and wish to turn it to:

"hello";

In this case, you can select world, Change it, and press backspace 5 times.

However, that's inefficient, and that can be shortened by:

  1. Select hello
  2. Set selection mode to Word
  3. Copy
  4. Enable selection extension
  5. Move to world (by pressing l)
  6. Replace

And seeing that Steps 3 to 6 is a common chore, the Replace mode is actually a shortcut for that.

Here's how it works using the Replace mode (starting from step 3):

  1. Enter the Replace submode
  2. Press l
  3. Press esc to return to Normal mode

The rigorous readers might have noticed the similarity of the Replace submode with the Raise action, that is in fact the case, under the hood, Raise is but a specialized version of the Replace mode which only executes the Up movement.