Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Repeat and combine edits

The textobject exercises show how to select one piece of text before changing it. Now use selections to decide which occurrences should change, then apply an edit once or repeat it where needed.

Open a fresh scratch buffer with :new for each exercise. Paste or type the starting text, then press Esc. Each example starts with one selection; press , to keep only the primary selection if needed.

Review repeated matches one at a time

Sometimes the same word appears several times, but only some occurrences should change. Start with:

draft draft draft
  1. Move onto the first draft, press miw, then *. This makes the selected word the search pattern; it does not move to another match.
  2. Press Alt-r, type ready, then Enter to replace the first selection.
  3. Press n to inspect the second match. Leave this one alone.
  4. Press n again to select the third match, then . to repeat the replacement.

The result is:

ready draft ready

Use n to find a match and . only when you want to apply the edit there. Search wraps by default, so stop after reviewing the intended matches. In Mitos, . repeats the last insertion or prompted replacement. It does not replay every kind of edit or the motions you used to find the text. See Macros and undo for repeating a sequence of commands.

Change every match in a chosen region

Use multiple selections when all the matches should receive the same edit. For this example, start with:

user_id = 7
backup_user_id = 8
print(user_id)
  1. Press % to select the whole buffer.
  2. Press s, type \buser_id\b, then Enter.
  3. Check the highlighted selections. There should be two; backup_user_id should stay unselected.
  4. Press Alt-r, type customer_id, then Enter.

The result is:

customer_id = 7
backup_user_id = 8
print(customer_id)

s searches within the current selections. To limit a change to a few lines, select those lines instead of pressing %: with one cursor on the first line, press 3x to select three lines, for example. You can also use mip for a paragraph or mif for a function body when the language has textobject support.

The pattern is a regular expression. \b marks a word boundary, so the example matches user_id as a whole word. A dot in a pattern means any character; use \. to match a literal dot. Replacement text entered with Alt-r is literal. See Search and replacement for more details.

Text matching does not distinguish two variables with the same name. For a code symbol, prefer Space r when your language server supports renaming: it can identify references to that symbol across files.

Add a prefix to selected lines

Selections can represent whole lines as well as words. Start with:

TODO write tests
DONE draft guide
TODO update screenshots

To turn only the TODO lines into checklist items:

  1. Press %, then Alt-s to create a selection for each line.
  2. Press K, type ^TODO\b, then Enter to keep only selections starting with the word TODO.
  3. Check that the DONE line is no longer selected.
  4. Press I, type - [ ] , including the trailing space, then Esc.

The result is:

- [ ] TODO write tests
DONE draft guide
- [ ] TODO update screenshots

I inserts before the first nonblank character of each selected line, so it preserves existing indentation. Use A instead when you want to append text at each line’s end. See Selections for more ways to build and filter selections.

Choose how to repeat work

What needs to happen?A useful starting point
Review matches and change only someSearch with / or *, move with n, repeat an insertion or replacement with .
Apply the same edit to all matches in a regionSelect the region, use s, then c or Alt-r
Add text to several linesSelect the lines, split with Alt-s, then use I or A
Repeat several commands in orderRecord a macro with Q, stop with Q, replay with q
Rename a code symbol and its referencesUse Space r with a language server that supports renaming

For a macro, make the starting position predictable and finish where the next replay should begin. For example, use a line boundary or a search rather than a fixed number of left and right movements. Replay once and check the result before adding a count such as 3q.

Previous: Edit code with textobjects · Next: Navigate a project