I'll show you some tips to help move your editing skills from beginner towards intermediate.
Ok, so you know the basics of vim.
You know about insert mode and command mode. You press i to start inserting text, esc to get back to command mode. You even know some advanced things like how to exit vim—life is pretty good.
I want to share a few tips to level up your editing skills from beginner to intermediate.
As a programmer, you spend a lot of time editing and navigating code. So regardless of which editor you use, it’s worthwhile to invest time into learning how to get more out of the tools you use every day.
Getting better at using your editor is not only about editing faster, but also about being more effective and accurate. As your skills improve you can be more precise and make fewer mistakes.
This will enable you to be more confident and work closer to your speed of thought. ✨
Using i to insert text at your cursor location is pretty good, but there are a few alternatives that are worth knowing about.
The two I use all the time are:
I use I a lot for commenting code. Try typing I// and then hit esc to return to command mode.
Oops, you forgot to add a semicolon? No probs, type A; and you’ve just appended a semicolon to the end of the line.
Tip: Generally after you’ve inserted some text, you want to return to command mode so that you’re ready to enter your next command. I’d recommend pressing esc any time you’ve finished typing.
Another two that go together:
Tip: The way to remember this is the capital O looks like a helium balloon that floats up. I’m not even kidding, I think about balloons every time I do this. 🎈
We all make mistakes sometimes. Undo and redo in vim work mostly as you’d expect, except the keyboard shortcuts aren’t what you’re used to.
Pro tip: I’ll often use vim’s autocomplete to avoid typos in the first place. Try it out by typing a few characters in insert mode and then press ctrl–n to get completion suggestions. It works by matching words in open files — it sounds basic but works better than you’d expect.
So maybe you’ve heard how advanced vim folks use h j k l instead of the arrow keys to move around — what the eff is that all about?? This is handy if you’re a touch typist since your hands are on the home row already.
Tip: If you’re always mixing up j and k, the way to remember it is that j kiiind of looks like a down arrow if you squint a bit.
If you’re like me and never learned to touch type properly, just use the arrow keys, it’s fine, don’t worry about it.
You’ll find that as your editing skills improve that there are much better ways of moving around a file anyway.
Here are a few movement commands I use a lot:
Phew! You know what? You’re doing great. I hope you’ve learned one or two useful things already, but don’t worry if you don’t remember everything.
Getting better at anything takes practice. You need to invest time and effort to learn a new skill and this is no different. I’d recommend that you focus on learning one thing at a time. Then later you can take what you’ve learned and turn it into a habit.
Alright, now we've covered the basics — let's continue with some intermediate stuff!
Let’s start with a few examples and I’ll explain on the way.
Say I want to delete a word. I move my cursor to the start of the word and press dw. It’s a bit like saying “(d)elete (w)ord”.
For this next example I’ll start with the text PastaController
with the cursor at the start. I can type the command dtC and now it’s just Controller
— wait, what just happened? If you look at that command closely, you can read dtC as “(d)elete up (t)o character (C)”.
What’s going on here is that we’re starting to compose commands. Pressing d is how we start deleting, but then we can combine it with the movement command tC.
Tip: Try deleting using some of the other movement commands we learned about earlier.
Often after deleting text the very next thing you want to do is to insert more text.
Instead of using d to delete, you can use the alternative c to change text. What this means is that after deleting the text, it’ll put you straight back into insert mode so you can start typing stuff in immediately.
A
(
Most of the text we edit every day is semi-structured. Most source code consists of words, quoted strings, html markup, parentheses and brackets of all sorts.
Vim has the concept of “text objects”, which allows you to take advantage of the structure in your text files to edit very precisely.
<
>
angle brackets… you get the idea hopefully!
Can you see how we’re building commands that read like little sentences?
Learning new things takes practice. Invest your time to learn new skills, and then turn what you’ve learned into a habit.
Thanks for reading! If you enjoyed this article or have any questions or comments, please feel free to let me know!
Thank you to all my kind and talented friends and colleagues at Culture Amp for their helpful feedback and advice in writing this article. 😊