vi command on Linux (Text Editor)
The vi Command in Linux is a powerful ASCII text editor that is widely used in the interface Linux character for editing files and programs. Its use is not very intuitive at first glance, but simple text editing can be done using few commands.
It can be especially useful for small one-off corrections, especially in a production environment. So it’s not a bad idea to know how to deal with this text editor universally available on Linux distributions.
Basically it has two working modes: the operating mode and the insertion mode. In operating mode, the vi waits for commands that will perform some action. In insert mode, everything you type is considered text.
The operating mode is accessed via the ESC key. The vi always starts in this mode. To enter the insert mode, simply press the INSERT key or the “i” command.To do simple file editing in vi:
Change the insertion mode with the INSERT key or by pressing the “i” key. The cursor can be moved in this mode with the position arrows. When you finish typing press the ESC key and type:
:w to save the changes ;
:wq to save and exit vi ;
:q! to exit without saving;
Below is the table of commands accepted by vi. All commands must be given in operating mode. The “+” sign indicates keys that must be pressed simultaneously. The comma indicates that the keys must be typed in sequence. Also check out our content with all the main processes and commands in Linux.
**Action** | **Command** |
Open File | :e Filename |
Save File | :w |
Save File As | :w Filename |
Save and Quit | :wq |
Quit without save | :q! |
Save if content changed | :ZZ |
Mark a text to copy or cut | v, arrow keys |
To copy a marked text | y |
To cut a marked text | c |
To paste a marked text | p |
Copy an line | yy |
Copy until end of file | yG |
Delete text in front of cursor (DEL) | x |
delete text in back of cursor (BACKSPACE) | X |
Erase line | dd |
Erase until end of file | dG |
Erase until end of the line | D |
Find a text below | /texto |
Find again | / |
Find a text upwards | ?texto |
Find again | ? |
Undo changes | u |
Redo changes | CTRL+r |
Center the text | :ce |
Left align | :ri |
Right align | :le |
Open new window | :new |
Split window in two | :split |
Open file in new window | :split filename |
Go to window up | CTRL+w, k |
Go to window down | CTRL+w, j |
Go to beginning of line | 0 |
Go to end of line | $ |
Go to end of the file | G |
Go down one line | j |
Go up one line | k |
Go to the left | h |
Go to the right | l |
Go to end of screen | L |
Help | :help |
Insert text before cursor | i |
Insert text after cursor | a |
Insert text em uma nova linha | o |
When running, I saw it searching for a configuration file called “.vimrc” in the users’ HOME directory. This file doesn’t always exist, but it can be created by the user to do some cool things with vi, such as:
Colorize the syntax
To color the syntax of files with source code, you must enable the “sintax on” directive in the .vimrc file, with the command:
In this way, vi will color the text, recognizing several programming languages. Use single quotes in these commands, as shown.
Syntax on in vim |
Enumerate the lines
Another interesting feature in vim is line numbering, which allows you to easily debug a source code. To enable this feature, you must use the “set number” directive in the .vimrc file, with the command:
In this way, vi will list the text, making it easier to debug the code.
Enumerate the lines |
Syntax Debugger
This vi option is very interesting for programmers. It allows a key to be mapped to activate a language compiler or interpreter, in order to verify the syntax, without having to leave the text editor.
In the example, the “-l” php option is used, which only performs a syntax check. The key combination chosen was CTRL B, which executes the command php -l in the source file.
’>>~/.vimrc
Thus, to check the syntax of a php code, simply press CTRL B, and the php interpreter is activated to verify the syntax:
Php code with error |
In this example, I induced a missing “;” error. When you press CTRL B, php will say that there is a syntax error:
Call to the interpreter |
This vim facility is extremely useful in interpreted languages, which allow you to verify the syntax without having to leave the editor.
For other languages, the same concept can be used by changing the syntax check option:
PERL: echo ‘map:! perl -c%
’>>~/.vimrc
PYTHON: echo ‘map:! python -d%
’>>~/.vimrc
Infinite undo
The “u” key in vi’s command mode allows you to return to the previous text, undoing the changes. However, when leaving vi, the ability to undo changes is lost. So, if you’re editing a source code and exit the editor, you won’t be able to use the undo changes feature anymore.
But there is a solution for maintain a history of changes, considered a true lifesaver.
It allows vi to keep a history of file changes in a special directory on the user’s HOME.
To enable this feature, you must execute the following commands:
First, the directory is created to store the history in the user’s HOME:
Then the undofile feature must be enabled:
Finally, the directory is configured to store these files, created in the first step:
In this way, vi will keep the history of changes, making it possible to undo changes with the “u” key.
From time to time, it’s interesting to clear the contents of the ~/.vim/undodir directory.
Change text (replace)
The feature to replace one text with another in vim also helps a lot to make code replacements in vi.
To make a replacement, enter command mode with the ESC key and then type:
In this way, the vim will search for all occurrences of the text to be searched and will replace it with text to be replaced in the entire document.
If you want the vi to just replace the first occurrence, the command is the same, but without the “g” at the end:
If the text to be searched or replaced contains a simple “/” slash, a ”\” backslash must be used first, to let the vi know that the ”/” in the text is not interpreted as a separator.
In this example, the instance of “/etc” will be replaced with “/var “:
Another interesting replacement is to place all the lines inside an HTML tag, such as a paragraph:
\ r&\ r/<\ /p>
The expression**.** * indicates the beginning of the line, and the ”\ r” the “return” (end of the line).
Search text
To search for text in vi, simply press the “/” bar in command mode and type the text. In this way, the vi will search for occurrences of the search term one by one, pressing the “/” key again.
One feature to boost text searches in vi is to enable the Highlight the term, as well as enable the “case insensitive”, allowing searches ignoring whether the text is in upper or lower case:
To enable search text highlighting:
To ignore whether the term is in an upper or lower case:
To search for text in accordance with the upper box, if the term contains a capital letter:
Indentation
An interesting trick of vi is the automatic indentation of the text.
To fix an indented source code, type in command mode:
This way, vi will format the code, making it more readable.
It is also possible to determine that the vi indents the code while you program, varying the spacing depending on the type of source code.
To enable this feature:
Then, you must create a directory called “.vim” in the HOME directory, if it doesn’t already exist:
In this directory, a file must be created for each type of source code, as in the example:
In this example, there is a php.vim file inside the ~/.vim directory.
And there are two directives in it:
- shiftwidth=2 Determines the number of spaces for self-indentation to be 2. The default is 8 spaces.
- tabstop=2 Determines the number of spaces the TAB contains. The default is 8 spaces.
For python for example:
In this example, the expandtab and softtabstop directives are for:
- expandtab It doesn’t replace TAB with spaces;
- softtabstop allows you to pretend that TABS are spaces when editing.
Remove the ^M at the end of the files
Generally after copying a text file from Windows to Linux and editing it in vi, the file appears with “^M” codes at the end of each line.
In this way, you can adjust the file in vi, using the command below, in command mode:
The replacement command also works well:
Did you like it?
Share