This is the approved revision of this page, as well as being the most recent.
So you thought you could get by with gedit? Not when you must configure an application on your server in a data center two states away! When it comes to remote system administration, all roads lead back to Vim. Not a Vim expert? No problem, you just need a handful of commands to do everything you need to do.
Entire books have been written about Vim, and they still fail to capture all of its functionality. But the core command set is all you really need if you're just making a few changes to a few files over SSH. You might not do it as efficiently as a Vim expert, but it's good enough to get the job done. In fact, if you're working with Vim (the pre-eminent vi-clone) on a server, there's a good chance it's "vim-tiny," a stripped-down Vim that offers the traditional vi functionality but not the full set of Vim features.
The first thing you must grok is that Vim has several modes -- command mode, insert mode, and last-line mode (also known as ex mode). When you start Vim, you'll be in command mode. Here, all of the keys are used to perform commands, not input text. To switch to input mode, hit i, and you're able start editing the file, adding text, using Backspace, etc. To return to command mode, hit Esc. To enter last-line or Ex mode, use :, and then input the command you wish to enter.
Vim Command Mode
I could go on for days about the commands needed in command mode, but we're just here for the basics, so let's look at movement. Movement is based on the standard alphabetic keys:
- h Move the cursor to the left one character.
- l Move the cursor to the right one character.
- j Move the cursor down one line.
- k Move the cursor up one line.
You can move faster by using b and w to move backward and forward by one "word" at a time, respectively. Vim looks at "words" as a string of alphanumeric characters. So "eix" is a word, but "eix-sync" is multi-word because it's broken up by a non-alphanumeric character.
Deleting is done with d or x. To delete a single character, move the cursor over that character and use x. Using dw will delete the word the cursor is over, and db will delete the previous word.
To delete an entire line, use dd. To delete from the cursor to the end of the line, use d$. To delete from the cursor to the beginning of the line, use d0. The $ is shorthand for "end of the line," and 0 is shorthand for beginning of the line. You can also use ^ (shorthand for "first non-blank character of the line").
Copying and Pasting in Vim
Let's look at copying and pasting real quick. To highlight text to copy, use the v, V and Ctrl-V commands. You might have guessed by now that Vim commands are case-sensitive, So v and V are different things.
The v command simply allows you to highlight changes character by character using the movement (hljk and others) or arrow keys. The v command highlights entire lines. And the Ctrl-v command highlights blocks of text -- very useful for highlighting and copying columns of text.
Once you've highlighted the text you want to copy, hit y to "yank" the text into the buffer.
To paste the text, use p or P to paste. The p command will paste after the cursor, and P pastes before the cursor.
Search and Replace
To search through the document, use the / key to initiate a forward search, or ? to initiate a backward search.
To search and replace, use :s with the range of lines and search terms. Like so:
The % means "global," but you can replace that with a range of lines, like this:
Another example of search/replace functions
Command Outcome :s/xxx/yyy/ Replace xxx with yyy at the first occurence :s/xxx/yyy/g Replace xxx with yyy first occurrence, global (whole sentence) :s/xxx/yyy/gc Replace xxx with yyy global with confirm :%s/xxx/yyy/g Replace xxx with yyy global in the whole file
I prefer to use the c (confirm) and g (global) options too, so when searching it will search the entire line and not just the first occurrence of a string.
A usual search would look something like :%s/old/new/gc, and when you hit Enter you'll be prompted before you make changes. I recommend using confirm; otherwise you can wind up with unexpected results.
What if you've made an edit you didn't want to make? Easy, use the undo u command. If you didn't mean to undo what you did -- and it's easy to accidentally hit u -- use Ctrl-r to redo the last change. Quick note: If Vim is in vi-compatibility mode, it will have only one "level" of undo. In normal Vim mode, you can undo many, many changes. But vi undoes only the most recent change.
Saving, Quitting and More ...
One of the things that's severely non-obvious while working with Vim the first time is how do I get the heck out of here? You can quit Vim in a number of ways, but I'll show the most usual ones.
First, if you want to save your changes before exiting Vim, use :w or save and exit in one action with :wq. Don't want to save your changes? It happens. No problem, just use :q! if you realize that you've made some edits that you don't want to save, and they are too complex to easily undo before exiting. Note that you can also write changes to a different filename by using :w newfile.
This is just a short and sweet intro to Vim for emergencies or minimal usage. You could do much, much more with Vim if you wanted. Be sure to read through the Vim tutorial by running vimtutor, and look through Vim's documentation by running :help. But in a pinch, this list of commands should get you through.
Good Starter Vimrc
The following file makes a very good starter .vimrc file. Place it in your home directory, and you will get true tabs (displayed indented 4 spaces) and you will be able to see all the whitespace in your document, which is handy when editing critical files:
By default, copying and pasting into a vim window will cause things to horribly auto-indent. To fix this, add the following to your .vimrc file:
Alternatively, you can temporarily switch to "paste mode" before pasting with :set paste. You will see -- INSERT (paste) -- in the status bar when you are in insert mode. Don't forget to disable paste mode with :set nopaste after pasting. You can also use bang to switch between "paste" and "nopaste" so that you just have to recall the last command : :set paste!. (See also)