官术网_书友最值得收藏!

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:

  1. insert mode
  2. 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:

  1. :wq
  2. :x
  3. 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!

主站蜘蛛池模板: 梧州市| 淮安市| 济宁市| 梅州市| 太仆寺旗| 宜君县| 沙洋县| 页游| 尤溪县| 和平县| 育儿| 明水县| 大邑县| 大同市| 醴陵市| 丹阳市| 盘山县| 大港区| 安陆市| 昌图县| 河北区| 乐昌市| 精河县| 红河县| 泌阳县| 九江市| 孝昌县| 长阳| 包头市| 麦盖提县| 全南县| 上杭县| 利川市| 老河口市| 于田县| 冕宁县| 尼勒克县| 桃源县| 内乡县| 渭南市| 阆中市|