The vi editor is available on almost all Linux and Unix systems. vi can be used from any type of terminal because it does not require the cursor-moving keys and function keys the alphabetic keys can be used for commands.
vi (often pronounced vee – eve) is short for the visual editor. It is a visual editor because, at any point in time, a part of the file being edited appears, displayed on the screen. v/is also a text editor it lets you add, change and delete text, but it does not provide formatting commands which would allow you to center powerful lines, or indent paragraphs, for example.
The vi editor is a screen-based editor, it has powerful features to aid programmers, but many beginning users avoid using vi because the different features overwhelm them Once you start learning, however, you realize that vi is well designed. You need only a few keystrokes to tell vi to do complex tasks. As you learn vi, you learn shortcuts that transfer more and more of the editing work to the computer – where it belongs.
vi (like any text editor) is not a”what you see is what you get”word processor. If you want to produce formatted documents, you must type in codes that are used by another formatting program to control the appearance of the printed copy. If you want to indent several paragraphs, for instance, you put a code where the indent begins and ends. Formatting codes allow you to experiment with or change the appearance of your printed files, and in many ways, give you much more control over the appearance of your documents than a word processor.
Starting 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 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 later.
When you start vi for the first time, you will see a screen filled with tildes (-) on the left side of the screen. Any blank lines beyond the end of the file are shown this way 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:
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 return to get out of vi, and fix your terminal type. You may use vi to edit a file that already exists by typing:
vi file name
Where the filename is the name of the existing file.
Modes of vi Editor
vi editor consists of three modes
- Command Mode
- Insert Mode
- EX Mode (Execute Mode)
In this mode, commands are used to move around and edit text objects such as letters, words, sentences, and paragraphs Pressing the Esc key will return us to command mode. If we want to reassure ourselves that we are in command mode, press Ese again and we should hear a beep.
We can use the arrow keys to move around in command mode, vi users do not use the arrow keys but rather the more ergonomic h|j|k|l keys situated on the home row, vi users also know how to jump or modify efficiently, the parts of the text. In Command mode we can use several commands. These Commands are case sensitive, for example, a does not mean the same thing as A. vi is a display-oriented text editor based on the underlying line editor ex. This means we can also give ex commands to vi. To enter an ex command, type : in command mode. We might now type set showmode followed by Enter. This tells vi to display the mode on the status line. We need to edit the .exre file to make this the default setting.
In this mode, we can insert text into a buffer. There are several commands that we can use to enter this mode.
We can now enter :help to find out what internal documentation our version of vi has to offer. In the case of vim, this help is pretty extensive, while in the case of regular vi, we should at least be offered a summary of all the available vi commands.
Inserting New Text
Text Insertion with”i”
How are you
i, I’m fine [esc]
How I am fine _ are you
Text Appending with”a”
How are you
a. I m fine [esc]
How a I am fine re you
Insertion of text by “I”
How are you
How are you
Insertion of text by “A”
How are you
A Hi [esc]
How are you Hi
A : Append at the end of the current line.
I : Insert from the beginning of a line.
O : (letter oh) Enter insert mode in a new line above the current cursor position.
a : Enter insert mode, the characters typed in will be inserted after the current cursor position. A count inserts all the text that had been inserted that many times.
i : Enter insert mode, the characters typed in will be inserted before the current cursor position. A count inserts all the text that had been inserted that many times.
o : Enter insert mode in a new line below the current cursor position.
1. “r” replacing single character
how are you
How are you
“S” replacing text with s
How are you
S I’m fine [esc]
How I’m finere you
C : Change to the end of the line from the current cursor position.
R : Replace characters on the screen with a set of characters entered, ending with the Escape key.
S : Change an entire line.
c : Change until “cc” changes the current line. A count changes that many lines.
r : Replace one character under the cursor. Specify a count to replace a number of characters.
s : Substitute one character under the cursor, and go into insert mode. Specify a count to substitute a number of characters. A dollar sign ($) will be put at the last character to be substituted.
Execute Mode (EX Mode)
The vi editor is built upon another editor, called EX. The EX editor only edits by line, From the vi editor we can use the : command to start entering an EX command. This list given here is not complete, but the commands given are the more commonly used. If more than one line is to be modified by certain commands (such as “:s” and “:w”) the range must be specified before the command.
:ab string strings
Abbreviation: If a word is typed in vi corresponding to string1, the editor automatically inserts the corresponding words. For example, the abbreviation “:ab usa United States of America” would insert the words, “United States of America” whenever the word “usa” is typed in.
:map keys new_seq
Mapping: This lets us to map a key or a sequence of keys to another key or a sequenc of keys.
Quit vi: If there has been changes made, the editor will issue a warning message.
Quit vi without saving changes.
Substitute: This substitutes the specified pattern with the string in the to_patter Without options, it only substitutes the first occurrence of the pattern. If a ‘g’ is specified, then all occurrences are substituted. For example, the command “:1,$s/Sourc/Target/g” substitutes all occurrences of “Source” to “Target”. In which :1,$s specifies from first line to last line.
Sets: It sets some customizing options to vi and EX. The “:set all” command gives all the possible options.
Removes the abbreviation previously defined by “:ab”.
Removes mapping defined by “.map”.
Starts editing a new file. If changes have not been saved, the editor will give us a warning.
Write out the current file.
Write the buffer to the filename specified.
:w >> filename
Append the contents of the buffer to the filename.
Write the buffer and quit.
Getting Out of vi
Now that you know how to get into vi, it would be a good idea to know how to get out of it. 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. If you were already in the command mode when you hit “Escape”, don’t worry. It might beep, but you will still be in the command mode.
The command to quit out of vi is :q. Once in command mode, type colon, and ‘q’. followed by return. 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 filename 2, you would type: w filename 2 and return.
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.