No matter what work you do with the Unix/Linux system, you will eventually write some C programs or shell (or perl) scripts. You may have also edit some of the system files at times. For all this you must learn to use an editor, and Unix provides a very versatile one — vi. Bill Joy created this editor for the BSD system. This program is now standard on all Unix/Linux systems. Bram Moolenaar improved it and called it vim(vi improved).
Like any editor, vi uses a number of internal commands to navigate to any point in a text file and edit the text there.It also allows you to copy and move the text within a file and also from one file to another. vi offers cryptic, and sometimes mnemonic, internal commands for editing work. It makes complete use of the keyboard. where practically every key has a function.
Starting the vi Editor:
There are following way you can start using vi editor:
|vi filename||Creates a new file if it already does not exist, otherwise opens existing file.|
|vi -R filename||Opens an existing file in read only mode.|
|view filename||Opens an existing file in read only mode.|
You will notice a tilde (~) on each line following the cursor. A tilde represents an unused line. If a line does not begin with a tilde and appears to be blank, there is a space, tab, newline, or some other nonviewable character present.
Three modes of vi editor
- Command Mode: The default mode of the editor where every key pressed is interpreted as a command to run on text. You will have to be in this mode to copy and delete text. Unnecessary pressing of [ESC] in this mode sounds a beep but also confirms that you are in this mode.
- Input Mode: Every Key pressed aster switching to this mode actually shows us as text. This mode is invoked by pressing a or i.
- ex Mode or Last line Mode: This mode is used to handle files as saving and performing substitution (replacing one string with another) .Pressing a : in thee Command mode invokes this mode. you the enter an ex mode followed by [Enter].
Hint: If you are not sure which mode you are in, press the Esc key twice, and then you’ll be in command mode. You open a file using vi editor and start type some characters and then come in command mode to understand the difference.
Closing and Saving Files:
The command to quit out of vi is :q. Once in command mode, type colon, and ‘q‘, followed by return key. 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.
The command to save the contents of the editor is :w. You can combine the above command with the quit command, or :wq and return key.
The easiest way to save your changes and exit out of vi is the ZZ command. When you are in command mode, type ZZ and it will do the equivalent of :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 test2 and return.