|Front | Info | Lists | Newsfeeds | Study Guide | What is BSD?|
This is the BSDA Study Guide Book written via a wiki collaboration. This is a work in progress. You may contribute to or discuss this specific page at http://bsdwiki.reedmedia.net/wiki/Be_familiar_with_the_vi__40__1__41___editor.html.
Be familiar with the vi(1) editor
The default editor on BSD systems is often vi(1) and many system utilities require familiarity with vi(1) commands. Be able to edit files using this editor, as well as modify a read-only file or exit vi(1) without saving any edits to the file.
The vi editor is a screen oriented text editor. ex is a line oriented text editior. Both ex and vi are different interfaces to the same program.
FreeBSD, NetBSD, OpenBSD and DragonFlyBSD all use the nex/nvi versions of the ex/vi text editors, these are bug-for-bug compatible replacements for the original Fourth Berkeley Software Distribution (4BSD) ex and vi programs.
As stated above vi is a screen editor, in practice this means that it takes almost the entire screen. The sreen is mainly a display of the lines in a file, except for the last line which is used for you to give commands to vi. vi is a modeful editor. This means that you are either entering commands or entering text. You need to be in the correct mode to do one or the other.
vi commands can be broken down into four types of commands:
To ease the process of familiarisation with vi, it is important to remember that it is a modeful editor. When starting vi from the command line you are initially in command mode. In command mode typing on the keyboard issues commands to the editor. These commands usually one or two characters, such as i to insert text or cw to change a word. To enter text, you need to be insert (i) or append (a) mode. To return to command mode you press the Esc key.
To remind you which mode you are in you can use :set showmode to tell vi to display on the command line at the bottom of the screen which mode you are in.
The command-line options are those used to invoke the vi editior, such as starting vi to edit a file called filename. If the file does not exist it will be created, thus if you edit a file with vi and it opens an empty file then chances are you have mistyped the file name.
If you wanted to invoke vi in read-only mode you have two options, either invoke with the -R switch, or use view:
All the switches are documented in the man page. But two useful switch when invoking vi are + and +n as illustrated below:
Before dealing with the movement and editing commands we will look at the exit commands, the reason for this is that if you open a file by mistake or wrongly edit a file it is useful to know how to recover the situation.
To that end the first exit command to mention is:
This is important as vi normally tries to save all edits to files. If you just use :q which is quit file in vi you will be warned if modifications have been made.
To save the file you use :w and this can be combined with the quit command to write and exit the file:
The ZZ command performs the same function as :wq in that the file is saved, if modified, and then vi quits that file.
If you are in command mode typing i or a with put you in editing mode. i inserts text before the cursor and a inserts text after the cursor. To exit editing mode you just press the Esc key. Once you have started editing a file it is useful to use the showmode command, this will tell you if you are in command or edit mode:
vi provides many methods of moving around the file, from character level to moving by screens, and using search to move around the file. This section will give a brief overview of the many movement commands that are available in vi. Character level movement is achieved with:
Character level movement can also be achieved using the arrow keys on the keyboard. Moving around a file at the character level is not always the most efficient method, however you can backwards and forwards through the file by word beginnings and endings:
At the line level:
Movement through the file can also be achieved by line numbers, where Ctrl-G will display your current line number. nG will move you to line n, and G will move you to the last line of the file. You can also use the ex command for moving to a particular line by doing :n where n is the line number to move to.
Before leaving the section on movement around files, another useful way to move around a file is using vi search capabilities. By typeing /pattern or ?pattern vi will search forwards (/) for pattern or backwards (?) through the file. Similarly to repeat the previous search just entering / for a forward search and ? for a backwards search. n will repeat a search in the same direction, and N will repeat the search in the opposite direction.
vi editing commands cover inserting text, changing text, deleting or moving text and yanking (vi version of copying) text. i and a have been mentioned above. When they are capitalized you get:
Changing text can be achieved at the character, word, line and even greater levels.
To copy words or lines in vi they are Yanked using:
Yanked words can then be pasted using:
The deletion commands in vi, can also be used to move text around a file as vi puts the deleted text in a buffer which can then be put elsewhere in the file by using the p and P commands.
Finally there are four other editing commands that are worth a quick mention:
vi(1) including: :w, :wq, :wq!, :q!, dd, y, p, x, i, a, /, :, :r, ZZ, :set number, :set list
ex, Bill Joy. USD/12.vi
Learning the vi Editor by Linda Lamb & Arnold Robbins is a useful text from O'Reilly. In addition there are many tutorials available on the internet.
While using the vi editor you can escape to a shell, using :sh, or execute comands using :! cmdname, thus if you allow users to edit configuatation files using sudo, you might well be giving them root access.
There are many vi clones that add functionality, such as Vim http://www.vim.org/ and Elvis http://elvis.the-little-red-haired-girl.org/. The advantage of these clones is that they often have GUI's and run on other OSes so you can use for favourite editor (vi of course ;~D) where ever you go.