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

4.1 Vim text editor


May 23, 2021 That's what Linux should learn



Whenever I encounter a knowledge point in my lecture that students need to remember, I suddenly raise my voice in order to get them to get up, so there's a saying they remember especially deeply: "Everything is a file in a Linux system, and configuring a service is modifying the parameters of their profile." A nd in the day-to-day work, we certainly have to write documents, these work is done through the text editor. The purpose of Mr. Liu's book is to give the reader a real grasp of how Linux systems work, rather than just staying on the "will use some operating system" level, so we chose to use the Vim text editor, which is installed by default on all current Linux operating systems and is a great text editor.

Vim is recognized by the majority of manufacturers and users because the Vim editor has three modes - command mode, end-line mode and edit mode, each mode supports a number of different command shortcuts, which greatly improves productivity, and users will feel quite easy after getting used to it. To manipulate text efficiently, you must first figure out the operational differences between the three modes and how to switch between them (see Figure 4-1).

Command mode: Controls cursor movement to copy, paste, delete, and find text.

Input mode: Normal text entry.

End mode: Save or exit the document, and set up the editing environment.

4.1 Vim text editor

Figure 4-1 How to switch between Vim editor modes

Each time you run the Vim editor, you go into command mode by default, switching to input mode before writing a document, and returning to command mode each time you finish writing the document before going into last-line mode to save or exit the document. I n Vim, you cannot switch directly from input mode to end mode. There are hundreds of uses for commands built into the Vim editor, and to help readers master the Vim editor faster, Table 4-1 summarizes some of the most commonly used commands in command mode.

Commands commonly used in Table 4-1 Vim

Command role

dd Delete (cut) the entire line where the cursor is located

5dd Delete (cut) 5 lines from the cursor

yy copy the entire line where the cursor is located

5yy copies 5 rows from the cursor

n Shows the next string to which the search command is located

N shows the last string to which the search command was located

u Undo the last action

p Paste data that was previously deleted (dd) or copied (yy) behind the cursor

End-line mode is primarily used to save or exit files, as well as to set up a work environment for the Vim editor, and to allow users to execute external Linux commands or jump to a specific number of lines of documents they are writing. T o switch to last-line mode, enter a colon in command mode. The commands available in the last row mode are shown in Table 4-2.

The commands available in Table 4-2 last row mode

Command role

:w Save

:q Exit

:q! Force exit (discard modifications to documents)

:wq! Force save exit

:set nu displays the line number

:set nonu does not display line numbers

: Command Executes the command

: The integer jumps to the line

:s/one/two replaces the first one of the row on which the current cursor is located with towo

:s/one/two/g replaces all ones in the row where the current cursor is located with towo

:%s/one/two/g replaces all ones in the full text with two

? String Searches the text from bottom to top for the string

/String Searches the text from top to bottom for the string