One Way Is   Archives  

VI Editor

vi is a screen-oriented text editor originally created for the Unix operating system. The portable subset of the behavior of vi and programs based on it, and the ex editor language supported within these programs, is described by (and thus standardized by) the Single Unix Specification and POSIX.

Vim "Vi IMproved" has yet more features than vi, including (scriptable) syntax highlighting, mouse support, graphical versions, and much more.

Starting the vi Editor

The vi editor lets a user create new files or edit existing files. The command to start the vi editor is vi, followed by the filename. For example, to edit a file called temporary, you would type vi temporary and then hit return. You can start vi without a filename, but when you want to save your work, you will have to tell vi which filename to save it into. When you start vi for the first time, you will see a screen filled with tildes on the left side of the screen. At the bottom of your screen, the filename should be shown, if you specified an existing file, and the size of the file will be shown as well, like this: "filename" 21 lines, 385 characters If the file you specified does not exist, then it will tell you that it is a new file, like this: "newfile" [New file] If you started vi without a filename, the bottom line of the screen will just be blank when vi starts. If the screen does not show you these expected results, your terminal type may be set wrong. Just type :q and hit return to get out of vi, and fix your terminal type.

The Two Modes of vi

The first thing most users learn about the vi editor is that it has two modes: command and insert The command mode allows the entry of commands to manipulate text. These commands are usually one or two characters long, and can be entered with few keystrokes. The insert mode puts anything typed on the keyboard into the current file. vi starts out in command mode. There are several commands that put the vi editor into insert mode. The most commonly used commands to get into insert mode are a and i. Once you are in insert mode, you get out of it by hitting the escape key. If your terminal does not have an escape key, ^[ should work (control-[). You can hit escape two times in a row and vi would definitely be in command mode. Hitting escape while you are already in command mode doesn’t take the editor out of command mode. It may beep to tell you that you are already in that mode.

Getting Out of vi

The vi editor has two modes and in order to get out of vi, you have to be in command mode. Hit the key labeled “Escape” or “Esc” (If your terminal does not have such a key, then try ^[, or control-[.) to get into command mode. The command to quit out of vi is :q. If your file has been modified in any way, the editor will warn you of this, and not let you quit. To ignore this message, the command to quit out of vi without saving is :q!. This lets you exit vi without saving any of the changes. Of course, normally in an editor, you would want to save the changes you have made. The command to save the contents of the editor is :w. You can combine the above command with the quit command, or :wq. You can specify a different file name to save to by specifying the name after the :w. For example, if you wanted to save the file you were working as another filename called filename2, you would type :w filename2 and hit return. (there is a space between the :w and filename2) Another way to save your changes and exit out of vi is the ZZ command. When in command mode, type ZZ and it will do the equivalent of :wq. If any changes were made to the file, it will be saved. This is the easiest way to leave the editor, with only two keystrokes.

How to Type Commands in Command Mode

The command mode commands are normally in this format: (Optional arguments are given in the brackets) [count] command [where] Most commands are one character long, including those which use control characters. The commands described in this section are those which are used most commonly in vi editor. The count is entered as a number beginning with any character from 1 to 9. For example, the x command deletes a character under the cursor. If you type 23x while in command mode, it will delete 23 characters. Some commands use an optional parameter, where you can specify how many lines or how much of the document the command affects, the parameter can also be any command that moves the cursor.

Some Simple vi Commands

Here is a simple set of commands to get a beginning vi user started. There are many other convenient commands, which will be discussed in later sections.

Text Buffers in vi

The vi editor has 36 buffers for storing pieces of text, and also a general purpose buffer. Any time a block of text is deleted or yanked from the file, it gets placed into the general purpose buffer. Most users of vi rarely use the other buffers, and can get along without the other buffers. The block of text is also stored in another buffer as well, if it is specified. The buffer is specified using the " command. After typing ", a letter or digit specifying the buffer must be entered. For example, the command: "mdd uses the buffer m, and the last two characters stand for delete current line. Similarly, text can be pasted in with the p or P command. "mp pastes the contents of buffer m after the current cursor position. For any of the commands used in the next two sections, these buffers can be specified for temporary storage of words or paragraphs.

Cutting and Yanking

The commonly used command for cutting is d. This command deletes text from the file. The command is preceded by an optional count and followed by a movement specification. If you double the command by typing dd, it deletes the current line. Here are some combinations of these:

There is also the y command which operates similarly to the d command which take text from the file without deleting the text.

Pasting

The commands to paste are p and P. They only differ in the position relative to the cursor where they paste. p pastes the specified or general buffer after the cursor position, while P pastes the specified or general buffer before the cursor position. Specifying count before the paste command pastes text the specified number of times.

Indenting Your Code and Checking

The vi editor has features to help programmers format their code neatly. There is a variable that to set up the indentation for each level of nesting in code. In order to set it up, see the customization section of this tutorial. For example, the command to set the shift width to 4 characters is :set sw=4. The following commands indent your lines or remove the indentation, and can be specified with count:

The vi editor also has a helpful feature which checks your source code for any hanging parentheses or braces. The % command will look for the left parenthesis or brace corresponding to a particular right parenthesis or brace and vice versa. Place the cursor onto a parenthesis or brace and type % to move the cursor to the corresponding parenthesis or brace. This is useful to check for unclosed parentheses or braces. If a parenthesis or brace exists without a matching parenthesis or brace, vi will beep at you to indicate that no matching symbol was found.

Word and Character Searching

The vi editor has two kinds of searches: string and character. For a string search, the / and ? commands are used. When you start these commands, the command just typed will be shown on the bottom line, where you type the particular string to look for. These two commands differ only in the direction where the search takes place. The / command searches forwards (downwards) in the file, while the ? command searches backwards (upwards) in the file. The n and N commands repeat the previous search command in the same or opposite direction, respectively. Some characters have special meanings to vi, so they must be preceded by a backslash (\) to be included as part of the search expression. Special characters:

The character search searches within one line to find a character entered after the command. The f and F commands search for a character on the current line only. f searches forwards and F searches backwards and the cursor moves to the position of the found character. The t and T commands search for a character on the current line only, but for t, the cursor moves to the position before the character, and T searches the line backwards to the position after the character. These two sets of commands can be repeated using the ; or , command, where ; repeats the last character search command in the same direction, while , repeats the command in the reverse direction.

Settings for vi

You can customize the way vi behaves upon start up. There are several edit options which are available using the :set command, these are the vi editor options available: (You can get this list by typing :set all and then hit return in command mode)

noautoindent magic noshowmatch
autoprint mesg noshowmode
noautowrite nomodelines noslowopen
nobeautify nonumber tabstop=8
directory=/tmp nonovice taglength=9
nodoubleescape nooptimize tags=tags /usr/lib/tags
noedcompatible paragraphs=IPLPPPQPP LIpplpipnpbp term=xterm
noerrorbells prompt noterse
noexrc noreadonly timeout
flash redraw timeoutlen=500
hardtabs=8 remap ttytype=xterm
noignorecase report=5 warn
keyboardedit scroll=11 window=23
keyboardedit! sections=NHSHH HUuhsh+c wrapscan
nolisp shell=/bin/csh wrapmargin=0
nolist shiftwidth=8 nowriteany

Some of these options have values set with the equals sign = in it, while others are either set or not set. (These on or off type of options are called Boolean, and have no in front of them to indicate that they are not set.) The options shown here are the options that are set without any customization. Descriptions of some of these are given below, with an abbreviation. For example, the command set autoindent, you can type :set autoindent or :set ai. To unset it, you can type :set noautoindent or :set noai.

The EXINIT Environment Variable and the .exrc file

There are two ways to customize the vi editor. If you create a file called .exrc in your home directory, all the commands in there will be read when vi starts up. The other method is to set an environment variable called EXINIT. The options will be set in your shell’s setup file. If you use /bin/csh (C-Shell), the command is as follows, and is put in the .cshrc file: setenv EXINIT 'COMMAND' If you use /bin/sh or /bin/ksh, the command is as follows, and is put into the .profile file: export EXINIT EXINIT='COMMAND' For example, if you want to have auto indent, line numbering, and the wrap margin of three characters, then the setenv command (for C shell) looks like this: setenv EXINIT 'set ai nu wm=3' If you want to put more than one command in the setenv EXINIT, separate the commands with a vertical bar |. For example, to map the g command to the G character in command mode, the command is :map g G, and combined with the above command, you get this: setenv EXINIT 'set ai nu wm=3|map g G' If you want to create the file called .exrc, you can put exactly the same things in the file as shown in the quotes after the EXINIT.

vi commands

This list is a summary of vi commands, categorized by function. There may be more commands available, check online.

Cutting and Pasting/Deleting text

Inserting New Text

Manipulating Character/Line Formatting

Moving the Cursor Around the Screen

Moving the Cursor Within the File

Replacing Text

Saving and Quitting

Searching for Text or Characters

Written on June 4, 2016 by Abdul Ghiasy.