LINUX – THE VI EDITOR
There are many ways to edit files in the Linux system, but one of the best way to edit the files is using the screen oriented text editor Vi. This editor enables to edit lines in context with other lines in the file.
Now we can find the improved version of the text editor which is called as VIM. VIM stands for V Improved.
The Vi is generally considered as a right standard in Linux system because,
· It is available for all flavors of the Linux system.
· Vi implementations are very similar across the board.
· It requires only few resources
· It is more user friendly than any other editors.
Vi editor can be used to edit the existing file or it can be used to create a new file from the scratch.
STARTING WITH VI EDITOR:
The following basic commands are used to view or edit the file.
Below is the example that shows how to create a new file if it does not already exist in the present (current) working directory.
After opening the file, the screen will appear as below,
In screen there will be a tilde (~) symbol on each line followed by symbol, the tilde symbol indicates it as empty line. If the tilde symbol does not present, then there may be space, tab, newline or any other non – viewable character may present.
After opening the file and before get into editing there are important concepts to be discussed in the vi editor,
There are two common modes in the file, one should know about these modes before starting to edit,
COMMAND MODE: This mode enables to perform the administrative tasks, such as saving files, executing commands, moving the cursor, cutting, yanking and pasting the lines or words and find and replacing. In this mode the typed characters are interpreted as commands.
INSERTION MODE: This mode enables to insert text into a file. Everything that typed in this mode is interpreted as an input and finally put into file.
The vi file always starts with an command mode, to enter a text insert mode should be selected. In order to enter into a command mode simply i should be pressed. To come out of the insert mode Esc key should be pressed, which will put back into a command mode.
GETTING OUT OF VI:
The command to quit out of vi is :q Once in command mode type colon (:) and q followed by return. If the file has been modified in any way, editor will warn and let not allow quitting. To ignore this message, the command to quit without saving is :q! . This allows quitting out of window without saving the changes.
The command to save the contents of the editor is :w . This command can be combined with the above quit command to save and exit. :wq .
The easiest way to save the changes and exit out of the window is the ZZ command. In the command mode type ZZ it will be equivalent to :wq
MOVING WITHIN THE FILE:
In order to move around the file without affecting the content enter into command mode (press Esc Twice). The following commands are used to move around the file.
The following two important points should be noted in vi editor:
· vi is case sensitive. So special attention should be taken for using capitalization of the commands.
· Most commands in vi can be prefaced by the number of times the action want to take place, for example 4k will move the cursor up four line.
There are many other ways to move within a file in vi. Remember that editor should in command mode pressEscTwice.
The control commands which can used along with the ctrl key,
To edit the file, editor should be in command mode. There are many ways to enter into insert mode from command mode.
Below is the list of commands which can be used for deleting the character, line in the opened file.
As mentioned above, most commands in vi can be prefaced by the number of times you want the action to occur. For example, 2x deletes two character under the cursor location and 2dd deletes two lines the cursor is on.
There is also capability to change characters, words or in line without deleting them. Below are commands
COPY AND PASTE COMMAND:
You can copy lines or words from one place and then you can past them at another place using following commands.
There are some advanced commands that simplify day-to-day editing and allow for more efficient use of vi,
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.
· 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 while using in search command and preceded by a backslash (\) to be included as part of the search expression.
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.
The substitution command (:s/) enables you to quickly replace words or groups of words within your files. Here is the simple syntax
The g stands for globally. The result of this command is that all occurrences on the cursor's line are changed.