Learn vi and vim Editor (4): fast moving location
Generally, the first step to edit a text is to move the cursor to the desired location. Therefore, the speed of editing a text depends on the number of buttons required to move the cursor to the destination. This article describes how to move the cursor quickly.
Move by screen:
In a file with thousands of lines of text, moving by line may be very inefficient. In this case, you can follow the screen to move. You can think of a file as a long scroll with text, while the screen is a window that usually shows 24 lines of text (26 lines of mine ).
^ F: scroll forward (toward the end of the text) to a whole screen;
^ B: Scroll back to the beginning of the text to display a whole screen;
^ D: Scroll toward the half screen;
^ U: Scroll back to the half screen;
^ E: Scroll one row forward;
^ Y: Scroll down a row;
^ F indicates the crtl key + f key. If you want to scroll the screen forward or down, but want to keep the cursor in the original text line, you can use the z command:
Z + enter: move the cursor to the top of the screen and scroll the screen;
Z.: move the cursor to the center of the screen and scroll the screen;
Z-: move the cursor to the bottom of the screen and scroll the screen;
For example, if you want to keep the cursor in the row at the bottom of the screen, but move the row to the top of the screen, enter z and enter, as shown in:
The cursor is on the last line of the screen:
Vcbbxlu12tk70ncjuw.vcd4kpha + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20141031/2014103109134346.png" alt = "\">
Therefore, it does not make sense to repeat the same z command, because the first execution has reached the goal, and executing the same command has no effect. The z command can accept numeric parameters. For example, z + enter moves the current row to the top of the screen, but 200z + enter moves the 200th lines of the file to the top of the screen.
When some messages appear on the screen and affect work, you can scroll the screen to eliminate these messages, or you can directly enter the crtl + L key to re-draw but not scroll the screen. :
After entering the ctrl + L key, you can re-draw the screen, prompting the message to disappear:
To keep the current screen view, run the following command to move it within the screen range:
H: Move the row to the top of the screen;
M: The row that moves to the center of the screen;
L: Move the row to the bottom of the screen;
NH: n rows down to the top of the screen;
NL: Move to line n at the bottom of the screen;
As shown in the following figure, the cursor is first in the current screen view:
After the M key is entered, the cursor moves to the middle line of the current screen, and the screen view does not change:
Move by line:
I have already talked about the "j" and "k" commands that move in the unit of action. Here are several additional commands:
Enter: Move to the first character of the next line;
+: Move to the first character of the next line;
-: Move to the first character of the previous line;
After the preceding three commands move to the target line, the cursor is placed in the first non-empty character of the line. To move data in the current line, run the following command:
^: Move to the first non-space position of the current row;
N ": column n to the current row;
Move based on text blocks:
In the vi editor, you can move text blocks (words, sentences, paragraphs, or sections. We have already talked about moving by words (w, W, B, B). Next we will introduce several commands for moving the cursor based on text blocks:
E: Move to the end of a word;
E: Move to the end of a word and calculate the punctuation marks;
(: Move to the beginning of the current sentence;
): Move to the beginning of the next sentence;
{: Move to the beginning of the current paragraph;
}: Move to the end of the next segment;
[[: Move to the beginning of the current section;
]: Move to the beginning of the next section;
Vi will look "? .! "Punctuation marks are used as the end of a sentence. When these punctuation marks are followed by at least two spaces or the last non-space character in a line, vi uses them as the end of the sentence. However, in practice, I found that as long as these punctuation marks are followed by a space, vi will end the sentence as a sentence. I don't know if it is wrong in the book, or where can I configure it? Write down this question for now.
The definition of a paragraph refers to the text before the next blank line or the text before the paragraph macro. The definition of a section is the text before the next macro. You can use the: set command to customize these macros that are used as segments or segments separators.
Move text based on search results:
When moving large files, one of the quickest ways is to move them based on the search results. The search command is a special character "/" in the format of/patten. The mode can be a complete word or a series of strings. If a space is added to the search mode, the space is also part of the word, of course, you can also use more powerful pattern matching syntax. Vi searches from the current position of the cursor and automatically redirects back the start of the file or the bottom of the file as needed. If it is found, the cursor locates the first place in the search mode, otherwise, the corresponding information is displayed at the bottom of the screen.
/Patten: search forward from the cursor position;
? Patten: Search back from the cursor position;
The last search mode will be left in your edit session, so you can repeat the search without re-entering the original search mode:
N: Repeat the search in the same direction;
N: Repeat the search in the opposite direction;
/Enter: Repeat the search;
? Enter: Search again later;
When you repeat the search, the system automatically bypasses the file header or end of the file by default. Therefore, you can set the wrapscan option in the vi editor to control whether to repeat the search. When you enter the command: when you set nowrapscan, you can cancel the search and bypass the file header or end. When the search fails, the following information is displayed:
If you fail to search back, the following information is displayed:
The search command can be used together with the Modify text command. That is to say, in the form of (command) (TextObject), the search command can appear in TextObject, while command represents the text modification command, such as c and d. For example, d? File is to delete all the characters that appear file for the first time from the cursor position to the back search.
You can use the following command to search in the current line:
Fx: searches (moving the cursor at the same time) Where x appears next to the row, and x represents any character;
Fx: Search (move the cursor at the same time) the place where x appears before the row;
Tx: Search (move the cursor at the same time) the first character in the place where x appears next to the row;
Tx: Search (move the cursor at the same time) The next character in the place where x appears before the row;
;: Repeat the previous search, with the same direction;
,: Repeat the previous search, in the opposite direction;
These commands, coupled with numerical parameters, represent the nth occurrence of the x character. These commands are useful when deleting or dragging. When the text block to be modified has multiple punctuation marks and it is difficult to count the number of words, it is very convenient to use character search and text modification commands.
Move by row number:
The row number is very useful to programmers, because it often tells us which line of error is returned during compilation. It is very convenient to directly move to the line in the source file. The: set nu option is used to display the row number of the file.
The CTRL + G command displays the corresponding information at the bottom of the screen, including the current row number, the total number of lines of the file, and the percentage of the current position in the full text. As shown in:
G command: adding the G command to the numeric parameter means that the parameter is directly moved to the corresponding specified line. If no row number is specified, the G command moves directly to the end of the file.
Enter two quotation marks ('') to return to the original position (the last time the G command was used ). If you edit the cursor in the middle, the two backquotes move the cursor back to the last edited position. If you use the search command, ''can move the cursor back to the position of the last search. "'Is similar to'', except that it moves the cursor back to the beginning of the row where the last position was located, rather than the exact position.
Moving the cursor by line number is a quick way to move the cursor in a large file.
Summary of the vi editor commands in this chapter:
^ F, ^ B, ^ D, ^ U, ^ E, ^ Y, z + enter, z ., z-, ^ L, H, M, L, enter, +,-, ^, n |, e, E ,(,),{,}, [[,],/pattern ,? Pattern, n, N ,/,?, Fx, Fx, tx, Tx,;, G, '','', ^ G.