In vim, I introduced the concept of mode in vim and how to move the cursor in VIM in multiple ways. This section describes copying, cutting, pasting, undo/Redo, and search operations. Let's get started!
1. Copy, cut and paste
Copy, cut, and paste are the most common operations for text editing. Vim also has corresponding operations, but their naming methods have changed in VIM: Vim calls cut as delete, copy as Yank, and paste as paste. When you use Vim to edit text, VIM will open a cache in the memory to put the text into it, and all the edits to the text will be written into this cache until: write or: w. Save and edit the result to the hard disk. During the cut operation, VIM deletes the text from the cache and saves it to the Register. During the copy operation, VIM "pulls" the corresponding text from the cache and saves it to the Register.
In normal mode, the commands for the cut (delete), copy (Yank), and paste (paste) operations in Vim are D, y, and P. In vim, you can apply the cut and copy operations to one character, one word, one line of text, and one piece of text respectively. The corresponding commands are:
Operation object | cut | copy
---------- | ------- | ----------
One character | DL | YL (note that it is a lowercase letter L, not a number 1)
---------- | ------- | -----------
One word | DW | YW
---------- | ------- | -----------
One line of text | dd | YY
---------- | ------- | -----------
Text Segment | DAP | yap
---------- | ------- | ----------
For example, to cut a word and paste it to another position, you only need to move the cursor to the word, press DW, then move the cursor to the position to be pasted, and press p. At the same time, VIM provides two methods for pasting:
P (lower case) pasted after the current cursor position
P (uppercase) pasted before the current cursor position
Ii. Undo/Redo
Similar to copying, cutting, and pasting, undo and redo operations are often used to edit text. In vim, press u to perform the Undo operation, and press Ctrl-R to perform the redo operation. Very simple! Theoretically, you can perform unlimited Undo/Redo operations in vim. At the same time, VIM provides some advanced Undo/Redo operations, that is, earlier and later commands. For example, to return to the status four minutes ago, you only need to execute the command: Earlier 4 m. Then I want to move forward to the status 30 seconds later, then execute the command: Later 30 s. The two examples just now use time points for reference. If I want to return to the status before the previous five changes, run the command: Undo 5. vim organizes the Undo list in a tree structure and can be viewed using the command: undolist. In a sense, VIM is not only a text editor, but also a time machine ).
Iii. Search operations
Vim has a powerful search engine built in. When searching, you only need to press/in normal mode, type the word you want to search for, and press Enter. For example, to search for text that contains hello, you only need to execute/Hello <CR>. If the text contains multiple words to be searched, press n to jump to the next place where the word appears, and press n to jump to the first place where the word appears. When searching in Vim, you can set parameters to customize the search method. The following describes three search-related parameters.
Set incsearch if you only fuzzy remember the words you want to search for, you can open incsearch, then Vim searches when you type the words you want to search
Case Insensitive for set ignorecase search
Set smartcase if the words you type to search for contain only lowercase letters, the case is ignored during search. Otherwise, Case sensitivity is taken into account during search.
In addition, if I want to search for a specific word, the escape character \ is required \. To search for the word "hello", run/\ <Hello \> and press Enter. If you search for numbers in the text, run the \/d command. /\ D \ + command to search for one or more numbers. /\ D \ * search for zero or multiple numbers. I want to familiarize myself with regular expressions.
References
1. http://www.swaroopch.com/notes/Vim