Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

4.1.1 Write a simple document


May 23, 2021 That's what Linux should learn



So far, we've got the theoretical basis for writing documents in Linux systems, so let's start writing a simple script document together. Mr. Liu wei will try to mark all the steps and keystrokes, if you forget the role of some shortcut commands, you can go back to the previous review.

The first step in writing a scripted document is to give the document a name, which is named .txt. I f the document exists, open it. If it does not exist, a temporary input file is created, as shown in Figure 4-2.

4.1.1 Write a simple document

Figure 4-2 attempts to write a script document

When you open the .txt, the command mode of the Vim editor is entered by default. You can only execute commands in this mode, not enter text at will, and we need to switch to input mode to write a document.

As mentioned in Figure 4-1, you can switch from command mode to input mode using the a, i, and o keys, respectively. Where the a key and the i key are one bit behind the cursor and the current position of the cursor switch to input mode, while the o key is to create an empty line below the cursor, at which point the a key can be tapned into the editor's input mode, as shown in Figure 4-3.

4.1.1 Write a simple document

Figure 4-3 switches to the input mode of the editor

When you enter input mode, you are free to enter text content, and the Vim editor does not execute the text you enter as a command, as shown in Figure 4-4.

4.1.1 Write a simple document

Figure 4-4 Enter text in the editor

After writing, to save and exit, you must first tap the keyboard Esc key to return command mode from input mode, as shown in Figure 4-5. Then enter: wq! switch to last-line mode to complete the save exit operation, as shown in Figure 4-6.

4.1.1 Write a simple document

The command mode of the Figure 4-5 Vim editor 4.1.1 Write a simple document

Figure 4-6 Vim editor's end-line mode

When you enter the wq! command in last-line mode, it means forcing the document to be saved and exited. You can then view the saved document with the cat command, as shown in Figure 4-7.

4.1.1 Write a simple document Figure 4-7 View the contents of the document

Isn't it easy?! C ontinue editing this document. Because you want to append content below the original text content, it is more efficient to tap the o key into input mode in command mode, as shown in Figure 4-8, Figure 4-9, and Figure 4-10.

4.1.1 Write a simple document

Figure 4-8 writes again through the Vim editor

4.1.1 Write a simple document

Figure 4-9 enters the input mode of the Vim editor

4.1.1 Write a simple document

Figure 4-10 is appended to a line of text

Because the text content has been modified at this point, the Vim editor rejects our actions when we try to exit the document without saving it. This input operation can only be concluded by forcing an exit, as shown in Figures 4-11, 4-12, and 4-13.

4.1.1 Write a simple document

Figure 4-11 attempts to exit the text editor

4.1.1 Write a simple document

Figure 4-12 Refuses to exit the operation because the file has been modified

Now we also have some Vim editor experience, should also feel not as difficult as imagined it. Now look at the contents of the text and find that the appended input has not been saved, as shown in Figure 4-14.

After learning the theory and then writing a text by themselves, do you feel full of achievements now? N ext, three small tasks will be arranged for the reader from shallow depth. In order to thoroughly grasp the use of the Vim editor, we must complete one by one not lazy, if you forget the relevant commands during the completion of these three tasks, you can return to the previous article to further review the master.

4.1.1 Write a simple document

Figure 4-13 Forced exit from the text editor

4.1.1 Write a simple document

Figure 4-14 View the text content that was eventually written