I'm a fan of Vim, I've been using Vim syntax in my work for more than a year, and I so love it. The most thing I love in Vim is the key combinations, which help me quickly edit the text. So today I'd like to share with you guys some key combinations that I usually use in my daily work. I hope they can help you be more productive in coding and inspire you to use or learn Vim.
Combinations
Below are examples of my screenshoots to show how the combinations work, they are not completely enough and I can add more later.
"ciw"
Change inner the word under the cursor
"cit"
Change the content inner a tag
"ci("
Change the content inner round bracket
"ct="
Change the content from the cursor to before the "="
"cc"
Change current line - Delete the current line and turn into insert mode
Main keys
"c" is the main command to change text object in Vim. There are many types of text object such as word, content inside a tag or brackets, or content before, after a character, etc. Below are two key combinations I always use:
- i - is a command to turn into insert mode in Vim, but when it combines with "c" like "ci", "i" means inner a text object.
- a - is also a command to turn into insert mode but the text editor cursor will place after the current cursor. "ca" means change the text object and the thing around it.
We can combine many keys to represent the boundary like:
- ci" to change the content inner the ""
- ci< to change inner the <>
- ca< to change the content inner the <> and also delete the <>
And more.
You might think there are lots of things to remember, but believe me, when you practice and practice everyday, you can dance with those combinations like a pro.
Top comments (10)
Here's a little trick:
cib
is equivalent toci(
. I usecib
because it is a little bit faster to pressb
instead of(
which isShift
+9
in QWERTY layout. This improvement seems small, but over the time this can make a big difference.Very good point. Thanks for the post, Max!
Wow. Thanks @m4xshen. That's a good trick. Is there any same thing that we can apply for [], and {}?
Yes, can use
ciB
instead ofci{
. However I personally don't use this because the speed are pretty much the same (pressingB
also requiresShift
key).You can also do
c%
if you're on the character itself, regardless of what that character is so long as it is part of a matched pair.Great article, Khang!
I have an explanation for 'ct':
"ct=" change text til the "="
We also use "ct" with another character. Eg: "ct,", "ct:", "ct_". It's really helpful when working with objects or JSON.
Yeah, thanks @huantd1 for your explanation, that makes "ct" command more clearly.
Very insightful!
This is great. The gifs really help visualize what's happening.
Very good write-up Khang!