- Learn Linux Quickly
- Ahmed AlKabary
- 908字
- 2021-06-11 18:43:47
The vi editor
The nano editor is usually the editor of choice for beginners. It is a great editor, but let's just say that it's not the most efficient editor out there. The vi editor is a more advanced Linux editor with tons of features and is by far the most popular editor among advanced Linux users.
Let's open the facts.txt file with the vi editor; to do that, you run the vi facts.txt command:
elliot@ubuntu-linux:~$ vi facts.txt
This will open the vi editor, as shown in the following screenshot:

Figure 13: The facts.txt file opened in vi
Unlike the nano editor, the vi editor works in two different modes:
- insert mode
- command mode
The insert mode enables you to insert text into a file. On the other hand, the command mode allows you to do things like copying, pasting, and deleting text. The command mode also allows you to search and replace text along with many other things.
Insert mode
By default, you enter command mode when you first open the vi editor, and you can't insert text while you are in command mode. To insert text, you need to switch to insert mode. There are several ways you can use to change to insert mode; Table 6 lists all of them.

Table 6: vi insert mode
You can navigate in the vi editor with your arrow keys, just like you would do in the nano editor. Now navigate to the last line in the file facts.txt and then press the letter o to switch into insert mode. You can now add the line "Linux is cool!"

Figure 14: Adding a line in vi
With insert mode, you can add as much text as you want. To switch back to command mode, you need to press the Esc key.

Figure 15: Switching between Insert Mode and Command Mode
The preceding screenshot illustrates how to switch back and forth between command mode and insert mode.
Command mode
Anything you want to do aside from adding text can be achieved from command mode. There are a whole lot of commands you can use with the vi editor. You may think I am joking, but there are books and courses out there that only discuss the vi editor. However, Table 7 will get you up and running with the vi editor as it lists the most popular commands you can use with vi.

Table 7: vi commands
As you can see, Table 7 has a lot of commands, so I will not go through all of them; that's left for you as an exercise. However, I will discuss some of the commands to help you get going with the vi editor.
Let's start by showing line numbers as it will make our life much easier! To do that, you run the :set number command, as shown in the following screenshot:

Figure 16: Show line numbers
Now let's copy line 4. You want to make sure the cursor is on line 4; you can do that by running the :4 command, as shown in the following screenshot:

Figure 17: Go to the 4th line
Now press the sequence yy, and it will copy the entire line. Let's paste it three times at the end of the file. So navigate to the last line and then press p three times, it will paste the copied line three times, as shown in the following screenshot:

Figure 18: Copying and pasting in vi
Alright! Let's replace the word cool with awesome because we all know Linux is not just cool; it's awesome! To do that, you run the :%s/cool/awesome command, as shown in the following screenshot:

Figure 19: Replace cool with awesome
Let's also replace the word Roses with Cherries because we all know that not all roses are red. To do that, run the :%s/Roses/Cherries command, as shown in the following screenshot:

Figure 20: Replace Roses with Cherries
It will even tell you how many substitutions took place.
COOL TIP
You should know that :%s/old/new will only replace the first occurrence of the word old with new on all the lines. To replace all the occurrences of the word old with new on all the lines, you should use the global option :%s/old/new/g
To understand and make sense of the tip above, add the line "blue blue blue blue" to your facts.txt file and try to use the :%s/blue/purple command to replace the word blue with purple. You will see that it will only replace the first occurrence of blue. To make it replace all occurrences of blue, you have to use the global option
:%s/blue/purple/g.
Saving and exiting vi
Eventually, when you are done viewing or editing a file in vi, you would want to exit the vi editor. There are multiple ways you can use to exit the vi editor, Table 8 lists all of them.

Table 8: Saving and Exiting vi
So let's save our file and quit the vi editor. Of course, you can use any of the following commands:
- :wq
- :x
- ZZ
They all achieve the same result, that is, saving and exiting vi.

Figure 21: Save and exit vi
If you have successfully exited the vi editor, I want to congratulate you because you are one of the elite. There are hundreds of memes and comics on the internet about how some people opened the vi editor, and were never able to exit!
- Android項目開發(fā)入門教程
- Learning ASP.NET Core 2.0
- Spring核心技術和案例實戰(zhàn)
- Selenium Testing Tools Cookbook(Second Edition)
- Terraform:多云、混合云環(huán)境下實現(xiàn)基礎設施即代碼(第2版)
- 利用Python進行數(shù)據(jù)分析
- PHP+MySQL+Dreamweaver動態(tài)網(wǎng)站開發(fā)從入門到精通(第3版)
- Android玩家必備
- CoffeeScript Application Development Cookbook
- Python 3 數(shù)據(jù)分析與機器學習實戰(zhàn)
- 小程序從0到1:微信全棧工程師一本通
- C++程序設計
- 大學計算機應用基礎(Windows 7+Office 2010)(IC3)
- Android智能手機APP界面設計實戰(zhàn)教程
- Java網(wǎng)絡編程實用精解