Usage of VIM

Source: Internet
Author: User
Tags diff uppercase letter

    VI Common usage
description, all commands do not include "", If "" "(half-width double quotes), is a command character, please note the distinction.

Insert:
"I" starts before the cursor position
the "I" cursor moves to the beginning of the current line and then inserts text before it
Append new text after the "a" cursor is currently located
the "A" command will move the cursor to the end of the line where the
"O" Inserts a new row below the current line
"O" inserts a new row above the current line

one. Mobile
(1). The command to move the cursor by word
in VI, "word" has two meanings: one is the generalized word, it can be any content between two spaces.
such as: {int k; it has only 2 characters, one is {int, the other is k;
The other word is the narrow word, in this sense, English words, punctuation and non-alphabetic characters (such as $,%, ^, &, *, (,) 、-、 +, {,}, [,], ~, |, \, <, >,/etc.) are treated as
a word. The above line includes {, int, K, 4 words.
The use of UPPERCASE commands in VI generally refers to the word as a generalized treatment, the use of lowercase commands is treated as a narrow sense.
1. W and w command: Move the cursor to the beginning of the next word, the difference is: w command, move the cursor to the next word (narrow) of the head, the W command, will move the cursor to the next word (generalized) of the first word.
2. E and E command: If the starting position of the cursor is within a character (that is, not at the end of the word), the command will move the cursor to the end of the word, or the command will move the cursor to the end of the next word if the starting position of the cursor is at the end of the word.
3. B and B commands: If the cursor is in the same word (that is, not the head), the command will move the cursor to the beginning of the word, and if the cursor is at the top of the word, the command will move the cursor to the beginning of the previous word.

(2). The command to move the cursor by sentence
in VI, a sentence is defined as a comma (,), a period (.), a question mark (?), and an exclamation point (!), followed by at least two or more (including two) spaces (a foreigner's habit of using a typewriter) or a line break character sequence. There is a great discrepancy between this definition and the usage of habits, and attention to use.
1. (Command: Move the cursor to the beginning of the previous sentence;
2.) Command: Move the cursor to the beginning of the next sentence.

(3). The command to move the cursor by Segment
in VI, a segment is defined as a fragment that begins and ends with a blank line.
1. {command: Move the cursor forward to the beginning of the previous paragraph;
2.} command: Move the cursor back to the beginning of the next paragraph.

(4). Scroll-screen command
Ctrl+u the screen forward (file header direction) tumbling half screen; Hint: U means up;
ctrl+d flips the screen backward (the end of the file) half-screen. Hint: D means down;
You can add a number n before these two commands, and the screen rolls n rows forward or backward. And this value is remembered by the system, and then the corresponding number of rows is rolled back with the Ctrl+u and Ctrl+d commands.
ctrl+f the screen to the end of the file tumbling a full screen (that is, a page); Hint: F denotes forward;
ctrl+b the screen to the top of the file, a full screen (i.e. one page). Hint: b means backward;
You can also add a number n before these two commands, then move the screen forward or backward by n pages;
Ctrl+e Scrolls the screen up one line;
Ctrl+y Scrolls the screen down one line.

(6). Move to the beginning or end of the line
the "$" command moves the cursor to the end of the current line line. If you have a keyboard on theKey, you can also complete the same function.
The "^" command moves the cursor to the first non-null character of the current line,
The "0" command moves to the first position in a row, and the key also accomplishes the same function.
The "$" command accepts the number prefix. 1$: Moves to the end of the line at the current line; 2$ means: Moves to the end of the line at the next line;
"0" and "^" Do not accept number prefixes.

(7). Move to a specific character
The "FX" command looks forward to the single character x in the bank and moves the cursor over the character X. Tip: "F" means "Find".
The "FX" command is used to find the reverse version of the "FX" command to the left.
The "TX" command moves the cursor to the previous character of the target character. Tip: "T" means "to".
Reverse version of "TX" "TX" command, moved to the back of the character.
You can prefix these four commands with a number.
The "FF" command can be passed ";" command is repeated, the "," command is used for reverse repetition. Using the "TT" command will cause the correct and foolish question.
(8). Bracket Matching
"%" is a very handy command that can match a pair of parentheses (i.e. "()", "[]", "{}").
If the cursor is on the (, it moves to the corresponding ")", and conversely, if it is on ")", It Moves to "(").
When the cursor is not on a useful character, "%" will first find one, and then it will move to its matching position.
(9). Move to a specific line
The "G" command moves the cursor to the end of the text;
The "GG" command moves the cursor to the top of the text;
The "NG" command moves the cursor to the first white-space character on the nth line, using ": Set Number" To display line numbers, and ": Set Nonumber" To turn off the display;
The "n%" command moves the cursor with a percentage;
The "H" command moves the cursor to the first line of the screen;
The "M" command moves the cursor to the middle line of the screen;
The "L" command moves the cursor to the last line of the screen;
Hint: "H" means "Home", "M" means "middle" and "L" means "last".
(10). Use Mark (Mark)
When you jump to another place with the "G" command, Vim remembers where you jumped from, and this position becomes a mark. There are also the Find commands: "/" and "?", while the "FTWEHJKL" command is not.
To return to the original place, use the following command: "(two inverted quotes, above);
If you execute this command again you will jump back to the original place;
Ctrl-o command jumps to an "older" place (hint: o denotes older);
The Ctrl-i command jumps to a "newer" place (hint: I only rely on the O on the keyboard);
The "MX" command can mark the current cursor position, and x can be any lowercase letter (A-Z);
The ' x ' command jumps to the tag you defined, and x is the tag you defined with the MX command;
"' X" (single quotation mark plus one marker) jumps to the beginning of the line where the marker is located, noting that it differs from the previous command.

Copy, delete, move

(i). Delete function
"DD"--delete one line;
"D"--Delete to the end of the line, i.e. "d$";
"X"--Removes the character of the cursor, or "DL";
"X"--Removes the character to the left of the cursor, i.e. "DH";
"DW"--deletes from the current position to the beginning of the next word;
"Daw"--delete the words on the cursor (including whitespace characters);
"DG"--Delete to the end of the text;
"DGG"--Delete to the head of the text;

(b). Modify the text
The "C" command represents the change, which is changed. Its function is similar to the "D" operator, except that it switches to insert mode when it is finished;
"CW"--Modify a word;
"C"--revise to the end of the line, that is, [C $];
"S"--modifies a character, i.e. "CL";
"S"--Modify a whole line, namely "CC";

(iii). Repeat a change
“.” is a very simple and useful command in VIM.
It repeats the last modification operation except the "U" (undo), Ctrl-r (Redo), and colon commands.


(iv). Visual mode
Start the visual mode with the "V" command: Move the cursor to the desired location and the selected text will be highlighted. Finally execute the "operator" command, such as delete the command "D", copy the Command "Y".
With the "V" command, the entire row is selected, "H", "L" will not work.
Use the "Ctrl + V" command to process text within a rectangular block. When using block visual mode, there will be Four Corners, "o" command to move the cursor to the diagonal, while "o" can be moved to the same side of the corner.
Tip: "~" To change the case of the selected text;
"U" all becomes lowercase;
"U" all becomes uppercase.
"J" merges the selected rows.

(v). Copy text
Cut: When you delete text with "D", "X" or other commands, the text is saved. Then use the "P" command to paste the text again (hint: p means put).
Copy, paste: Use the "y" command first, and then paste it with the "P" command. Tip: "Y" is yank.
"YY"--copy one line;
"Y"--also when copying a whole line;
"Y$"--copied to the end of the line;
"YW"--copy a word. These can all be prefixed with a number;
Tip: Use Visual mode to select text, simple and intuitive.
Tip: "Y" "P" can be different files.

Copying files using registers
The register is where vim holds the text. Represents a register with a lowercase letter.
For example: "Fyas", copy a sentence to register F.
"FP", using register F.

Edit Multiple Files

(i). Edit another file
": Edit XXX": Close the current file, open another;
": edit! XXX ": Do not save the current file, open another;
": Hide Edit xxx": Edit another file, but not immediately save the current file, just hide it;
(b). Open multiple files at the same time
"Vi xxx1 xxx2 xxx3", open three files;
": Next" ": next!" ": Wnext", edit the next file;
":p revious" ":p revious!" ": wprevious", edit the previous file;
Hint: You can prefix a number.
(iii). Quick Jump in Files
(In the English-American keyboard, ^ on the top of 6): Jump to the previous file, and then use this command will jump back;
"'": Jump to the location of the last time you left this file;
"'." : Jumps to the location of the last modified file;
Use file tagging: "MX" and "' X" commands, and jump with Ctrl+i and ctrl+o;
Note: The X here must be an uppercase letter, representing the global.

Split window

(i).
1. Split window
Horizontal Split Vertical Split
: Split:vsplit
: Split xxx:vsplit XXX
: new:vnew

2. Close the window
Method 1, ": Quit" and ZZ;
Method 2, ": Close" command, it can avoid leaving vim when the last one is left;
Method 3, ": Only" command, close all other windows;

3. Change the window size
To enlarge the window: "+";
To narrow the window: "-";

4. Jump between windows
Ctrl-w W is used to bounce between windows
Ctrl-w h jump to the left window
Ctrl-w J jump to the window below
Ctrl-w K jump to the upper window
Ctrl-w L jump to the right window

5. Show differences with Vimdiff
Method 1: Start from the shell, for example: Vimdiff main.c~ main.c
Method 2: Start the diff mode from VI: ": vertical diffsplit xxx", if you do not use "vertical", you will split the screen horizontally.
Method 3: If you have a patch or diff file: Edit the file first, and then tell the Vim patch file the name. For example:
: Edit MAIN.C
: Vertical Diffpatch main.c diff
Make big changes

Describes how to do multiple changes and how to change a lot of text

1. Record and Replay commands
Method 1. Use the "." The command repeats the previous modification operation, but it does not complete the complex function;
Method 2. Using the Record command, proceed as follows:
1. The "q{register}" command initiates a keystroke record, and the result is saved to the register specified by the {register}. The register name can be represented by any letter in a to Z.
2. Enter your command;
3. Type "Q" (not followed by any characters) to end the record;
4. Execute the macro with the "@{register}" command;
Tip: the "@@" command repeatedly executes the last macro.

2. Use, modify Register
The register used to record the operation is the same as the register you used to copy the text.
How to use:
G moves to the end of the line (note: It is easier to use the new command to open the window)
o Create a blank line
"NP copy n register the text, your command will be copied to the end of the entire file
{edits} modify these commands as you would modify plain text
0 back to the beginning of the line
"ny$ Copy the correct command back to the N register
DD Delete temporary rows

3. Extending a Register
The record to an uppercase register represents the extended command to the corresponding lowercase register.
Replace

The ": Substitute" command (which can be abbreviated as ": S") allows you to perform string substitution in successive lines.
Format: ": [Range]s/old/new/[flags]"
The description is as follows:
1. Use "n,m" to indicate rows from nth to M; If you do not specify a row range, the ": S" command only works on the current line.
2. "." Represents the current row, "$" means the last line; "%" means that the command is on all lines, "1,$".
3. You can use the "+-" number, such as "n+x,m-y", NM can be "." and "$".
4. Use the visual mode to set the range; Note: The command is scoped to the row, not the selected range.
5. By default, the command only works on the first matching point in a row, so that all matching points in the row need to be labeled with a "g (global)" tag;
Other Tags: "P" (print, printing), used to print out all modified lines when the command is executed;
"C" (confirm, confirm), asking you if you need to replace it before each replacement.

Insert File
": Read {filename}" command reads into the file, inserting below the current line;
": nread {filename}" command inserts the line number of the row with "n" pointing, where the number "0" means the beginning of the file, "$" means the end of the file;

Preservation Department Branch
To write a portion of a line to a file, you can use the ": Write" command;
It writes the full text when there is no specified range, and it writes only the specified range of rows when the range is specified;
If the file already exists and you must write it, add an exclamation mark;
CAUTION: "!" Must follow the ": Write", the middle can not leave a space, otherwise this will become a filter command;
": Write >>{filename}" is used to append content to the file;

Using external programs
The command "{Motion}{program}" filters a block of text with an external program.
In other words, it executes an external command specified by {program} and uses a block of text as input.
Then use the program's output instead of the selected text block.

Recovering from a crash

Method 1. Know the file name, you can use the "-r" option to start vim: "Vim-r xxx";
Method 2. Do not know the file name, into the original folder execution: "Vim-r" ";
Method 3. Use "Vim-r" to list all the swap files it can find, and then use Method 1;

Folded


Collapse command:
Zff-old creation (Create folding)
Zoo-pen a fold (open fold)
Zcc-lose a fold (off folding)
ZR Open nested collapse ("ZR" for all open)
ZM off nested folding ("ZM" for all off)


Move between code

Using tags
1: Create tag file: "Ctags {filename}", or "ctags-r";
2: Move the cursor to the mark of the word you want to view, then use the command;
3: With return;
Tip: "Ctrl-w" is viewed with a new open window;


Move between code
"[]" jumps up to the end of a function, "[" moves down to the end of a function;
"[[" jumps up to the beginning of the function, "]]" jumps down to the end;
Note: These four commands require a "{" "}" symbol in the first column;
"[}" jumps to the beginning of the current code block {} and "[}" jumps to the end of the current code block {};
"]{" "}" "[(" "[]" ""] (""]) "" "[/" "]/" also has a similar function, just matches "()" "/*"

The "[I" command looks for identifiers under the cursor, not only in the current file, but also in all the included files;
"[" Jumps to the first item, can be;
"n[" jumps to nth item;
"]i" lists only the items below the cursor

"[D" looks for only the macros defined by the "#define",
"]d" lists only the items below the cursor
"GD" is only found in the current file and jumps to the place where it was first used;
"GD" is only found within the current function, note that the starting symbol "{" Of the function is required in the first column;

Usage of VIM

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.