Text editor vi. The very short manual

In this post I will shortly describe editor vi, its main modes and commands. Also, I will give some useful tips how to use it the best way. То же по русски.

Editor vi (visual editor) – a universal full-screen text editor for Linux (UNIX). It is included in all versions and distributions.

People use text editor vi to make changes in the text files on the server already for 50 years. Learning editor vi for this purpose only takes a few minutes. That is the time you read this post. So I will not dive too deep.

Editor vi has three modes:

  • the first mode is command mode – in this mode one can navigate through the file, type commands for text editing or switch to another mode. Arrow keys move the cursor trough the edited file. Used in command mode commands consist of one or two lowercase Latin characters.
  • the second mode is insert mode – in this mode all typed on a keyboard symbols are displayed on the terminal screen in the place, where editor cursor is placed, and are stored in the buffer. Any editing commands do not work, except moving through the text using arrows keys. One can enter to the insert mode from command mode by pressing “i” key (from the word “insert”). To exit from this mode one have to press “esc” key.
  • and finally, the third mode is command line mode allows one to perform more complex commands, such as search and file management. To enter to the command line mode from the command mode one have  to press the colon “:”. Then one can see all typed characters in the bottom line of the screen.

Until now, everything sounds scary. Actually vi is not so bad, as it looks from the first glance. It’s simple and very efficient. Otherwise it could not survive for more then 50 years.

To start editor vi, one have to type (see “Linux commands. The very short description“):

vi falename

To edit existing file, one have to type its name. To create a new file, respectively, one have to type the filename, one wants to create. Editor vi is in command mode after start.

Editor vi. Command mode commands briefly:

All commands use only in lowercase Latin letters.

1. Moving the cursor on the screen:

  • arrows keys or h, j, k, l on one symbol (or one line);
  • 0” (zero) to the beginning of the line or “$” to the end of the line;
  • w” to the beginning of the next word, “b” to the beginning of the previous word;
  • ctrl+f” moves cursor to the screen down, and “ctrl+b” to the screen up;
  • G” moves cursor to the very beginning of the text and “shift+G” to the very end of the text.

The numerical coefficient in the front of the move command repeats it specified number of times. For example, command “j” moves the cursor one line down. Respectively command “5j” moves the cursor five lines down, etc.

2. Editing

  • d” cuts text to the clipboard. Respectively, “d$” cuts from the cursor to the end line. “d0” (zero) cuts from the beginning of the line up to the cursor. “dd” cuts the whole line and “5dd” cuts five lines down, beginning with the one where the cursor is located;
  • y” copies text to the clipboard. Respectively, “y$” copies from the cursor to the end line. “y0” (zero) copies from the beginning of the line up to the cursor. “yy” copies the whole line and “5yy” copies five lines down, beginning with the one where the cursor is located;
  • p” pastes text from the clipboard. If there is a part of the line in the clipboard, it is inserted after the cursor. If the whole line or several lines, so they are inserted after the line, where cursor is located;
  • u” undoes the last edit operation. One can press the command many times, until it would undo all editing operations in the opened the file. Editor vi would inform user, when he/she undo the first editing operation.
  • /expression” searches expression through the text from cursor to bottom. One have to type the expression directly behind the sign “/“. And they will be displayed on the bottom line of the screen. For example, “/test” will seek for “test” the text. It is easier to type expressions by copy-paste. If one wants to continue searching the same expression, then one have to simply type “/“.

3. Command line commands

The command line consists of the colon “:” (the command line qualifier) and some Latin letters or symbols. The most useful ones:

  • w” writes the file to disk. The mew version of the file replaces the old one. If one need the old version , then before editing it is necessary to save it somewhere or with a modified name (for example, I add an extension .orig – original).
  • q” closes editor vi. If one made any changes, then to close the editor without saving changes, one need to type “q!“;
  • set nu” – turn on line numbering;
  • set nonu” – turn off line numbering.

Actually, this is all that one may need to make small changes in text files on the server.

Good advices

If one need to create a new file, so one have to press command “i” just after the editor vi launch to switch to input mode, enter by copy-paste the text, and then press the button “esc” to return to command mode, and “: wq” to save the file and exit. That is it.

If one need to make a small changes in a large file, it is better to use the search command after starting editor vi to find something in those places in the text where you have to make changes.

If I need to make a several changes to the text, I open multiple terminal windows. The first of them I use only to make changes without leaving the editor vi. And after I made each change, I save the new version with the command “:w“. At this time in the other open windows I can test and validate each change. What it gives us? If we did not close the editor vi, one can always undo the change by using command “u” in case the changes is wrong.

If one wants to make a significant changes in the text, it is easier to transfer the file to a workstation using the sftp (See posts “Client sftp. Short description.”, “PSFTP – SFTP client is PuTTY for Windows“. or “SSH client for Windows Bitvise Tunnelier” depending on the operating system and the user’s tastes), then edit it by favorite editor and move back to the server again using sftp.

In this post I described the text editor vi basic commands, which is necessary to make small changes in text files on the server.


No comments yet.

Leave a Reply