Using vim effectively takes time.
The good news is that using vim efficiently is an exercise in muscle memory. With a little bit of practice, you'll get much better at using vim. At some point, you stop thinking about how to do something and your fingers just do it.
You spend more time thinking about what to do instead of how to do it.
I've been using vim for a few years and it's easily the best investment I've made in my development career. Not only is vim more efficient, but honestly it's just fun. At this point, I fly through my editor like a maniac making changes like those hackers you see on TV.
While the best way to get better at vim is to practice, there are a few things you need to know to be even more productive.
Vim is a modal editor
A modal editor is a text editor that offers multiple interaction modes.
In other words, vim has different modes that solve different problems. For example, you'll use one mode to move around a file and another mode to edit the file or copy text. Understanding how these modes work will help you take advantage of what vim has to offer.
In this post, we're going to take a look at the four most common modes you'll encounter.
By default, this is the mode you're put in when you start vim.
You'll spend most of your time in this mode. As developers, we tend to spend most of our time editing existing code, not writing new code. Normal mode optimizes for this use case.
It's used to:
- Navigate around a file with the keyboard
- Quickly replace or delete single characters in your text
- Undo changes (
ukey) or redo changes (
Ctrl + rkey)
You should initially spend most of your time getting comfortable with this mode. Effective navigation is the first step to being a more effective vim user.
This mode is similar to Normal mode.
Like normal mode, you can navigate around your file the same way. The key difference is that vim highlights the text under your cursor as you move around. After you're done navigating, you can execute another command on the highlighted text. For example, you can copy the text or delete it.
To enter Visual mode, press the
v key while in Normal mode.
Visual mode makes it easy to change large areas of code.
This is the second most used mode after Normal mode.
This mode should seem familiar. The reason is that it resembles the experience of other editors. It's used to insert text into a file.
In vim, you usually enter Insert mode for short bursts. You edit some text and then switch back to Normal mode to navigate around the file again before the next action.
The most common way to get into Insert mode is to press the
i key while in Normal mode.
Command Line Mode
The Command Line (also known as Cmdline) mode lets you execute a single command.
Here are a few things you can do with Command Line mode:
- Execute a command as you would in a terminal
- Run another program and process the results
- Use a regex to replace text in your file
- Search for text in the current file
I use this mode all the time. Sometimes I use it to create files or folders without leaving vim with a
mkdir command. I also rely on the search and the text replacement via regex functionality quite a bit.
There are three other modes
So we talked about four of the most common modes, but there are three more basic modes in vim. I opted not to talk about them because I rarely use them myself, but here's a list of the other modes and where to get more information about them:
- Ex mode
- Select mode
- Terminal Job mode
Here are the official vim docs on the different modes.
How do you switch back to Normal mode?
Since Normal mode is where you’ll spend most of your time, you’re going to want to know how to get back to it.
If you’re unsure what mode you’re currently in, you can press
Esc twice to go back to Normal mode. The only time that doesn’t work is for Ex Mode. In that case, you need to enter
:visual to get back to Normal mode.
Getting comfortable with using vim takes time.
A lot of the growing pains are a result of not having muscle memory built up. It’s like typing. You need to practice the movements to get better. That said, there are a few things that are important to know about how vim works.
Understanding the most frequently used modes in vim is one of those things.
The sooner you get used to switching between modes, the sooner you’ll be productive with vim.