Updated: 2012-02-17 06:35 EST

1 Introduction Index up to index

2 Versions of vi/vim Index up to index

The stock Unix “vi” and the open-source “vim” (and various other clone editors such as “nvi”, “elvis”, and “stevie”) are all based on the original 1980’s Berkeley “vi” program written by Bill Joy (the same guy who wrote the C Shell).

Some systems (e.g. Ubuntu) install a “light” version of vim as a default; you have to ask for the full version after installation. You may find that some of the features described in a textbook only work on a full installation of “vim” (e.g. the “Getting Help” commands).

I will refer to the editor using its traditional name “vi” - you should always interpret this to mean modern “vim”, not the old “vi” editor.

3 Learning the VIM Text Editor Index up to index

VIM is an easy-to-use but hard-to-learn text editor. It doesn’t need the X Windows system to be running; you can use it over a serial line and a dial-up modem.

Start by printing a VIM Reference Card and doing the online tutorial:

3.1 Online VIM Tutorial - vimtutor Index up to index

3.2 VI and VIM Reference Cards Index up to index

You can find good “cheat sheets” on VI by searching on the Internet for “vi editor cheat sheet” or “vi editor summary pdf” or “vi reference card”. URLs come and go; but, these were working at one point:

3.3 Other Tutorials Index up to index

You may find these useful:

3.4 What to Learn First Index up to index

You can get by in VI with knowing how to read in a file, delete and append single characters and lines, and write the file back out. (See the list of “bare minimum” commands, below.)

The more you learn about VI, the faster you will edit files and the better your mark will be on tests and exams. VI skill is essential to you as a Unix programmer; it is now the universal Unix editor.

Almost every letter, upper and lower case, is a VI command. The more you learn, the faster you can edit. While you may begin by learning to delete a word slowly using ten “x” commands, eventually you should evolve into knowing how to use the single quick “dw” command.

See below for the location of the online VIM tutorial.

3.5 Avoid the Arrow Keys Index up to index

Do not use the arrow keys to move the cursor - use the command-mode letters h,j,k,l instead. The letters don’t require you to move your hands from the home row on your keyboard, and they encourage you to remain in command mode where you can use all the VIM commands. Arrow keys will slow you down.

4 Setting VIM Options - Getting Help Index up to index

VIM comes with hundreds of options that influence how it works. You can set the options at any time; but, to save them and have them used every time you start VIM, you need to put commands into a file named “.vimrc” in your home directory.

The following are good options that you should set in your .vimrc file, if VIM does not already have them set for you:

$ echo "set showcmd showmode confirm ruler" >>$HOME/.vimrc
$ cat $HOME/.vimrc
set showcmd showmode confirm ruler

The above shell command line appends the options to your .vimrc file. You only need to do this once; VIM will read the file on start-up. If you execute the above command more than once, you will put multiple copies of the options into the file. This isn’t an error; but, it isn’t necessary either. Edit the file and remove the duplicate lines.

The full “vim” editor has help available by typing “:help”, e.g. you can type “:help showcmd”. This splits your terminal screen and opens up a help file inside “vim”. To close the help file, just do :q as you would do to close any file.

Some people also like to have option “set number” in their .vimrc.

5 Absolute bare minimum basic VI commands Index up to index

Look up the meaning of these VI/VIM commands:

a i x dd
h j k l
:r file
:w file

The above commands will perform most any edit on any file you have. All the other things in VI simply make the editing faster and get you better marks (and a better job) in less time.

Some of the other VI commands make the editing much, much faster. Compare “dd” (delete a whole line) with typing 80 “x” commands, or “:1,$s/idallen/alleni/g” to replace every occurence of “idallen” with “alleni” in the whole file using a single global search and replace!

6 What to learn Next in VI Index up to index

Other useful things to know in VI, in vague order of importance:

u                        -- undo
p P                      -- put (after yank or delete)
/search string           -- search for pattern in file
.                        -- repeat last change
r                        -- replace one character
o O                      -- start a new, empty input line
5dd 10x                  -- repeat counts on most commands
0 ^ $ H L M              -- position cursor on screen
A I                      -- insert text at end/start of line
U                        -- undo all changes on current line
cc s                     -- change current line, current char
f F                      -- move forward/backward to character
!!                       -- pipe text into any Unix command
:1,$s/pattern/new text/g -- global search and replace
:set showcmd             -- show partial commands in status line
:set showmode            -- show INPUT mode
:set confirm             -- ask before quitting modified buffer
:set ruler               -- show current file position
:set autoindent          -- auto-indent lines of code
:set wrapmargin=5        -- wrap input text at column 75
:set number              -- show buffer with line numbers
:syntax off              -- turn off syntax highlighting
:$r file                 -- read file in at end of buffer
:$r! program             -- read output of executing program

The VIM tutoral has details on many of these commands.

7 Using VIM over a remote connection Index up to index

This section is only for people using vim over a remote connection. If you use vim in a Linux terminal window, things usually work fine.

Make sure your terminal is correctly configured before you enter VI! The VI/VIM editors must know how to drive your terminal during an edit session; if you see anything about an “unknown” terminal type when you start up the editor, you must exit immediately and fix the terminal type.

Be careful when you resize a window containing a VI/VIM session. A remote session of VI on another machine may not pick up the new window size, and what you see on your screen may get garbled. (If this happens, quit VI, type “resize” at the shell prompt, and restart VI again.)

To be safe, do not change the size of a VI/VIM window when the editor is open in a remote window.

If you use telnet/PuTTY from under Windows to connect to a Unix machine, make sure you have NO SCROLL BARS VISIBLE in your telnet window! Set the correct number of lines and correct terminal type when you start telnet, and don’t change them. (This won’t be a problem for people working from a Unix machine, since Unix is good about passing the window size to VI.)

The “resize” command is useful to set the number of lines correctly after you telnet or ssh into the remote machine:

$ resize

8 Finding a missing vimtutor VIM Tutorial Index up to index

If “vimtutor” doesn’t work, you will have to locate and copy the tutorial file yourself. On most Unix/Linux machines, you will find the tutorial somewhere under the directories /usr/share/vim* or /usr/local/share/vim*, e.g. possibly /usr/share/vim/tutor/tutor. Different Unix systems put the tutorial in different places; go look for the file named “tutor” under the above places.

The vim tutorial is a file named “tutor” under directory /usr/share/vim/vim63/tutor/ on Fedora and under /usr/share/vim/tutor/ on most Linux systems.

The “vimtutor” command startes up vim on a fresh copy of the tutorial file. If you are not using “vimtutor”, you must copy the tutorial file into your own directory first:

$ cd
$ cp /usr/share/vim/tutor/tutor tutor     # use the actual pathname!
$ vim tutor

Use the actual pathname to the “tutor” file on your system. Follow the instructions in the tutorial.

The VIM tutorial was written to be used under various operating systems, including MS-DOS; so, it has one or two MS-DOS specific external commands in it (e.g. there is no DEL command in Unix - use rm instead!). Also, if you have “set ruler” turned on, the Ctrl-g command will not show your current line number; because, the ruler line already shows it for you.

| Ian! D. Allen  -  idallen@idallen.ca  -  Ottawa, Ontario, Canada
| Home Page: http://idallen.com/   Contact Improv: http://contactimprov.ca/
| College professor (Free/Libre GNU+Linux) at: http://teaching.idallen.com/
| Defend digital freedom:  http://eff.org/  and have fun:  http://fools.ca/

Plain Text - plain text version of this page in Pandoc Markdown format

Campaign for non-browser-specific HTML   Valid XHTML 1.0 Transitional   Valid CSS!   Creative Commons by nc sa 3.0   Hacker Ideals Emblem   Author Ian! D. Allen