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

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!

主站蜘蛛池模板: 元朗区| 广饶县| 贞丰县| 英德市| 托里县| 济南市| 江安县| 孟村| 苍南县| 百色市| 贞丰县| 武川县| 秀山| 乌审旗| 勃利县| 张家港市| 宁乡县| 东平县| 钦州市| 徐闻县| 铜梁县| 临沧市| 松滋市| 五大连池市| 项城市| 环江| 资中县| 太白县| 璧山县| 灯塔市| 威宁| 茌平县| 城固县| 金寨县| 黄陵县| 海伦市| 石门县| 临漳县| 青田县| 建水县| 石家庄市|