Step 5 of 6

Surround: add, replace and delete pairs

Surround: add, replace and delete pairs

So far match mode has only selected. It also edits — specifically, it edits the pairs themselves. Three commands cover it, and each one takes the pair character as its last key:

  • ms — surround the current selection with a pair: ms" wraps it in double quotes, ms( in parentheses, and so on
  • mr — replace the closest surrounding pair with another: mr"' turns "text" into 'text'
  • md — delete the closest surrounding pair, keeping what was inside: md( turns (text) into text

ms works on whatever is selected, so pair it with a selection first — miw to wrap a word, x to wrap a line, or any selection from the last few steps. mr and md don’t need a selection at all; they look outward from the cursor for the pair you name, so the cursor just needs to be somewhere inside it.

In most editors, changing a string’s quote style means deleting and retyping at both ends. Here it’s three keys.

Try it in the editor. Each line below wants a different surround command — work through the checklist.

  1. Wrap a word in double quotes Put your cursor on hello on line 3, select it with miw, then type ms".
  2. Replace double quotes with single quotes Move onto single on line 4 (inside the quotes), then type mr"'.
  3. Delete the parentheses Move onto nothing on line 5, then type md(.
Wrap a word in double quotes Put your cursor on hello on line 3, select it with miw, then type ms".

Next: Replace double quotes with single quotes

Loading editor…