Vim, A Real Editor
What is Vi
A Brief History
Anciently, there was a very powerful line oriented editor called ed. ed was extremely difficult to use, so an “editor for mortals” was born. It was called em. em had two errors instead of just one. Bill Joy, then a student at Berkeley, combined many of the features of em with ed and called it ex. Then he lost all the source code. When he was done rewriting the thing he had vi. Initially, vi was a really primitive editor.
Vi was not Free in the sense that GNU software is Free. It was part of the proprietary Unix distribution and the source code was not readily available. This motivated a lot of people to rewrite vi so that they could add important features to it. That is why there are many vi clones today, some of which are available under open source licenses.
When you open vi, you get a buffer. A buffer is where you edit text. It is a region of memory that stores characters. It becomes a file when you write it to disk. Most of the time, you can think of a buffer as being equivalent to a file. I think that vi is much easier to use if you associate the buffer with a file name when you open vi. You can do this by passing a file name on the command line to vi. For example, you can open vi like this: vi <myfile>. If the file does not exist, then vi creates it when the buffer is saved. If the file exists, then vi loads it into the buffer as soon as it opens. Now you can edit that file by going into insert mode.
Vi is a mode oriented editor. Depending on your mode, the same keys do different things. Vi has an ex mode to make it run on just one line at a time like the archaic editors. It's most important modes, however, are normal mode, command mode and insert mode.
Normal mode is the default mode. In normal mode, each keystroke is interpreted as a short command. Command mode is used for commands that require arguments. Command mode is accessed with the colon ':' from normal mode. You can always stop what you are doing and return to normal mode by pressing the escape key (“Esc”–found in the top left corner of your keyboard). Insert mode is accessed with the “i” key. In insert mode you can edit the text in your buffer. You can return to normal mode by pressing the escape key.
There are other modes, but we will wait to discuss those until after you have more experience with vi.
Types of Vi
Because the source code to vi was not originally available, many people wrote clones. Though the basic functionality of vi is the same in each clone, they all extend the features of vi in different ways. This makes some clones very different from each other.
Vim is the most popular vi clone. It comes standard with most Linux distributions. Gvim is a graphical version of Vim with a nice GUI. This document focuses on Vim.
Other popular vi clones are elvis, viper, and calvin. They each have a different look and feel. You might find that one of them suits you more than vim.
The Vim Tutor
The best way to become familiar with Vim is by running the command vimtutor from the command line. This opens a 25-30 minute interactive vim tutorial. It is very thorough and will give you a good understanding of the basics of using vim.
Many times while programming it is helpful to open more than one file at once. There are many ways to do this in vim. The simplest way is to split the screen. To do this you can use the following commands:
split: Split the screen horizontally. vsplit: Split the screen vertically.
Both of these commands will give you a new buffer on the screen and allow you to still see the old one. You can then use :edit to open a new file in the buffer. I find this very handy for watching multiple parts of a file or viewing a header and the source file at the same time.
Now that you know how to put multiple buffers on the screen, you will probably want to learn how to move between them. I have found that the quickest way is to return to normal mode by hitting the escape key. You can then move between buffers by hitting <CTRL>-<w> followed by the direction you want to go (using either the arrow keys or h,j,k,l). This should make the buffer you moved to the active buffer.
Another helpful method for using multiple buffers is to use the MiniBufExplorer plugin.
Another handy trick when programming is to be able to fold your code. Very few programs can be contained in one screen of text, but folding helps more of your program to fit on the screen at a time. A fold will collapse a set of lines into one line. To make it work the best you will need to add this line to your $HOME/.vimrc file:
Then to create a fold you can hit <Shift>-<V> to go into visual line mode. Move the cursor to the end of the block you wish to fold and hit 'zf'. You should see one line replacing all the code you had blocked. To open the fold, just hit the space bar on the line. To close it you can just type 'zc' anywhere in the fold. You can learn more about folds by typing :help folding in vim.
When vim opens a file it creates a swap file with a name like this: <filename>.swp. Please note that the last letter of the filename may be different. These files are used to store a backup of the file you are working on so that the entire file does not have to be stored in memory. If vim dies while you are working on a file you can usually recovering it by starting vim with the filename as an argument and choosing to recover the file.
Everyone uses an editor differently, and because of this vim is very customizable. Most of the customization takes place in two scripts: $HOME/.vimrc and $HOME/.gvimrc. The first script is executed any time vim or gvim starts and the second is executed when gvim starts. To begin you may want to copy the example .vimrc to your home directory:
$ cp /usr/share/vim/vim61/vimrc_example.vim $HOME/.vimrc
You can add your own commands anywhere in this file. For a reference on how the file works you can type :help vimrc-intro. A list of the variables you can set and what they do can be found by typing :help
Programming with Vim
While programming I find it annoying to have to jump to another terminal to compile my code and then jump back and forth between the error messages and the code to try to find the error. Vim makes this whole process a lot easier. If you use make to build your program you can just type :make and it will run make. If there are errors you can type :copen and it will give you list of the errors in a new buffer. Moving between lines and hitting enter will jump to that error in your code. The command :cclose will close the buffer with the error messages. If you don't use a makefile you can set the command to compile your program with :set makecmd=“command”.
To see line numbers to the side of each line, just type :set number. To get rid of them you can use :set nonumber If you want to go to a specific line number just use :line.
Yes, you can run a shell inside of Vim. Type :shell and you will have a working shell inside your Vim session. Note: This shell will not allow you to run Vim inside of it or other programs that require a more functional terminal.