One of the magical things about Vim is that it uses nouns and verbs to construct an editing language. For example d
(delete) is a verb and w
(word) is a noun. By combining a noun & verb we make an edit. Learning this language is key to getting the most out of your editing, I recommend this great talk by Chris Toomey if this concept is new to you.
Once you have the basics down you know how to operate on the standard text objects that vim provides you might want to start adding your own. Thankfully there are some excellent plugins to add a whole host of extra objects. What is nice about these extensions to the language is they feel natural and intuitive and don't take much investment to learn at all.
Examples
Below I have listed a few of my favourite extensions with a link to the plugin on Github as well as the Plug line for them. I've also added a very simple "selling point" on what the plugins do, but if one piques your interest do check out the full docs on the github page because a lot of these are a lot more powerful than the simple examples I am listing. Also pipe (|
) denotes cursor position.
Surround
This is probably the most essential plugin for Vim, it feels totally natural and whenever I encounter vanilla Vim I inevitably try to use it forgetting that it isn't there by default!
This allows us to select (
surrounding)
brackets, braces, quotes, etc. So cs"'
would change surrounding "
's to '
's.
"b|ar"
-> cs"'
-> 'bar'
Plug 'tpope/surround'
Targets
Vim allows us to do things like ci"
to change text inside quotes, but what about other markers? Targets lets us do this to pretty much everything else that would encapsulate text, such as ,
's or +
's.
For examples to delete a string from a concatenation da+
, will remove the string and +
from under the cursor.
foo + 'str|ing' + bar
-> da+
-> foo + bar
Plug 'wellle/targets.vim'
Text Object User
This isn't in itself a text object, but it provides an easy framework for other users to create their own. This means that it is a pre-requisite for all of the following objects.
I will list my favourite ones, but I recommend you check out this list of all the existing objects that have been created for this framework, as I suspect there are some that might appeal to you and not me!
Plug 'kana/vim-textobj-user'
Indent
Ever wanted to select a block of code by it's indentation level? Well this allows you to do just that, with ii
and ai
.
foo foo
b|ar -> `dii` -> foo
bar
foo
Plug 'kana/vim-textobj-indent'
Comments
This is really handy, especially for multi-line comments. Simply provides ic
and ac
to edit comments.
// Co|mment
-> cic
-> // |...
Plug 'glts/vim-textobj-comment'
Functions
This works for Java and C, however there is most likely a language specific version that will provide the same functionality. I use this one for JavaScript.
You can now use if
and af
to select the contents of a function or the whole function itself.
void foo() { void foo() {
return bar; -> `dif` -> }
}
Plug 'kana/vim-textobj-function'
Conclusions
There are a lot more of these extensions out there, for your specific language or for that common repeating text pattern that you want to operate on, but can never find a nice way of selecting it.
I usually advise against adding lots of plugins at once, but hopefully with the use of the vim language these should seem like second nature as soon as you are aware of the new objects you can operate on.
:wq