PrimitiveType

Quick Guide to the Vim Text Editor


I use Vim as my text editor of choice when editing files in Linux. For the novice, Vim can be quite intimidating, but it heralds so much power once you get used to it that it's worth the effort to learn. Here's my guide to help you along the way!

Launching Vim

From the terminal, type "vim filename", where filename is the file you want to edit. If the file does not exist, launching Vim with this command, and then saving the file from Vim, will create it. (It is possible to supply further files to open with vim, separated by spaces, but I usually just stick to one for simplicity.) When you are editing in Vim, a special backup file is created by Vim which is used to restore your work should the program abort abnormally. This is useful if, for example, an SSH session to a server crashes midway through an edit. When you next try to open the file with Vim you will be presented with a series of options on what to do, one of which allows you to recover your lost work.

Entering Text

When you first open Vim you are in standard mode (at least, that's what I'll call it for this article), and you are able to view the contents of the file but if you try editing it as you might with Notepad, you'll be surprised that nothing (or something unexpected) happens. To enter text you have to press "i" or the Insert key (I always use the latter), and then you can use the keyboard to enter text as expected. You'll see on the bottom left of the screen or window it says "insert". When you are done entering text, press escape and you will return to the previous, standard mode.

If you want to overwrite the characters as you type, press Insert twice so that "replace" is displayed at the bottom of the screen. In fact, pressing Insert again toggles between "insert" and "replace" modes.

Navigating Within a File

When in standard mode, you can move about the document with ease in a number of ways. The simplest way is with the arrow keys, which work as expected, moving across characters and lines one at a time (in the same way as in insert mode). You can use "w" to jump to the beginning of the next word, or "b" to jump to the beginning of the previous word. Pressing "3w" jumps forward 3 words and is like pressing "w" 3 times. You can prefix most of the movement commands in this way, so that pressing "15" followed by the down arrow key will move you down 15 lines.

To move to the start of a line, press "Home", and to move to the end of a line, press "End". To jump to the start of the document press "1G" and to jump to the last line press "G". To go to any particular line number, say 250, press "250G". If you want to see some information abut your document, including how many lines there are and how far down the document you are currently positioned, use "<ctrl>G".

Saving Files and Exiting Vim

To save your file, make sure you are not in text input mode by pressing escape, and type ":w" followed by enter. You can now carry on editing and save changes to your file in the same way, or exit Vim by typing ":q" followed by enter. To save a file and exit with one command, enter ":wq". If you don't want to save your changes and still want to exit Vim, use ":q!". The "!" overrides any warning and blocking Vim will otherwise give you about unsaved work.

Running and Aborting Commands

As we have seen above, commands to save and exit from Vim are issued by typing ":" followed by the command. There are many commands that are performed this way, and others that do not require you enter the "command line", as we'll see below. Whenever you have typed some of a command but decide not to run it, you can press Escape to get back to standard mode.

Deleting Text

In standard mode, to delete the current line of text type "dd" and press enter. This also copies the line internally in Vim, enabling you to put it back into the document by entering "p" (which places it on the line below the current cursor position). If you want to delete individual characters ahead of the cursor, you can eat them away by pressing the Delete button. However, for the Backspace button to work for deleting characters backwards, you must be in insert mode (Delete still works in this mode too).

You can also highlight an area of text to delete. This is done by entering visual mode from standard mode. To do this, go to the start of the area of text you want to delete, press v, then use the arrow keys to go to the end of the text area you want to delete, and finally press Delete.

Shifting text

If you are a programmer you will be familiar with the indentation of lines to enhance the readability of your code. To apply a constant indentation to a range of lines, first specify the amount by which you want to shift them, say two spaces, for instance, with this command: ":set shift-width=2". Then select the lines by pressing "v" and using the arrow keys, and press Shift + the left or right arrow key to shift them by two spaces in the direction of the arrow key pressed. Vim also supports automatic indentation, but I haven't used it much.

Undoing and Redoing Changes

To undo your last change, press "u" in standard mode. Similarly, to redo an undone change, use "R". Both support multiple levels, so you can press them repeatedly to get to the desired change. Note that when you quit Vim, your undo history is wiped out.

Searching

To search for a string of characters, in standard mode press "/" followed by the string and press enter. What happens next depends on how Vim is configured. On some configurations, all instances of the search terms are highlighted and the cursor jumps to the first found term. On other configurations, as you type the next match is highlighted and if you press enter your cursor is positioned at the search term. "/" searches forward in the document. To search backwards in the same way, use "?". If no matches are found, Vim displays an error message informing you so.

Vim's search is case-sensitive by default, so to carry out a case-insensitive search, include "\c" somewhere in the search term (I always put it at the end to make it easier to read), as so: "/look for this\c"

Searching and Replacing

Replacing is done with the substitute command, denoted by "s". To search and replace a string with another string at every occurrence in the document, use this command: ":%s/string1/string2/g". The "%" specifies the range of the operation, in this case the whole document, and the "g" tells the command to replace all occurrences on one line and not just the first. Omitting the "%" performs the operation on just the current line. You can also specify a range of lines to perform the operation on, though I find it easiest to highlight an area with "v" and then enter ":s/string1/string2/g" (note that the command line will display extra characters that mean the operation will take place between the highlighted lines, so the command will appear as ":'<,'>s/string1/string2/g").

It can be useful to use regular expressions to perform more advanced search and replace operations. For example, I sometimes use the following to comment out several highlighted lines in an Apache configuration file (as there is no multi-line comment available, and commented lines start with a "#"): ":s/^/#/g". This is an extremely simple regular expression replacement, where the only special character is the "^", which indicates the start of the line. However, looking at more complicated regular expressions is beyond the scope of this article.