11 Vim tips that will save you hours of editing time

Want to take your Vim game to the next level? Since using Vim, I have learned many tips and tricks that have saved me a lot of time and headaches when editing with Vim. I share some of my best tips in this guide so you can incorporate them into your workflow.
Move the cursor quickly
Vim is designed for efficient navigation without relying on the mouse or arrow keys. Knowing Vim’s cursor movement commands can save you a lot of time and common sense when dealing with large files. Vim offers several ways to quickly move the cursor, from moving by characters, words, and paragraphs to jumping to specific locations.
Besides the arrow keys, you can use H (left), J (down), K (up), and L (right) to move by characters. You can also add a number before the character to skip that number of characters. So, pressing 5j will move the cursor five lines. To cover greater distances, you have:
-
w: Move to the beginning of the next word.
-
W: Move to the beginning of the next word after spaces
-
b: Jump to the beginning of the previous word.
-
B: Jump to the beginning of the previous word before spaces.
-
e: Go to the end of the current word.
-
E: Go to the end of the word before spaces.
-
0: Go to start of line
-
$: Go to end of line
Also, if you want to go to a specific line, enter that line number and then use G to go to this line. So if you want to go to line 500, type 500G and press to access this line.
Convert tabs to spaces (and spaces to tabs)
A common problem that developers and content creators face is inconsistent indentation. Some teams or projects may prefer tabs for indentation, while others prefer spaces. In Vim, you can convert tabs to spaces and vice versa, depending on your project’s specific requirements.
If you want to convert all spaces back to tabs in an existing file, run these two commands:
:set noexpandtab
:retab!
The first command ensures that tabs will be used instead of spaces. The second requires the reconversion of spaces to tab characters based on the tabstop parameter.
If you prefer spaces and want to convert tabs in an existing file, run:
:set expandtab
:set tabstop=4
:set shiftwidth=4
:retab
These commands tell Vim the details of the indentation block when converting tabs to spaces.
Indent multiple lines
Indentation is a good practice for keeping your file clean and organized. If you’re working with a language like Python, correct indentation becomes more crucial. With a single command, you can indent all lines in a file. First, type gg to jump to the first line of the file (this might just be another helpful tip.) Then type =G to indent the lines. The final command is therefore:
gg=G
Highlight spelling mistakes
I make a lot of typos and mistakes when I type quickly. In modern word processors like Microsoft Word and Google Docs, you have a spell checker that suggests the correctly spelled word. Vim also has a built-in spell checker. It will highlight words that it thinks have been misspelled. To enable spell check, use this command:
:set spell
Change the direction of visual selection
When working in visual mode, you can select text by character, line, or block. Once you start selecting text in any direction, you may realize that you need to extend or change the selection in the opposite direction. Instead of exiting visual mode and starting the selection again, you can change the direction of the selection by pressing the o key.
Go to the corresponding support
When working on a large code base, you often need to find the end of a block of code. Unless the code is well formatted and indented, it’s difficult. In Vim, you can easily find the start or end of a block of code by jumping to the corresponding hook. Pressing the percent sign (%) does just that. If you are on the opening bracket and press %, you will move to the ending bracket and vice versa.
This works for parentheses, braces and square brackets.
Show line numbers
This one is pretty simple. By default, Vim does not display any line numbers, only tilde signs. You can enable line numbers on Vim in different modes. In absolute line number mode, you see line numbers from the first line to the last line. To configure this, run this command:
:set number
In relative number mode, the line your cursor is on is the zero line. The rest of the lines are numbered relative to the line your cursor is on. So the lines above and below your cursor start at one and continue. To enable relative numbering, run:
:set relativenumber
What if you need both? There is another mode called hybrid mode, in which you can use both. By enabling hybrid mode, the line your cursor is on displays its absolute line number. But the lines above and below show their relative numbers. Enable this mode by running:
:set number relativenumber
Normal mode
The :norm command in Vim allows you to run commands in normal mode from command line mode. This is useful for repetitive tasks and batch editing. When you need to apply the same normal mode commands to multiple lines simultaneously, you can do so by writing a single line instead of repeating the same thing for each line.
For example, I want to add a semicolon (;) at the end of a few lines. First, I need to select all rows. Then run these commands:
:norm
A;
Once I press Enter, all selected lines will have a semicolon at the end.
Likewise, you can insert text, change case, or indent multiple lines at once.
Delete text in insert mode
In traditional text editors, there are many keyboard shortcuts for deleting characters and words. Vim has its own set of shortcuts for quickly deleting chunks of text. You can delete characters, words and entire lines. You can delete either from the backward or forward direction of the cursor. Here are some useful shortcuts:
-
Ctrl+H: deletes a character backwards (same as backspace).
-
Ctrl+W: deletes a word backwards.
-
Ctrl+U: Deletes everything from the cursor to the start of the line.
-
Ctrl+K: Deletes everything from the cursor to the end of the line.
Dynamic snippets
Snippets are like predefined pieces of text that you can quickly insert to speed up your workflow. Instead of repeatedly typing the same code or boilerplate text, you can use snippets to quickly expand a longer block of text, saving time and reducing typos. Dynamic snippets take this a step further by allowing snippets to include dynamic content, such as the current time, date, or other variable information.
Using plugins like UltiSnips or vim-snippets, you can define dynamic snippets that adjust based on the current context. For example, let’s create a snippet to expand “jd” to John Doe. If you are using UltiSnips, add the following to your UltiSnips file (usually ~/.vim/UltiSnips/snippet_file_name.snippets 🙂
snippet jd
John Doe
endsnippet
For dynamic snippets like time and date, you can format it like this:
snippet time
`!v strftime("%H:%M:%S")`
endsnippet
Now, if you type “time” and press the Tab button, the current time should be inserted automatically.
Autocomplete words
Vim has a built-in word completion feature. This can be useful when writing something where a word comes up often (like variables in a program). To autocomplete a word, start typing the word and press Ctrl+N. You can also type Ctrl+P. The difference is that the latter searches the list from the bottom (the last word) while the former searches from the top.
With these new tips in your knowledge, you should be able to use Vim with more confidence. We also have other guides that show you how to exit Vim and set a Vim profile.


