neovim icon indicating copy to clipboard operation
neovim copied to clipboard

Vim-style editing: Text objects

Open practicalli-johnny opened this issue 2 years ago • 0 comments

text-objects are used in visual or operator-pending mode.

w for instance stands for the text-object word. When you are in operator pending mode, for instance right after pressing d for delete, pressing w (so dw) will delete from the cursor position to the end of the word. Two modifiers exist to create more powerful text-objects: a (for around) and i (for inner). The former will act on the text-object on its right plus its surroundings delimiters (for a word, it’s basically the whitespaces). The latter, i, will apply the operation only inside the text-object. For a word, it means just the word, ignoring the whitespace around it.

With this, you can do really powerful editing. For instance, if you have this HTML code:

<div class="stuff">I like cookies</div>

And you want to change I like cookies to I love pasta, you can simply put your cursor anywhere in that text, and press cit, which translates to:

c = change i = inner t = tag

t is a text-object native to Neovim that can recognize “tags” of the filetype you are editing (here, HTML tags). So that command will change the text inside the tag. If you wanted to completely replace the whole tag with another one, you could have pressed cat, which would have removed the surroundings (i.e. the

and
).

practicalli-johnny avatar Apr 13 '23 08:04 practicalli-johnny