This is the most powerful Linux text editor: discover all its commands

0
91

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 their attention on the code. However, if we want to take full advantage 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, the user interface will be displayed, a user interface where there is only one window and the currently highlighted cursor position and the lines of text preceded by the ~ character. At the bottom, the file name is displayed along with the number of characters.

vi commands

This editor has two working methods: 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 perform searches.

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

Another important aspect to consider 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.

  • l 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 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 shows the last line

  • M moves us to the middle of the file.

Modify text

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

  • s to replace the character where the cursor is.

  • r to replace the character where the cursor is located.

  • cw at the beginning of the word 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 top 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

  • 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 before the cursor.

  • dw deletes the word that follows the cursor.
  • dd to delete the entire line where the cursor is.

  • 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 from the cursor position.

  • d1G delete the lines 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 we want to copy.

If you instead cut multiple lines, we want to move them, we must use the d2d command, to cut the two lines following 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-to-find/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 executed.

  • U undo all the changes we make in a 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 merge 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 made to a Vi file are stored in memory. From there, we have to move the changes to a file using the following commands:

  • w filename save all changes to a file with the name we set.

  • w saves the changes made to a file.

  • wq save the changes and close the editor.

  • what! close the editor without saving changes

Previous articleThis smart radio with Android is universal and works for almost any car
Next articleDreame L10s Pro, the robot vacuum cleaner that scrubs, collects, dries and even cleans itself