I use Vim because it’s faster for me than other text editors. Sure, you could use vim-mode for Atom or one of the various packages for Sublime, but they all have shortcomings — and Vim works everywhere. Here are five things I do to make Vim faster for me.
Relative Line Numbers
Most people know that you can use numbers with Vim commands to make a change multiple times, across multiple lines, or multiple characters. That’s not super useful if you have to stop to count the number of lines in a block of code before deleting it, for example. I use relative line numbers in Vim. The current line is always 0. I can instantly see that a block of code is 14 lines long and d14d
to delete it.
https://gist.github.com/joshbetz/271f612479de27f6d958
Use .
to repeat the last command
This one is pretty self explanatory. Any command can be repeated with .
. If you delete a line with dd
and realize that you also want to delete a few more lines, pressing .
three times will delete the next three lines. It may seem like a small thing, but that’s half as many keystrokes.
Use =
to format code
Often, when you copy and paste code from somewhere else, it won’t be formatted properly. The =
command can help here. You can either do something like gg=G
to format the entire document or highlight the block of code that needs to be formatted (in visual mode) and press =
to format just that block.
Make shifts keep selection
When you highlight a block of code for the purpose of indenting it, the selection is lost when you do the first shift. Often times, you need to shift it more than one position though. The following snippet keeps the current selection in visual mode after you perform a shift.
https://gist.github.com/joshbetz/7f9fca420fae26d04794
Search to find what you’re looking for
I think one of the reasons I’m slower in other text editors is that I use the mouse to browse for specific code instead of using the search function. Start searching with /
. After you enter the search and press return, you can jump the next and previous matches with n
and N
respectively.
With regard to making shifts keep the selection, I personally prefer to utilize the ‘.’ command to shift a block of code multiple times. These are definitely some good tips though. I didn’t even know that vim had relative line numbers.
Oh, awesome! https://github.com/joshbetz/dotfiles/commit/64ff0dd10792e8678c350566baa91acc9be30c23
Re shifts, you can apply you can . repeat as you mention – so you don’t need the selection really. At least I don’t miss it.
The relativenumbers snippet did not work for me – I see no change when switching to Insert mode and still see relative numbers.
I did discover that
:set numbers
:set relativenumbers
Makes all lines show relative, except the current one – for which the real line number is shown.