7 versatile Vim commands that are easy to memorize
Table of Contents
- 1. The dot command
- 2. Mark a location within a file
- 3. Word autocompletion
- 4. Simple arithmetic
- 5. Jump to the command-line without exiting Vim
- 6. Using the buffer list
- 7. Splitting windows
If you're like me, you often re-Google Vim commands on the fly at the moment you realize you need them. For productivity's sake, it is worth practicing a few useful commands to turn them into habit. In this article, we'll discuss 7 versatile Vim commands that are easy to understand, use, and commit to memory.
1. The dot command
The dot command is a simple and powerful tool in Vim. It allows repeating the last change by pressing the
. (period) key in Normal mode. This command is so versatile because "the last change" can mean many, many things, such as:
- Deleting one or more characters, words, or lines in Normal mode using
ddor variations thereof.
- Changing text indentation level using commands like
<<, or variations thereof.
- Adding text in insert mode (between pressing
ito get into Insert mode and
ESCto exit insert mode).
In general, executing any command in Normal mode that changes the text in your active file (or buffer as it is called in Vim), can be replayed using the dot command.
When repeating changes made in Insert mode, the change will be repeated using the same command that was used to enter Insert mode. For example, if Insert mode was entered using the
i command, the text will be placed before the cursor when repeated using the dot command. If Insert mode was entered using the
a command, it will be placed after the cursor when repeated using the dot command. Similarly, text inserted using the
A commands gets repeated at the beginning and end of the line by the dot command, respectively.
Since motions (commands that move the cursor around in Vim) don't change anything, they cannot be replayed using the dot command. However, like most commands in Vim, the dot command can be prefixed with a number which tells Vim to execute it that many times. For example typing
5. will repeat the last change 5 times.
Less memorizable BONUS: Type
@: in Normal mode to repeat the last Command mode command (i.e. the kind where you type a colon which brings the cursor to the bottom of the screen to enter something like
:some-command). This one isn't as easy to memorize, but hey, at least you know it exists now.
2. Mark a location within a file
Next, we'll discuss a convenient feature that allows us to mark the current location in a file so that we can easily jump back to it later in the editing session. This is especially useful in long code files in which we need to frequently jump back and forth to make edits.
Marks are set in Normal mode. We can set a mark on the exact, current location of the cursor by typing
m ("m" for "mark"!) followed by any lowercase letter. Therefore, we have 26 possible marks we can set and use at one time within each file.
We can jump back to that exact location by, (in Normal mode), typing a back-tick
` followed by the lowercase letter used to create the mark. To help this one sink in, remember that the "back"-tick takes you "back" to the mark specified after it.
For example, if we create a mark using the command
mb, we can jump to that location at any time using the command
Double-tapping the back-tick key
`` will jump back to the location before the last jump. This is nice if you jump to a mark accidentally and want to get back to where you were working.
One last cool aspect of marks is that they persist on a per-file basis when you exit Vim with or without saving the buffer.
3. Word autocompletion
Another easy productivity boost can be obtained via Vim's built-in autocompletion feature. We will discuss the simplest version of this feature, which allows you to partially type a word (like a variable or function name), and tell Vim to pop up a list of autocompletion suggestions for you to choose from.
Vim's autocompletion list is triggered directly from Insert mode, since autocompletion shouldn't interrupt the flow of our writing too much. In Insert mode, start typing a word and then press
<CTRL-n>. This will display a little pop-up list containing a list of words that match the part of the word we typed so far:
This word list is generated only from existing words in the current file, so it is great for autocompleting things like long variable and function names.
You can toggle down the list of words by pressing
<CTRL-n> to highlight the desired word, and then simply keep typing to continue your sentence without delay.
Fun fact - you can use
<CTRL-p> to toggle backward through the word list, but that's an extra thing to remember which defeats the purpose of this article, so pretend I never said anything.
Less memorizable BONUS: Instead of sourcing word lists for autocompletion from the current file, we can source them from Vim's built-in dictionary. To do this, first enable Vim's spellchecker by running
:set spell. You might see some text highlighting - ignore it. Now when you're ready to autocomplete a word in Insert mode, instead of typing
<CTRL-x><CTRL-k>. This will source the autocompletion list from Vim's dictionary and present you with many more options, which can be good on the off chance you're trying to write a real word.
4. Simple arithmetic
This one will be short and sweet. If Vim's cursor is positioned on top of a number in an open file, we can press
<CTRL-a> ("a" for "add"!) to increment that number by 1 and
<CTRL-x> to decrement that number by 1.
Typing a number before either of these commands will run it multiple times. For example, typing
5<CTRL-x> will subtract 5 from that number.
If the cursor is not positioned directly on top of a number, the
<CTRL-x> commands will jump forward to the next occurrence of a number in the file and increment or decrement it accordingly.
Flashback to (1): The dot command can be used to repeat these addition and subtraction operations.
5. Jump to the command-line without exiting Vim
We can run any command from the command-line without exiting Vim. From Normal mode we can type a colon to enter Command mode followed by an exclamation point, followed by our terminal command. From Normal mode, we would type something like
:!ls. This would display the command-line (keeping our Vim buffer open in the background), and list the contents of the current directory. As soon as we press a key, we are returned to our Vim file so we can continue working where we left off. Any command-line operation can be executed in this way.
6. Using the buffer list
Although most of us probably work on one file at a time in Vim, we can boost productivity by learning to make use of Vim's multi-file capabilities.
Previously, we mentioned the term buffer. When we open a file with Vim, we aren't working directly on the file itself. Instead we're working on an in-memory representation of that file, called a buffer. Most commands operate directly on the buffer, which gets written back to the file when we save the buffer.
Vim allows us to open multiple buffers in the same session. Try opening two files at once like this
vim file1.ext file2.ext.
Vim will create and open a buffer for the first file listed, but in the background, a buffer for the second file is also created. You can prove this to yourself by running the
:ls command in Vim which will list the open buffers. You can open as many files as you want this way, specifying wildcards like
vim *.java if your heart desires it.
Use the commands
:bnext ("buffer-next") and
:bprev ("buffer-previous") to toggle forwards and backwards between your open buffers. You can treat each buffer as a regular old Vim session.
7. Splitting windows
Splitting windows is a step beyond using multiple buffers, because it allows you to see the contents of multiple files at the same time. Think of splitting windows as a frontend for viewing and working with multiple buffers at once.
If you're like me, you've probably split windows in Vim in the past, few and far between. And you probably always forget how to do it and are too lazy to look it up in the moment. Consider this an attempt to hammer it into your head - it's pretty dang simple:
When you have a file open in Normal mode, use the command
:split file.extto create a horizontal split in the current window, displaying both the old and new files one above the other.
Use the command
:vsplit file.extto create a vertical split in the window, displaying both the old and new files side by side.
In Normal mode, press
<CTRL-w>w(that means just press CTRL and tap 'w' twice) to toggle the active window.
:wqcommand to close out of the active window, which will allow the other window to take up the full screen.
Fun fact - windows can be split and re-split multiple times - knock yourself out!
Less memorizable BONUS: To lock scrolling for split windows so they scroll together, run the command
:set scrollbind in each of them.
Vim's productivity is diminished if we have to constantly consult Google while working. We presented 7 Vim commands that are easy to commit to memory and will pay dividends in time-saved down the line.
If you're interested in learning more Vim tips and tricks, check out Practical Vim: Edit Text at the Speed of Thought.
If you're interested in learning more about the basics of coding and software development, check out our Coding Essentials Guidebook for Developers, where we cover the essential languages, concepts, and tools that you'll need to become a professional developer.
Recommended product: Coding Essentials Guidebook for Developers