Vi, Terminal Text Editor for Linux: Full Tutorial

Vi is a text editor used by many developers to write code, because it lacks an interface, thus avoiding any type of distraction allowing the user to focus all attention on the code. However, if we want to make the most of it, it is necessary to know a series of commands with which to interact with the code or written text.

The first time we run the application, it will show the user interface, a user interface where there is only one window and the currently highlighted cursor position and lines of text preceded by the ~ character. At the bottom, the file name is displayed along with the number of characters.

Vi, Terminal Text Editor for Linux

vi commands

This editor has two methods of operation: Edit / Insert mode and Command mode . The editing mode, as its name describes, allows you to add or edit lines of text or code to an already created document. It does not allow us to search.

Command mode allows us to scroll through the text and modify lines of code or text, perform searches and so on. This is the mode in which Vi runs every time we open it. To switch between the two modes of Vi, we just have to press the Esc key.

Another important thing to keep in mind when working with Vi is how it works. When we edit a file, the changes are not made directly on the file, but are saved in a copy of the document that is automatically created in the device’s memory. These changes are only saved when we save the contents stored in memory.

Vi commands in Command mode (forgive the redundancy), are generally made up of one or two letters plus a number . To use these commands, we must first enter a colon (:) and, on certain occasions, press the Enter key to execute them.

Next, we show you the most common Vi commands with which you can get the most out of this code editor. We must remember that to switch between Insert mode and Command mode, we must press the Esc key.

scroll through the file

If we use Vi from the device itself, we can use the arrow keys to scroll through the document. If not, we can use the following commands/letters

  • h to move the cursor to the right. Another option is to use the backspace key on our keyboard.
  • He moves the cursor to the left. You can also use the space key.
  • j to move the cursor to the top line.
  • k moves the cursor to the bottom line.
  • w to move between the words located after the cursor.
  • b allows us to move between the words located before the cursor.
  • e to move the cursor to the end of a word.
  • ^ moves the cursor to the beginning of the line where the cursor is
  • $ moves the cursor to the end of the line where the cursor is located
  • Enter will move the cursor to the next line.
  • H scrolls the view to the top.
  • L displays the last line
  • M moves us to the middle of the file.

modify text

If we want to modify characters, words or complete lines Vi, we must use the following commands:

  • s to replace the character where the cursor is located.
  • r to replace the character where the cursor is located.
  • cw at the beginning of the word that we want to replace with another.
  • cc at the beginning of the line where we want to replace the text.
  • To split a line , we move the cursor to the position where we want to split it, press the R key and Enter.
  • To join two lines into one, we move the cursor to the end of the upper line and press the J key.

insert text

If we want to insert text before or after the cursor or at the beginning and end of a line, the commands that we must use are the following:

  • I to insert text at the beginning of the line where the cursor is located
  • A to insert text at the end of the cursor line.
  • i to insert text before the cursor
  • a to insert text after the cursor.
  • Or insert a line above the cursor position.
  • or insert a new line below the cursor position.

Erase text

Vi allows us to delete characters and lines with these commands

  • x to delete the character after the cursor.
  • X to delete the characters located before the cursor.
  • dw deletes the word that follows the cursor.
  • dd to delete the entire line where the cursor is located.
  • D delete the rest of the line to the right of the cursor.
  • d0 deletes the content of the line to the left of the cursor.
  • dG delete all lines located from the cursor position.
  • d1G delete the lines located at the top up to the position where the cursor is.

Copy, cut and paste

To copy one or more lines, we must place the cursor at the beginning of the first line and write the command y2y to copy the two lines of text located just after the cursor. We must replace the 2 with the number of lines that we want to copy.

If instead you cut several lines, we want to move them, we must use the d2d command, to cut the next two lines at the cursor position. If there are more lines, we must replace the 2 with the number of lines to cut.

To paste the lines that we have copied, we place the cursor in the position where we want to paste them and press p to paste them after the cursor or P to paste them before the cursor position.

Search for and replace

If we want to search a vi file, we must use the symbol / followed by the text we want to search for and press the Enter key. If the document contains several results that match the search criteria, we can move between them by pressing the n key to access the next match or N to return to the previous match.

To replace words or a string if we are writing code, we use the following command

:g/text-what-we-search/s//text-to-replace/g

Undo changes

As long as we have not saved the changes we have made to the file, we can undo the changes using.

  • u to undo the last command we have executed.
  • U undo all the changes we make in one line, so we must execute it on the line where we want to revert the changes.

Merge two vi files

Through commands, the Vi editor also allows you to join two files into one. To do so, we must place the cursor in the position where we want to add the content of another file and use the command:

:#r name-of-the-file-we-want-to-join

Save files

As I mentioned above, all changes to a Vi file are stored in memory. From there, we have to transfer the changes to a file using the following commands:

  • w file-name save all changes to a file with the name we set.
  • w saves the changes to a file.
  • wq save the changes and close the editor.
  • what! close the editor without saving changes