Say the cursor is in the middle of a word and we want to delete it. We can either:
bde(go to the beginning then delete) like a peasant
- or use
diw(delete inside a word) like an absolute chad.
diw is the combination of Vim command
d and the text object
It can be roughly translated as
delete inside word.
Commonly, text objects start with
r which stands for
Some relevant text objects are:
||inside square bracket ]|
||round square bracket ]|
||inside html tag|
||around html tag|
When combining with Vim command
y, we have the powerful Vim language. For examples:
dimdelete inside method
ci'change inside single quote
yi]yank inside square bracket
citchange inside html tag
Try doing the above examples the normal way to appreciate how bad-ass and efficient text objects are
(change i to a and we have around method, around single quote, etc.).
To practice, I found this free course from thoughtbot really helpful.
But seriously, WHY?
A few reasons to use text objects:
- easy to remember and apply because they feel really close to the human language.
- repeatable with the dot
- we can define our own text object.
There is a plugin for Ruby block to be
selected as text objects with
ar(which, you guess it, means
inside ruby block/
around ruby block).
Just try these tips for a few days to see if they really improve your productivity as they did to me. You’ve got to get your hand dirty to appreciate the beauty of the Vim language (or Vim in general).