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
- Move onto the first
draft, pressmiw, then*. This makes the selected word the search pattern; it does not move to another match. - Press
Alt-r, typeready, thenEnterto replace the first selection. - Press
nto inspect the second match. Leave this one alone. - Press
nagain 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)
- Press
%to select the whole buffer. - Press
s, type\buser_id\b, thenEnter. - Check the highlighted selections. There should be two;
backup_user_idshould stay unselected. - Press
Alt-r, typecustomer_id, thenEnter.
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:
- Press
%, thenAlt-sto create a selection for each line. - Press
K, type^TODO\b, thenEnterto keep only selections starting with the wordTODO. - Check that the DONE line is no longer selected.
- Press
I, type- [ ], including the trailing space, thenEsc.
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 some | Search with / or *, move with n, repeat an insertion or replacement with . |
| Apply the same edit to all matches in a region | Select the region, use s, then c or Alt-r |
| Add text to several lines | Select the lines, split with Alt-s, then use I or A |
| Repeat several commands in order | Record a macro with Q, stop with Q, replay with q |
| Rename a code symbol and its references | Use 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