This is one of my favourite Vim features. Say you have the following code:
Say your cursor is where the caret indicates. Typing
ci) (“change inside parens”) in normal mode will:
- delete all the text between the two matching parens
- place you in insert mode with the cursor between the two (now adjacent) parens
- put the deleted text in the yank buffer so that
pwill paste it.
The use case here is obviously so you can assign a name to that long complicated expression.
ci) is much easier than selecting it with the mouse, and keeps your hands on the keyboard where they belong ;)
With nested parentheses, it does what you expect (affects the text contained by the innermost matching pair to contain your cursor - try it and see).
Other equally useful variants:
i"- “inside double quotes” - everything between double quotes
i'- “inside single quotes”
iw- “inside word” - the word the cursor is on
is- “inside sentence” - great for editing prose
ip- “inside paragraph”
There are also similar motions beginning with “a”:
i)but includes the parens (e.g.
da)deletes everything inside parens and the parens themselves)
iwbut includes trailing whitespace.
For another great taste that goes great with this, see the surround.vim plugin. To whet your appetite: six keystrokes to wrap your current selection in <div> tags; four to change a string from “double-quoted” to ‘single-quoted’.
Comments are disabled, but please feel free to reach out on social media if you'd like to discuss this post!
Comments from a previous URL for this post (which now redirects here):