In this chapter you will learn how to work with the VIsual editor.
Objectives: In this chapter, future Linux administrators will learn how to:
Use the main commands of the VI editor;
Modify a text with the VI editor.
user commands, linux
Knowledge: Complexity:
Reading time: 20 minutes
Visual (VI) is a popular text editor under Linux despite its limited ergonomics. It is indeed an editor entirely in text mode: each action is done with a key on the keyboard or dedicated commands.
Very powerful, it is above all very practical since it is on the whole minimal for basic applications. It is therefore accessible in case of system failure. Its universality (it is present on all Linux distributions and under Unix) makes it a crucial tool for the administrator.
If the file exists at the location mentioned by the path, VI reads it and puts it in commands mode.
If the file does not exist, VI opens a blank file, displaying an empty page on the screen. When the file is saved, it will take the name specified with the command.
If the command vi is executed without specifying a file name, VI opens a blank file and displays an empty page on the screen. When the file is saved, VI will ask for a file name.
The vim editor takes the interface and functions of VI with many improvements.
vim[-ccommand][file]
Among these improvements, the user has syntax highlighting, which is useful for editing shell scripts or configuration files.
During a session, VI uses a buffer file to record all the user's changes.
Note
The original file is not modified as long as the user has not saved his work.
At startup, VI is in commands mode.
Tip
A line of text is ended by pressing Enter but if the screen is not wide enough, VI makes automatic line breaks, wrap configuration by default. These line breaks may not be desired, this is the nowrap configuration.
To exit VI from the Commands mode, press :, then type:
q to exit without saving (quit);
w to save your work (write);
wq (write quit) or x (eXit) to save and exit.
In command mode, Click the Z key of uppercase status twice in a row to save and exit.
You must add ! to the previous commands to force the exit without confirmation.
Warning
There is no periodic backup, so you must remember to save your work regularly.
This is the default mode when VI starts up. To access it from any of the other modes, simply press the Esc key.
At this time, all keyboard typing is interpreted as commands and the corresponding actions are executed. These are essentially commands for editing text (copy, paste, undo, ...).
This is the text modification mode. To access it from the command mode, you have to press special keys that will perform an action in addition to changing the mode.
The text is not entered directly into the file but into a buffer zone in the memory. The changes are only effective when the file is saved.
This is the file modification mode. To access it, you must first switch to command mode, then enter the ex command frequently starting with the character :.
The command is validated by pressing the Enter key.
In command mode, there are several ways to move the cursor.
The mouse is not active in a text environment but is in a graphic environment, it is possible to move it character by character, but shortcuts exist to go faster.
VI remains in command mode after moving the cursor.
It is necessary to position the cursor under the first character of the word to cut (or copy) otherwise VI will cut (or copy) only the part of the word between the cursor and the end.
To delete a word is to cut it. If it is not pasted afterwards, the buffer is emptied and the word is deleted.
The Ex mode allows you to act on the file (saving, layout, options, ...). It is also in Ex mode where search and replace commands are entered. The commands are displayed at the bottom of the page and must be validated with the Enter key.
From the 1st to the last line of the text, replace the searched string by the specified string:
:1,$ s/search/replace
Note: You can also use :0,$s/search/replace to specify starting at the absolute beginning of the file.
From line n to line m, replace the searched string with the specified string:
:n,m s/search/replace
By default, only the first occurrence found of each line is replaced. To force the replacement of each occurrence, you have to add /g at the end of the command:
:n,m s/search/replace/g
Browse an entire file to replace the searched string with the specified string:
Executing VI by specifying the options to be loaded for the session is possible. To do this, you must use the -c option:
vi-c"set nu"/home/rockstar/file
It is also possible to enter the Ex commands in a file named .exrc in the user's login directory. The commands will be read and applied at each VI or VIM startup.