Vim/Guide

vim is a powerful, extendable, versatile, text editor. It has a long history and wide usage on many platforms. This tutorial will vi-like text editors in general, and vim in particular.]]

Following this guide, it should be possible to become a proficient vi user, with just a short time commitment. It will show how to move around, edit text, use insert mode, copy and paste, and use important vim extensions like visual mode and multi-window editing. It is aimed at anyone not yet comfortable using vi, to get up to speed with one of the most popular and powerful text editors. The investment in learning such a tool should be rewarding for years to come, if spending any real amount of time editing text files.

This tutorial is designed to be followed sequentially, but individual sections may be used for reference.

vi-like editors and Vim
was written in 1977 by computer engineer Bill Joy, at Berkeley (of BSD fame). vi was not created in a vacuum, and to cut a long story short, it is a descendant of the ed line editor, but is able to edit a whole screen of text.

There are many versions of vi, all more or less similar to the original version. This guide focuses on, a popular version of vi, but most of the information will apply to other vi-like editors. Any Vim-specific commands will be indicated as such.

There are installation instructions for Gentoo on the wiki, but Vim can be downloaded from www.vim.org for users on other OSs, if it is not already installed.

In addition to an enhanced command-line editor, vim also comes with Gvim, a graphical editor which can be configured to use the GTK+ library. Gvim can make things a bit easier for some beginners, who would rather avoid the command line - but knowing how to use a shell is also a valuable skill.

Opening a file
Launch Vim with the command. If only Vim is installed, the command should launch Vim (see the text editor article for details of how this is set up in Gentoo).

To open a text file in Vim, simply pass it the name of the file on the command line.

Using a name for a non existent file will create a new file with that name on saving.

To use gvim, type.

After vi loads, the contents of the text file will be shown, or just the beginning if it is too long to fit.

Quitting
Surely this shouldn't be the first thing to learn ? Well, because vi isn't self discoverable (this does not mean that it is not ergonomic or easy, just that it must be learned), how to quit can be a very useful thing to know starting out. If anything is to be retained from this tutorial, this is the sort of knowledge that can help around the office, to liberate an overly curious colleague from an incomprehensible black and white screen :).

From the command mode, type  then press  (Return) to quit.

If the file was modified in some way, vi will refuse to exit. To quit while throwing away any changes, type  then press the  (Return) key.

See I'm trapped in vim! for more details.

Introducing modes
Unlike many editors, when vi starts up, it is in a special mode called command mode. In this mode, pressing a letter key will not insert that letter into the document, but will execute a command associated with that key. The current mode will be indicated at the bottom left of the screen (or it will be blank for command mode).

Command mode is one of the things that makes vi such an efficient editor, that still has appeal over forty years after it's invention. This mode is used for navigating around files, and for high level editing operations, such as copying, pasting, or deleting whole sections of text; for searching; for recording macros etc.

If one thing has made vi so popular, it is that command mode is like an efficient document editing language, with functions represented by single characters, providing huge power to the user, right under the fingertips.

Certain commands will enter different modes, sometimes while simultaneously performing an action. The main modes are the all important command mode, the insert mode for entering text, visual mode for text selection, and what is called ex-mode, which is a little like a command line.

Don't worry about understanding what all these are for, yet. How to enter or exit the appropriate modes will be indicated in the tutorial, when needed. If the wrong mode is entered by accident, press several times to return to command mode.

Introducing the ex-mode
Pressing while in the command mode will open the ex-mode, which is a little like a command line, in some ways. As just seen, quitting using, uses the ex-mode.

It is called ex-mode because vi has a built-in non-visual editor called ex. This is a descendant of the old Unix ed command, which is at the very origins of vi.

This mode has specific commands, different to command mode, and allows composing of more complex commands, when needed.

ex-mode can be used similarly to to perform line-based editing operations. In some configurations (default vim doesn't seem to do this at the moment) hitting the key while in command mode will stick in ex mode. In this case, the ":" prompt will remain on screen, and hitting enter will scroll the entire screen upwards. To get back to vi mode, type "vi" (or "visual") and hit.

Moving around
Before any actual editing can take place, the first thing to learn is how to get around a file in command mode, and vi has a lot of movement commands. For this part of the tutorial, find an unimportant text file and load it into vi as explained in the opening a file section.

Up, down, left, right
When in command mode, use the, , , and keys to move the cursor left, down, up and right respectively. For modern versions of vi, the arrow keys will also work for this purpose - but is is advised to use "hjkl", as they are right under the finger tips. vi is very advantageous to touch typists, but less so if hunting and pecking.

Try using the "hjkl" keys to move around in the text file. Try pressing until the cursor gets to the beginning of a line. Notice that vi doesn't allow to "wrap around" to the previous line by hitting while on the first character, or to "wrap around" to the next line by hitting  at the end of a line.

Use + and + (remember as "foreword" and "back") to move forwards and backwards a whole page at a time. Modern versions of vi (like vim) will also allow use of the  and  keys for this purpose.

Go to the beginning or the end of a line
Press (zero - easy to remember) to jump to the first character of a line, and  to jump to the last character of a line. (caret) will go to the first non blank character of a line. These are keys that are often closer to the fingertips than the "home" or "end" keys, on most keyboards.

Since vi has handy movement commands close to the fingertips, it makes a great pager (like the or  commands). Using vi as the default system pager can help learn the movement commands.

Word moves
vi provides commands to move to the left or right by word increments. To move to the first character of the next word, press (remember as "word"). To move to the last character of the next word, press (remember as "word End"). To move to the first character of the previous word, press (remember as "word Back").

After playing around with the word movement commands, it may have become clear that vi considers words like  as five separate words! This is because by default, vi delimits words by spaces or punctuation. is therefore considered five separate words.

Sometimes, this is desirable behavior, but sometimes it isn't. Vi also has the concept of a "bigword". vi delimits bigwords by spaces or newlines only. This means that while  is considered five vi words, it's considered only one vi bigword.

To jump around to the next or the previous bigword, simply use a capitalized word move command. i.e. to jump to the first character of the next bigword,  to jump to the last character of the next bigword, and  to jump to the first character of the previous bigword. Compare the matching word and bigword movement commands until the differences are clear.

Move by sentence or paragraph
Use the and  characters to move to the beginning of the previous and next sentence.

Hit {{Key| { }} or {{Key| } }} to jump to the beginning of the current paragraph, or the beginning of the next.

Jumping about
In command-mode, jump to a particular line by typing. To jump to the first line of a file, type, or. Note that  is capitalized.

To go to the end of a file, type.

Searching
To jump to the next occurrence of a particular text pattern, type  and hit. Replace with the regular expression matching the text to be found.

Regular expression knowledge is not needed to search for simple strings. Just typing  will move to the next occurrence of foo.

To repeat the search forwards, hit. To repeat the search backwards, type. Type  to repeat the last search.

Save, and save as
To save changes to the current text file, type. To save changes to another file, type  to save as. To save and quit simultaneously, type  or.

Buffers (windows)
In vim (and other advanced vi editors, like elvis), it is possible to have multiple buffers open at once. To open a file into a new window, type. will appear open for editing in a new split window. To switch between windows, type +++ (control-w twice). Any,  ,   and   commands will only be applied to the currently-active window.

Editing from command mode
Editing commands in command mode tend to be simple. The more complex editing commands will automatically enter insert mode, but this will be covered later.

Deleting and replacing
Pressing will delete the character under the cursor. Now, move to the middle of a the paragraph somewhere in a text file, and hit (capitalized), this will join the next line to the end of the current line.

To replace a single character', move over a it and hit, and then type in a new character - this will replace the original character while remaining in command mode.

Move to any line in the file and type  to delete the current line of text.

Repeating and combining commands with a movement
Repeat any editing command by hitting the key. For example, typing  will delete 4 lines, and   will join four lines.

Some commands, such as the command to delete, can be combined with a movement command, using some of the movements previously discussed. For example,  will delete from the current position to the beginning of the next word;   will delete up until the end of the next sentence, and   will delete the remainder of the paragraph.

Undo
Vim maintains a history of actions, to allow to undo them. By pressing, the original version of vi allowed to undo the last edit only. Modern versions of vi, like vim, will allow to repeatedly press to continue to undo changes. Try combining some and  commands.

Insert mode
After covering how to move around in vi, perform file I/O, and basic editing operations, it is time to type in free-form text! vi's insert mode may seem a little complicated at first. However, after becoming comfortable with insert mode, its complexity (and flexibility) will become an asset.

In vi insert mode, enter text directly to the screen just like in many other visual editors. Once modifications are complete, hit escape to return to command mode.

Enter insert mode by pressing or. , will insert before the current character, and  will append after the current character. Remember, after entering text, to hit to return to command mode.

Try using the arrow keys and the key to get a feel for how insert mode works. By using the arrow keys and key, it is possible to perform significant editing steps without repeatedly entering and leaving insert mode.

Insert options
Here are some other handy ways to enter insert mode, from command mode. Press (capital) to begin appending to the end of the current line, regardless of the current position on the line. Likewise, press (capital) to begin inserting text at the beginning of the current line.

Press from command mode to create a new blank line below the current line into which to insert text, and press  (capital) to create a new line above the current line.

To replace the entire current line with a new line, press the key twice in a row. To replace everything from the current position to the end of the line, type. To replace everything from the current position to the beginning of the line, type.

In addition to performing a special operation, every one of these commands will enter insert mode. After typing in text, hit to return to command mode.

Changing text
The  (change) command was just introduced when typing ,   and. is a special form of the change command, similar to. The  and   commands are examples of using the change command in combination with a movement command. In this form,  works similarly to , except that it enters into insert mode to enter replacement text for the deleted region. Try combining some movement commands with  and test them out (hint: ,  ,  ).

These "composite" commands that change mode may not seem groundbreaking, but usage will show that these slight gains in having a command perform two steps not only speeds things up, but with practice will put less of a cognitive load on the user.

Compound commands
vi really becomes powerful when using compound ("combo") commands, like  and. In addition to these commands, it is possible to combine a number with any movement command, such as, which will tell vi to jump three words to the right. Here are some more movement "combo" command examples:,.

vi, in addition to allowing (number)(movement command) combinations, also allows  or   to be combined with a number or movement command. So,  will delete the next three words,   will delete the current and next two lines, etc. Test out some   and   combo moves to get a feel for how powerful and concise vi editing can be. Once these commands are learned, it will be second-nature to edit files at blazing speed.

Productivity features
Now that how to move, save and quit, perform simple edits and deletions, and use insert mode has been covered, it should be possible to use vi to perform almost any task.

However, vi also has many more powerful commands. This section will cover how to cut, copy, and paste, search and replace, and use autoindent features. These commands will help make vi even more fun and productive.

Visual mode
The best way to cut and paste is to use visual mode, a special mode that has been added to modern versions of vi, like vim and elvis. Think of visual mode as a "highlight text" mode. Once the text is highlighted, it can be copied or deleted, and then pasted. In gvim, highlight by simply dragging the left mouse button over a particular region:

Enter visual mode by hitting (this may be the only option if using vi from the console.) Then, by moving the cursor using movement commands (typically the arrow keys), highlight a region of text.

To copy highlighted text, hit (which stands for "yank"), to cut the text, hit, then vi will return to command mode. Now, move to the position to insert the cut or copied text, and hit to insert before the cursor, or  to insert after the cursor.

Voila, the cut/copy and paste is complete! Test out several copy/cut and paste operations before advancing to the next section.

Replacing text
To replace patterns of text, use ex mode. To replace the first pattern that appears on the current line, type  and press the  key, where is the pattern to match, and is the replacement string.

To replace all matches on the current line, type  and hit enter.

To replace every occurrence of this pattern in the file (which is often the case), type. To do a global replace, but have vi prompt for each change, type  (stands for "confirm") and hit.

Indentation
vi supports autoindentation, for editing source code. Most modern versions of vi (like vim) will auto-enable autoindent mode editing a source file (like a file, for example). When autoindent is enabled, use + (control-d) to move one indent level to the left, and + (control-t) to move one indent level to the right.

If autoindent wasn't enabled automatically, manually enable it by typing in. The tab size to can be set to user preference by using the  command;   is quite popular.

Finishing off
To use vi as the default editor in Gentoo, use :

To set this for a user instead of system wide, set the EDITOR variable in :

External resources

 * The vi Lovers Home Page - An excellent resource for all things vi.
 * The vim homepage - The place to go for all your vim needs.
 * Learning the vi and Vim Editors, 7th Edition - An excellent choice for those who are looking for a good, old-fashioned book. Contains good coverage of vi and vi clones.