The Vim Way
1. Dot Command
.command repeats the last change
- Changes can operate on characters, lines and even the whole file.
- For example to delete a comment like
//at the start of a line you might enter
3dl. Moving to the next line and pressing
.will remove the comment characters from that line also.
2. Reduce Extraneous Movement
- Vim has operations which are effectively compounds operations e.g. move the cursor to somewhere and start editing, e.g.
Acan be thought of as
- Should favour these as it helps with the dot command - we don't need to position the cursor ourselves.
- Other examples
3. Use Idiomatic VIM commands for editing
- For complex editing we want a sequence of commands that (optionally delete current text and ) enter insert mode, insert changed text and exit insert mode.
- This can be repeated using the
- If we do a search to find occurences of where to perform the operation then we can repeatedly make the change using
;to repeat the search and
.to make the change.
4. Act, Repeat, Reverse
- When facing a repetitive task try to make the motion and the change repeatable.
.repeats the last change
@:repeats the last Ex command (i.e.
;repeats the last f
nsearches againt for a pattern specified using
uundoes the last change or substitution
,undoes the last find command (f
Nundoes the last
5. Find and Replace by Hand
- Idea is to make the first replacement by hand and then use the
.command to repeat the next changes. Can be useful if you need to manually verify each change e.g. search/replace some instances of a text snippet.
- Can use
*to seach for the word under the cursor at the moment and
nto find the next match.
- Combining these we get the following workflow
- Find the first instance of the text you are searching for
*to find the first instance that we wish to change (if not at it already)
- make the change e.g. using
cwto change a word. Drop into insert mode only once so that VIM records all the changes as a single edit.
- now press
nuntil you find the next instance you wish to change and
.to make the change.
6. Dot Formula
- Ideally for repeated edits we would use the formula of one keystroke to move and another keystroke to edit e.g.