Vi is one of the most popular text editors in Linux. Thousands of developers prefer it because of its lack of interface. You can focus on writing code without any distraction. However, you can take advantage of all of its power by learning the commands to interact with your written code.
The first time you execute the text editor Vi, you will see a minimalist interface. It only shows a window and the cursor location. You can also see the name of the file as well as the characters number in the bottom, nothing else. Raspberry Pi text editor works similarly, so it’s time to learn about Vi commands.
Vi commands
The Vi text editor has two working methods. The Edit or Insert Mode lets you add or edit text or code lines in an already existing document. You can’t use the search command here. The Command Mode lets you move around the text and modify the text or code lines. You can perform searches and it’s the default Vi mode when you open the program. In order to switch modes you have to press the Esc key.
Another important aspect when using Vi is the way it works. When you edit a file, the changes are not made on the file but on a copy in the device memory. The changes are only registered on the memory contents.
Vi commands in Command Mode are made of one or two letters and a number. In order to use these commands you need to previously add “:”. Sometimes you have to execute the commands by pressing Enter. Here you will find a list of the most used commands in Vi to make the most of this code editor.
Move around the file in Vi text editor
You can use the arrow keys to move around the document but there is also the possibility of using letter keys to move.
- h moves the cursor right. You can also use the Back utton.
- l moves the cursor left. You can also use the Space Bar.
- j moves the cursor one line up.
- k moves the cursor one line down.
- w moves the cursor between words after it.
- b moves the cursor between words before it.
- e moves the cursor to the end of a word.
- ^ moves the cursor to the beginning of the line.
- $ moves the cursor to the end of the line.
- Enter moves the cursor to the next line.
- H moves the view to the upper part.
- L shows the last line.
- M moves the cursor to the middle of the file.
Modify text with the Vi editor
If you want to modify character, words and complete lines, Vi requires to introduce the following commands:
- s substitutes the character where the cursor is located.
- r replaces the character where the cursor is.
- cw at the beginning of a word to substitute it.
- cc at the beginning of the line where we want to replace text.
- To split a line you move the cursor to the location and press R and Enter.
- To join two lines in one, we move the cursor to the end of the upper line and press J.
Insert text
In order to insert text before or after the cursor or at the beginning or end of a line, there are specific commands.
- I inserts text at the beginning of a line.
- A inserts text at the end of a line.
- i inserts texts before the cursor.
- a inserts text after the cursor.
- O inserts a line above the cursor position.
- o inserts a new line below the cursor position.
Erase text with Vi editor
You can also erase characters and lines with certain commands in Vi.
- x erases the character after the cursor.
- X erases the characters before the cursor.
- dw eliminates the word after the cursor.
- dd eliminates all the line where the cursor is located.
- D erases all the line right from the cursor.
- d0 erases the content of the line left of the cursor.
- dG erases all lines from the cursor onwards.
- d1G erases lines all lines above the cursor.
Cut, copy and paste
In Vi text editor you must put the cursor at the beginning of the first line and write the command y2y to copy the two lines after the cursor. You have to replace 2 for the number of lines we want to copy.
If you want to move several lines, you need to type the command d2d. This cuts two following lines from the cursor. Replace number 2 if you need to cut more lines.
In order to paste the copied lines, you have to place the cursor where we want to paste them and press p to paste after the cursor or P for the location before.
Search and replace
Vi text editor lets you search in a file. You have to input the symbol / after the text we are searching for and press Enter. If the document has several results you can navigate through them using n and N for next and previous coincidence respectively. To replace words or a string in code, use the following command.
- :g/objective text/s//new text/g
Undo changes in Vi text editor
If you haven’t saved the changes in the file you can undo them. Use the following commands:
- u to undo the last executed command.
- U to undo all changes made in a line. You have to use this command in the line to revert.
Mix two files
Through different commands, Vi lets you merge two files in one. To do it you have to place the cursor on the position we want to add content from other file and input:
- :#r name of the command we want to merge
Save files
All the changes you make in a Vi file are stored in the memory. From there we have to take them into a file using the following orders:
- w name of the archive in order to save all changes with the name we set.
- w saves the changes made in a file.
- wq saves the changes and closes the editor.
- q! closes the editor without saving the changes.