3 of the Most Common Beginner Problems in Vim and How To Fix Them
Vim gets a bad rap because of its steep learning curve. Many give up after trying vim for a short time. Often, the argument people make is
Isn’t the editor’s job to get out of the way so you can write code? Who wants to remember all those shortcuts and commands?
I am not writing about why you should use vim. Plenty of people have written about it. Here’s a good article if you’re curious. Instead, this post focuses on fixes for the top 3 reasons why early users stop using Vim. More often than not it is frustration that they could’ve easily avoided had they known the basics.
So, Let’s dive in!
Reason #1: Slow Navigation
Ever tried to navigate a 500 line file with only hjkl or arrow keys? I have. It is frustrating. Frustrating enough that it will make you go back to the GUI editors with their smooth mouse scrolling.
You can quickly develop bad habits or what I like to call vim anti-patterns if you don’t pay attention. Using only j/k to go up and down a file one line at a time will make your vim experience frustrating. When your key repeat rate isn’t set sufficiently high — the problem is even worse. Each time you are moving one line, there is a key delay as you hold the j/k keys — making you slower and degrading your vim experience.
How to fix
1. Change the Key Repeat rate
Although I don’t recommend navigating only with hjkl, if you do ensure the key repeat rate is as high as it can be. In OSX you can change the repeat rate by going to System Preferences > Keyboard > Keyboard . Max out the “Key Repeat” and keep the “Delay Until Repeat” to short. This will help you especially if you want to navigate to locations that are close to the cursor.
2. Familiarize yourself with these
Familiarize yourself with these four commands and you will save yourself a ton of time:
Ctrl + f : Move down one page
Ctrl + b : Move up one page
G : Move cursor to bottom of the file
gg : Move cursor to top of the file
3. Consider navigation-by-search
Navigating around the file using search is easy in vim. To activate search press “/” followed by the keyword. You can also search for the word under the cursor by pressing “*”.
IMHO incremental search and case insensitive search are two of the most important settings in your .vimrc. Incremental search executes the search as you type. It is especially useful for jumping to method definitions.
" put these in your .vimrc
set incsearch
set ignorecase
Reason #2: The vimrc is a confusing mess
Ever copy pasted a thousand line .vimrc file you found on the web? I’ve been there. Not knowing what each line is doing gives you a feeling of powerlessness. Troubleshooting becomes monumentally hard when you don’t know what each line is supposed to do.
By copy pasting the .vimrc you are essentially delaying the learning curve not escaping it. Not having to bother what “filetype indent on” does in your .vimrc may seem nice at first. But it will eventually catch up on you and you’d wish you had learned it earlier. Saving you a lot of time and frustration.
How to fix
1. Don’t copy-paste your .vimrc
You should start from scratch and add things as the need arises. Let’s say you start with a blank .vimrc and you are doing Ruby development. You will instantly feel the need to convert tabs to 2 spaces. Look that up. Add it to your .vimrc. Repeat.
If you do look at someone else’s .vimrc only take what you need. I try to keep a lean vimrc file. I periodically go back and cleanup stuff I don’t use. The vimrc is not a place to be greedy. At times it will mess some other settings and you will spend hours trying to figure out what went wrong. If your pasted code seems to do different things depending on where it is pasted — you are most likely setting the same thing twice.
Remember, always add one at a time. Write comments for every line you add in the .vimrc. This will help when you are in troubleshooting mode.
2. Familiarize yourself with basic vim vocabulary terms:
One of the common problems we face in computer science and while writing code is knowing the right word to describe something. Vim has its own vocabulary and programming language called vimscript.
Going through a vim customization file can be daunting. You are likely to see keywords like vnoremap, map, inoremap, autocmd etc. Even if you don’t use all the features of vim it is good to know the basic keywords and what they mean.
In vimscript “mapping” simply means you are binding a key to another one. For example: the j key moves the cursor down one line. Now if you want to map the t key to move the cursor down then you will simply map t to j.
You should be able to interpret some of these basic commands:
:map means map recursively
:noremap means map non-recursively
:inoremap means map only in insert mode
:nnoremap means map only in normal mode
autocmd is generally used to run commands for a particular filetype
cnoreabbrev is generally used to alias/map commands in the vim command line
set is used to change value of a internal vim variable
let is usually used to change configuration variables for plugins
If you see a term you don’t understand look it up using :help. Vim help is pretty useful for looking up vimscript keywords.
Reason #3: Spending too much time looking up basic commands
How many times have you given up using a command because you couldn’t remember the exact syntax and ended up using an anti-pattern to solve it? I know I have a hundred/perhaps a thousand times. Finding what you’re looking for can take anywhere from 30 seconds to several minutes on the web. Not the best use of anyone’s time for the useless mental effort.
How to fix
1. When you learn something new, WRITE IT DOWN
The problem is not that there are too many commands to remember in Vim; the problem is that finding them is painful.
When you use a shortcut/command long enough, it will naturally get committed to your muscle memory. As time progresses some of them will fall off your memory. It is good idea to keep a list so you can go back to it and remind yourself or simply look up when you need it. The area under the long tail of commands you will use is significant enough that you need to have quick access to them when you need it.
If you learn a new shortcut that you want to commit to your memory, write it down. You are more likely to find it faster in your notes than going on a wild goose chase on your search engine.
2. Keep Your Notes Handy: Use a plugin (or write your own)
You should have a go-to place for vim commands that you commonly use. It could be something as simple as writing a cheatsheet in a text file. You should have access to your notes from within VIM so you don’t waste time context switching.
If you want to be adventurous, you can write your own little vimscript function that will invoke search over the notes file. You can then key bind a shortcut key to that function.
I use the grepg.vim plugin with grepg to access my vim cheatsheet. I already use grepg.vim to access my Ruby and Javascript cheatsheets from vim anyway. Adding a new topic for Vim is super simple. I don’t waste time context switching because I can easily copy/paste results from the scratch buffer(split window) it opens.
Conclusion
The learning curve to become productive in Vim doesn’t have to be so steep. By following some of the practices above and understanding a few key concepts, you can make your vim journey a lot smoother. If you want to become a power user, you will have to learn many key bindings. But relax, you don’t have to memorize all of them. Write them down and use a smart way to search them from Vim.
Happy Coding!
Footnotes:
* Some vim users will recommend using :help for Reason #3. It is no secret that vim has great documentation. It is pretty thorough. But I find it hard to navigate. This is my 5th year using vim and I use the :help interface rarely.
* If you are looking for a pre-written cheatsheet. Here are some good vim cheatsheets: http://jrmiii.com/attachments/Vim.pdf , http://sheet.shiar.nl/
Thanks to Kenan Shifflett and David Resnick for reading drafts of this.
P.S: I’m the founder of GrepPage. Shoot me any questions / concerns at yash AT greppage DOT com or tweet to me @evidanary