Want to know Vim just well enough to make quick edits to source code and configuration files on Linux servers? Here's what you need to know.
WTF is Vim?
Vim is a text editor and IDE that's been around a long time. Its feature set is legendary, but this post just covers the bare minimum you need to know for simple editing tasks. If you're interested in the details of Vim's rich history, check out Two-Bit History's post Where Vim Came From.
You'll find Vim (or its closely related ancestor vi) on pretty much every Linux or Unix server you'll ever connect to with SSH or remote desktop. And that's the beauty of it – learn a little Vim and you can be instantly productive whenever you connect to a server, with no need to install any extra software.
What about Emacs? I have no opinion on the longstanding editor war between vi/Vim and Emacs. I've chosen to learn Vim rather than Emacs myself for two reasons: all Unix and Linux systems come with vi and/or Vim installed, and I seem to know more Vim experts than Emacs experts. But both will run on any popular OS of the last 40 years, and Emacs is a great choice as well. Whatever floats your boat.
What you need to know
The rest of this post covers the details of getting started with Vim. These sections cover the basic concepts:
If your editing requirements are very simple, those basics may be all you need. If you find yourself moving lines of text around or making repetitive changes, however, these two additional sections will probably be helpful:
Vim has over a dozen different modes, with different sets of commands available in each mode, and the same keystroke or command usually does different things in different modes. For our minimalist approach, we're only going to use two modes: command mode (also known as Normal mode) and insert mode.
When you first launch Vim, it's in command mode, so the keystrokes you type will be interpreted as commands, rather than being inserted into the document as text. If you start start typing text, before long you'll probably press one of the commands that switch to insert mode, with subsequent keystrokes being inserted into the document as text.
For example, if you fire up Vim and then type The quick brown fox jumped over the lazy dog, the letter i will switch into insert mode, and you'll see this on the screen:
Now you're in insert mode, and to return to command mode you'll need to press the Esc key. There are a few situations where you need to press Esc twice to get back to command mode, so I always press it twice just to be sure.
So here's everything we need to know about modes:
- Vim is in command mode when it starts up
- From command mode, press i to switch to insert mode
- From insert mode, press Esc twice to switch to command mode
Much has been made of the alleged difficulty of exiting Vim, and "How to exit the Vim editor" is the most popular question in the history of Stack Overflow. Now that you understand a bit about Vim's modes, you can see where the confusion comes from: people get into insert mode (often by just thrashing around and pressing random keys), and then their attempts at various commands are just inserting text into the document.
In actual practice, exiting Vim is simple and straightforward if you're in command mode. To exit Vim, make sure you're in command mode (pressing Esc a couple of times will do the trick), and then type one of these commands and press Enter:
|quit - if there are unsaved changes, will show an "no write since last change" error
|write and quit - saves changes (:x is a shortcut for this command)
|quit and discard any unsaved changes
There are some other variations, such as closing all buffers at once instead of one at a time, but we're being minimalists so we don't care about all those details.
Moving the cursor
In command mode, you can move the cursor to any position in the document with the arrow keys. Note that each line is treated as a discrete region that you can only move right or left within – the right and left arrow keys won't wrap around to lines before and after the current one, you need to use the up/down arrows for that.
It can be tedious to move around with just the arrow keys, so here are a few more cursor navigation commands that you may find worth memorizing:
|page up (your PgUp key may also work for this on some systems)
|page down (your PgDn key may also work for this on some systems)
|go to beginning of file ([[ also works)
|go to end of file (]] also works)
|go to beginning of current line
|go to end of current line
Vim has a many more cursor navigation commands, but those are sufficient for basic editing tasks.
NOTE: you can also use the h/j/k/l keys to navigate in command mode – h = left, j = down, k = up, l = right. These may be needed if your keyboard doesn't have arrow keys or they aren't mapped correctly for the system you've connected to.
Once you've positioned the cursor where you want it, press i to enter insert mode and begin typing. Characters will be inserted at the current cursor position, and you can also move the cursor with the arrow keys to make changes in other locations in the file.
Vim has numerous ways to enter insert mode, some of which are very clever and useful in common source code editing tasks, but we're going to keep things simple here. The only other command that may be useful in a minimalist approach is the o command (in command mode), which inserts a blank line immediately after the current line and puts the cursor at the beginning of that blank line in insert mode.
You can delete text using the same techniques we've already learned: just move the cursor to where you want to delete some text, then press i to switch into insert mode and use the Delete key. You can also move around with the arrow keys while in insert mode, to delete text in other places.
That one-character-at-a-time approach can be slow or clumsy in some situations, so here are a few commands (for use in command mode) that may be helpful:
|delete character (like the Del key in insert mode)
|delete character to the left (like Backspace on some systems)
|join current line to next line (deletes the newline character)
|delete current line
|delete current word
The dd and dw commands can be prefixed with a number of repetitions. For example 4dd deletes 4 lines starting with the current line, and 3dw deletes 3 words starting with the current word.
To undo the last change you've made, press u in command mode. The definition of last change can get a bit complicated, but in general it works intuitively. If you're in insert mode and type a change, then move to another place in the file with the arrow keys and type another change, those will be two separate changes in the undo stack.
The ctrl-r command will redo a change that has been undone.
Copy & paste
If you need to move a block of text from one position in a file to a different position, these commands will be useful:
|yy or Nyy
|copy current line(s) to clipboard
|yw or Nyw
|copy current words(s) to clipboard
|paste clipboard contents after cursor
|paste clipboard contents before cursor
Vim uses its own clipboard for copy and paste operations. If you want to copy and paste between Vim and other applications, you need to prefix any of the above commands with "* to indicate that the OS clipboard should be used. For example, use the command "*p to paste the current contents of the OS clipboard into your document after the current cursor position in Vim.
Search & replace
To search forward for a string xxx, use the command /xxx, and to search backward use the command ?xxx. Note that these searches wrap around the end/beginning of the file. To make a search case insensitive, add \c to them.
To repeat the last search in the same direction press n, and to repeat the last search in the opposite direction press N.
For search and replace, use the command :%s/xxx/yyy/g to search for all occurrences of xxx and replace them with yyy. Here are a few things to know about search and replace in Vim:
- The presence of % means to search the entire file; if this character is omitted only the first occurence of the search string will be matched
- The /g option means to match all occurrences on each line; if this is omitted, only the first occurrence on each line will be matched
- You can add /c to have Vim prompt for confirmation of each change
- You can add /i to make the search case insensitive
Note that \c indicates a case insensitive search, but /i indicates a case insensitive search and replace.
If you only use Vim sparingly, the above commands are likely all you'll ever need to know. And if you want to know more, the built-in help documentation is exhaustive and well-organized, exactly as you'd expect from a project that refuses to succumb to death by underdocumentation.
The :help command opens the documentation, and you'll find information there about how to search and navigate it. Keep in mind that the documentation is just yet another file that's open in Vim, so all of the cursor navigation and searching commands you've already learned will work there. Use the :q command to exit the help screen.
There are also many great learning resources online (here's a good overview of a few of them), and when you're ready to start customizing your Vim setup, GitHub has over 200,000 examples of .vimrc configuration files that you can study or borrow from.