Learn VI and VIM editor (4): fast moving position

Source: Internet
Author: User

In general, the first step to editing text is to move the cursor to where you want to edit it, so the speed of editing the text depends in part on the number of keys required to move the cursor to the destination. This article mainly describes how to quickly move the cursor.

To move according to the screen:

in a file with thousands of lines of text, it can be very inefficient if you move by line. You can follow the screen to move it now. You can think of a file as a long reel with text, while a screen is a window that typically displays 24 lines of text (my display is 26 lines).

^F: Scroll forward (toward the end of the text) a full screen;

^B: Scroll a full screen backward (toward the beginning of the text);

^d: rolling forward half screen;

^u: Scroll backward half screen;

^e: Rolling forward one line;

^y: Scroll backward one line;

where ^f represents the CRTL key +f key. If you want to scroll the screen forward or down, but want to keep the cursor in the original line of text, you can use the Z command:

Z+enter: Moves the cursor to the top of the screen and scrolls 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, the current cursor is at the bottom of the screen, and now you want the cursor to remain on the line, but to move the line to the top of the screen, you can enter z directly, and enter the ENTER key, as shown in:

The cursor is on the last line of the screen:


After entering the z enter key, the line of the cursor moves to the first line of the screen:


So you'll find that repeating the same Z command doesn't make sense, because it's done after the first execution, and then executing the same command has no effect. The z command can accept numeric arguments, such as Z+enter, which moves the current line to the top of the screen, but 200z+enter moves the line 200 of the file to the top of the screen.

When some messages appear on the screen and affect work, you can either scroll the screen to dismiss the messages, or you can enter the Crtl+l key directly to redraw the screen without scrolling. :


After you enter the Ctrl+l key, you can redraw the screen, prompting the message to disappear:


If you need to keep the current screen view, you can move through the screen range with the following command:

H: Move the line to the top of the screen;

M: Move to the center of the screen Line;

L: Move to the bottom line of the screen;

NH: Move to the top of the screen to the nth row;

NL: Moves to the bottom of the screen up to the nth row;

The effect is as shown, at first the cursor is in the first line of the current screen view:


After entering the M key, the cursor moves to the middle row of the current screen, and the screen view does not change:


Move by line:

The two commands, "J" and "K", which were moved by the behavior unit, are supplemented by several commands:

Enter: Move to the first character of the next line;

+: Moves to the first character of the next line;

-: Moves to the first character of the previous line;

when the above three commands are moved to the destination line, the cursor is placed on the first non-null character of the line. To move through the current line, you can use the following command:

^: Moves to the first non-blank space of the current line;

n|: Moves to the nth column of the current row;

To move according to a text block:

The VI Editor can be moved in blocks of text (words, sentences, paragraphs, or sections). I've already talked about moving by word (w,w,b,b), and then I'll introduce a few commands to move the cursor based on a block of text:

e: Move to the end of the word;

e: Move to the end of the word and count the punctuation;

(: Move to the beginning of the current sentence;

{: Move to the beginning of the current paragraph;

}: Move to the end of the next paragraph;

[[: Move to the beginning of the current section;

"]: Move to the beginning of the next section;

VI will look for "?. ! "such as punctuation as the end of the sentence. When the punctuation is followed by at least two spaces or as the last non-whitespace character of a line, vi ends it as a sentence. But I actually found that, as long as these punctuation marks followed by a space, VI will be the end of the sentence, do not know is not the book is wrong, or where can be configured? Let's write down this question for the time being.

The definition of a paragraph refers to the text before the next blank line, or to the text that appears before the paragraph macro. The definition of a subsection is the text before the next section macro. You can customize these macros that are treated as segments or section separators by using the: Set command.

To move text based on the results of the search pattern:

one of the quickest ways to move through a large file is to move it based on the results of the search pattern. the Search command is a special character "/" in/patten format. A pattern can be a complete word, or a string of strings, and if a space is added to the search pattern, the space is also part of the word and, of course, more powerful pattern-matching syntax can be used. VI starts the search from the cursor's current position and automatically moves back to the beginning of the file or the bottom of the file as needed , and if it does, the cursor will be positioned where the search pattern first appears, otherwise the information will be displayed at the bottom of the screen.

/patten: Search forward from the beginning of the cursor position;

? Patten: Search backwards from the beginning of the cursor position;

The last search mode is left in your edit session, so you can repeat the search without having to reenter the original search mode:

N: Repeat search in the same direction;

N: Repeat the search in the opposite direction;

/enter: Repeat the search forward;

? Enter: Repeat search backwards;

because the default is to automatically wrap around the header or end of the file when you repeat the search, you can control whether the search is going around by setting the Wrapscan option of the VI editor, when you enter the instruction : When set Nowrapscan, you can bypass the file header or end of the file when you cancel the search. When we fail the search forward, the following message is displayed:


If the search fails, the corresponding information appears as shown:


The search command can be used in conjunction with the Modify Text command. That is, for the use of (command) (textobject), the Search command can appear in Textobject, while the command represents a text modification command, such as C,d. For example, D?file is the deletion of the cursor location to the back search for the first time all characters of file appear.

You can search in the current line with the following command:

FX: Search (while moving the cursor to) the next occurrence of X in the bank, where x represents any character;

Fx: Search (at the same time move the cursor to) the place where X appears before the bank;

TX: Search (while moving the cursor to) the first character of the bank where the next occurrence of x occurs;

Tx: Search (while moving the cursor to) the last character of the line where X appears before the bank;

;: Repeat the previous search in the same direction;

,: Repeat the previous search, in the opposite direction;

These commands, coupled with numeric parameters, represent the place where the search x characters appear in the nth time. These commands are useful when deleting or dragging, and it may be convenient to combine text-editing commands with character searches when there are multiple punctuation marks in a text block that needs to be modified, and it is difficult to count the number of words.

To move by line number:

line numbers are very useful to programmers , because compiling an error usually tells us what line of error it is, and it can be very handy if you can move directly to that line in the source file. Use the: Set NU option to display the line number of the file.

The ctrl+g command displays the appropriate information at the bottom of the screen, including the current line number, the total number of rows in the file, and the current position as a percentage of the full text. As shown in the following:

G command: The numeric parameter plus the G command represents a direct move to the corresponding specified line. If no line number is specified, the G command moves directly to the end of the file.

Enter two anti-quotes (') to return to the original position (the position of the last use of the G command). If you do an edit operation in this middle, two anti-quotes move the cursor back to the last edit position. If you use the Search command, you can move the cursor back to the location of the last search. "and" functions similarly, except that it moves the cursor back to the beginning of the row where the last position was, not the exact position.

Moving the cursor according to the line number is a quick way to move in a large file.

This chapter VI editor commands summarize:

^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.


Learn VI and VIM editor (4): fast moving position

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.