Vim usage notes

Source: Internet
Author: User
Tags repetition

The followingArticleThe use of the vim editor is summarized and comprehensive.
Vim Study Notes

1. Display row number information
Instant commands
: Set nu
Permanent command
Echo "Set Nu"> ~ /. Vimrc

2. Two documents for one window operation
: SP filename Vertical Split Window
: Vert SP filename Horizontal Split Window
CTRL + w j/K move cursor between upper and lower windows
CTRL + w h/l move cursor between left and right windows

3. Access VI commands
VI filename: open or create a file and place the cursor at the beginning of the first line
Vi + N filename: open the file and place the cursor at the beginning of line N
Vi + filename: open the file and place the cursor at the beginning of the last line
Vi +/pattern filename: open the file and place the cursor at the first string matching pattern
VI-r filename: a system crash occurred when VI was being edited last time, restoring filename
VI filename... filename: open multiple files and edit them one by one.
4. move the cursor
H or backspace: move the cursor one character to the left
L or space: move the cursor one character to the right
K or Ctrl + P: move the cursor up a row
J or enter or Ctrl + N: move the cursor down a row
W: move the cursor one word to the beginning
W: move one word to the beginning of the word to the right of the cursor. Ignore punctuation marks.
B or B: move one word left to the beginning of the word
E or E: move one word to the end of the word to the right of the cursor.
): Move the cursor to the end of the sentence.
(: Move the cursor to the beginning of the sentence.
}: Move the cursor to the beginning of the paragraph
{: Move the cursor to the end of the paragraph
NG: move the cursor to the beginning of line N
N +: move the cursor down n rows
N-: Move n rows above the cursor
N $: move the cursor to the end of line N
H: move the cursor to the top line of the screen
M: move the cursor to the middle line of the screen
L: move the cursor to the last line of the screen
0: (Note the number is zero) move the cursor to the beginning of the current row
$: Move the cursor to the end of the current row
5. Screen tumble commands
CTRL + u: half screen at the beginning of the file
CTRL + D: Flip the half screen to the end of the file
CTRL + F: Flip the screen to the end of the file
CTRL + B; open a screen at the beginning of the file
NZ: Rolls row n to the top of the screen. If n is not specified, the current row is rolled to the top of the screen.
6. Insert text commands
I: Before the cursor
I: at the beginning of the current row
A: After the cursor
A: At the end of the current row
O: open a new row under the current row
O: open a new row above the current row.
R: Replace the current character
R: Replace the current and subsequent characters until you Press ESC.
S: Starting from the current cursor position, replace the specified number of characters with the input text
S: delete a specified number of rows and replace them with the input text
NCW or NCW: modify a specified number of characters
NCC: modifies a specified number of rows.
7. delete commands
Ndw or ndw: n-1 characters starting at and following the cursor
Do: Delete to the beginning of a row
D $: Delete to the end of the row
NDD: Delete the current row and the next n-1 row
X or X: delete a character. x deletes the character after the cursor, and X deletes the character before the cursor.
CTRL + u: delete text entered in input mode


Operator + scope = command
Operator
D cut
Y replication.
P paste, Which is used with D and Y. The last d or y data can be placed in the row and column where the cursor is located. P: paste the cut data to the next row of the current row, P: paste the data to the previous row of the current row; $ P: paste the cut data to the end of the current row, 0 p: paste the data to the beginning of the current row. The paste operations to the beginning or end of the row correspond to the cut or copy operations: d $ (cut to the end of the row), D0 (cut to the beginning of the row ), Y $ (to the end of the row), y0 (to the beginning of the row ). C modification, similar to the group and of Delete and insert. Delete A Word Group, sentence, and other materials, and insert new materials.
Scope
E is the last character of the string from the cursor position.
W is the first character from the cursor location to the next string.
B is the first character from the cursor position to the previous string.
$ From the cursor location to the last character of the row.
0 is the first character of the row from the cursor position.
8. search and replace commands
/Pattern: Search for pattern from the beginning of the cursor to the end of the file
? Pattern: Search for pattern from the beginning of the cursor
N: Repeat the previous search command in the same direction.
N: Repeat the previous search command in the reverse direction.
: S/P1/P2/G: replace all p1 in the current row with P2.
: N1, N2s/P1/P2/G: replace all P1 from line N1 to line N2 with P2.
: G/P1/S // P2/G: replace all P1 files with P2.
9. option settings
ALL: lists all options.
Term: Set the terminal type
Ignorance: Case Insensitive in search
List: displays the stop table (CTRL + I) and end mark ($)
Number: displays the row number.
Report: displays the number of changes made by line-oriented commands.
Terse: displays brief warning information
Warn: No write information is displayed if the current file is not saved when it is transferred to another file.
Nomagic: allows you to use special characters without "\" in search mode.
Nowrapscan: Prohibit VI from searching at both ends of the file and starting from the other end.
Mesg: Allows VI to display the information that other users write to their terminal using write.
10. Last-line command
: N1, N2 Co N3: copy the content from line N1 to line N2 to line N3.
: N1, N2 M N3: Move the content from line N1 to line N2 to line N3
: N1, N2 D: Delete content from line N1 to line N2
: W: Save the current file
: E filename: open the file filename and edit it.
: X: Save the current file and exit.
: Q: Exit VI.
: Q! : Do not save the file and exit vi
:! Command: execute shell command
: N1, N2 W! Command: The content from line N1 to line N2 in the file is used as the command input and executed. If N1 and N2 are not specified, the entire file content is used as the command input.
: R! Command: place the output result of the command to the current line.
11. Register operations
"? Nyy: Save the content of the current row and Its n rows to the register? , Where? It is a letter, and N is a number.
"? Nyw: Save the current row and Its n characters to the register? , Where? It is a letter, and N is a number.
"? Nyl: Save the current row and Its n characters to the register? , Where? It is a letter, and N is a number.
"? P: retrieve the register? And place it at the cursor position. Here? It can be a letter or a number.
NDD: delete n lines of text from the current row and its bottom, and put the deleted content in the delete Register No. 1.

Another article on VIM reposted (http://hi.baidu.com/jkxtu1984/blog/item/eded7a1f6b175f0f304e1503.html)
Vim shortcut:

Index ctags file names

Move the cursor:
Four Directions
K
H 0 l
J

CTRL + F, CTRL + B flip down, flip up
CTRL + D, CTRL + u flip down half page, flip up half page
$ Move the end of a row
0 move the beginning of a row
W move the next word
B Move to the previous word
Gg jump to the beginning of the document
G jump to the end of the document
% Jump to the matching brackets ("{}" "[]" "()")
CTRL + I, tab to the next jump point
CTRL + O jump to the previous jump point

23 GG, 23g,: 23 jump to 23rd rows
CTRL + I, Tab jump to the next hop point (jump point)
CTRL + O jump to the previous hop point

Search replacement:
# Search for the entire keyword from the cursor
* Search for the entire keyword from the cursor
G # search for keywords from the cursor
G * search for keywords from the cursor forward
Search for characters in the current row for FX, TX, FX, and TX.

Search replacement example:
: S/search/replace
: S/If/since: Replace the next "if" with "since"
: % S/If/Since replace all "if" with "since"
: 1, 3 S/If/Since/g is only valid for 1, 3 rows. If no prefix exists, it is only valid for the current row.

Expression:
. Replace one character
* Indicates the repetition of one or more characters.

/D. * an can match Dan, divan, and Debian.

Word boundary:
Specify the word boundary: \ <and \>
For example,/\ <D [A-Z] * an \> matches a word that starts with D and contains any lowercase letter and ends with.

/\ $ [0-9] * \. [0-9] [0-9] match to find $ XX... x. XX has only two decimal places.

Common editing commands:
A, I insert after the cursor, insert before the cursor
Dd deletes a row
CC, s delete a row and enter insert mode
DW deletes a word
CW deletes a word and enters the insert mode.
Delete a character in X and DL.
S, CL delete a character and enter insert mode

P Paste
XP exchanges two characters
DDP exchange two rows

Y Replication
YY copy a row
U undo
CTRL + R redo
. Repeat the previous Modification

CTRL + R redo
. Repeat the previous Modification

Partition Pane:
: Split/vsplit separate a window
: New/vnew create a new window
: SF {filename} open filename in a new window
: Close current window
: Only close all Windows except the current window
: Ctrl-w h to the Left window
: Ctrl-w j to the following window
: Ctrl-w k to the window above
: Ctrl-w l to the right window
: Ctrl-w t to the top window
: Ctrl-w B to the bottom of the window

Repeated operations (macro operations ):
Q [A-Z] starts the record operation and records it to the register [A-Z]
Q. Stop Logging
@ [A-Z] execute operations in the register
@ Execute the operations recorded in the latest register
Example: A buffer zone has two rows:
Sys/types. h
Stdio. h
--> Change:
# Include <sys/types. h>
# Include <stdio. h>
The procedure is as follows:
QA # Start record
^ # Move the beginning of a row
I # Enter insert mode
# Include <# input content
$ # Move the end of a row
I # Enter insert mode
> # Input content
Q # Stop a record

Move another row:
@ A: You can perform the same operation.

Visual mode operation:
CTRL + V enter block-Based Visual Mode
V enters the character-based Visual Mode
V enters the line-Based Visual Mode

C. Delete the selected block
I {string} <ESC> after the block is selected, press the uppercase I, enter the string, and then press ESC,
You can insert the same content into each row of the block.


Jump to Declaration:
[[Jump forward to the first "{"
[] Jump forward to the first "}"
] [Jump back to the first "{" in the top Frame "{"
] Jump backward to the first "}" of the top Grid "}"
[{Jump to the current Code Block (defined {})
[} Jump to the end of the code block

Shell:
: Ctrl + z/suspend suspends Vim in shell; minimize window in Gui
:! {Command} executes shell commands
: Shell to open a new shell

Save the vim status (Suspended ?) :
: Mksession session. Vim Save the current Vim status
: Source session. Vim replies to the vim status
Vim-s session. Vim resume session when Vim is started

High Efficiency mobile


Outside the insert mode

Basically, you should stay in the insert mode as little as possible, because in the insert mode, VIM
Just like a "dumb" editor. Many new users will stay in the insert mode because it is easy
. But Vim is powerful in its command line mode! You will find that after you learn more about vim,
You will spend less and less time using the insert mode.



Use H, J, K, L



The first step to use Vim for efficient editing is to discard the arrow keys. Use vim,
You do not need to move between the arrow keys and the primary keys frequently, which saves you a lot of time.
In command mode, you can use H, J, K, and l to implement the left, bottom, top, and right arrows respectively.
You may need to adapt to this method at the beginning, but once you get used to this method, you will find the efficiency of this operation.



When you edit your email or other text with paragraphs, you may find that using the direction keys and what you expected
The results are different. Sometimes many rows may be skipped at a time. This is because your section is viewed in vim.
Is a long line. In this case, you can type a G before H, J, K, or l,
In this way, VIM will move as you wish on the screen.



Move the cursor effectively in the current row



Many editors only provide simple commands to control the movement of the cursor (such as left, top, right, bottom, to the beginning/End of the line ).
Vim provides many powerful commands to satisfy your desire to control the cursor.
When the cursor moves from one point to another, the text between the two points (including the two points) is called "Crossed ",
The command here is also called motion. (This important concept will be used later)



Here are some commonly used commands (motion ):



FX: move the cursor to the next X of the current row. Obviously, X can be any letter,
You can also use; to repeat your previous F command.



TX: similar to the preceding command, but it is moved to the left of X. (This is really useful)



FX: it is similar to Fx, but you can look back.



W: move the cursor forward to a word.



B: Move a word behind the cursor.



0: move the cursor to the beginning of the current row.



^: Move the cursor to the first letter of the current row.



$: Move the cursor to the end of the row.



): Move the cursor to the next sentence.



(: Move the cursor to the previous sentence.



Move the cursor effectively throughout the file



Vim has many commands that can be used to reach the desired place in the file.
The following are some commands for moving data in a file:



<C-F>: Move a screen down.



<C-B>: Move a screen up.



G: to the end of the file



Numg: move the cursor to the specified row (Num ). (For example, 10 Gb is up to 10th rows)



GG: to the beginning of the file



H: move the cursor to the screen



M: move the cursor to the center of the screen



L: move the cursor to the screen



*: Read the string at the cursor and move the cursor to where it appears again.



#: It is similar to the above, but it is looking in the opposite direction.



/Text: search for the string text from the current cursor and reach the place where the text appears.
You must press enter to start the search command. If you want to repeat the previous search, press n.



? Text: similar to the above, but in the opposite direction.



MA: Mark a bookmark at the position of the current cursor. Its name is. Only lowercase letters are allowed.
You can't see the existence of bookmarks, but it is already there.



'A: To bookmarks. Note that this is not a single quotation mark. It is generally located on the left of 1 of most keyboards.



'.: The last time you edited the file. This command is useful, and you do not need to mark it yourself.





Efficient input



Automatically complete with keywords



Vim
There is a very beautiful keyword auto-completion system. This indicates that you can enter a part of a long word,
Then press a key, and VIM completes the long term input for you. For example, you have
A variable named iamalongandawkwardvarname is somewhere in your code.
Maybe you don't want to input it one by one every time.



To use the keyword auto-completion function, you only need to enter the first few letters (such as iamal ),
Then press <C-N> (CTRL, then n) or <C-P>. If Vim does not give the words you want,
Basically, until you are satisfied, VIM will keep repeating the matched strings it finds.



Smart entry into insert mode



Many new users only use I to enter the insert mode. In this way, you can enter the insert mode, but it is usually not that appropriate,
Vim provides many commands to enter the insert mode. Below are some of the most commonly used:



I: insert it to the left of the Current Character



I: insert at the beginning of the current row



A: insert it to the right of the Current Character



A: insert at the end of the current row



O: Insert a new row under the current row.



O: Insert a new row on the current row.



C {motion}: Delete the characters crossed by the motion command and enter the insert mode.
For example: C $, this will delete the characters from the cursor position to the end of the line and enter the insert mode. CT !,
This removes the exclamation point from the cursor position to the next one (but not included), and then enters the insert mode.
The deleted character is stored in the clipboard and can be pasted out.



D {motion}: similar to the above, but does not enter the insert mode.





Valid text for moving large segments



Use Visual selections and the appropriate selection mode



Do not want to use the original VI. Vim allows you to highlight (select) some text and perform operations.
Three Visual selection modes are available:



V: select by character. Frequently used mode, so try it yourself.



V: select by row. This is especially useful when you want to copy or move many lines of text.



<C-V>: select by block. Very powerful. This function is available only in a few editors.
You can select a rectangle and the text in the rectangle is highlighted.



When selecting a mode, use the arrow keys and commands described above (motion ).
For example, vwww highlights the three words in front of the cursor. Vjj will highlight the current line and the following two lines.



Cut and copy in visual selection mode



Once you highlight the selection area, you may want to perform some operations:



D: paste the selected content to the clipboard.



Y: copy the selected content to the clipboard.



C: paste the selected content to the clipboard and enter the insert mode.



Cut and copy in non-visual selection mode



If you know exactly what you want to copy or cut, you do not need to enter the visual selection mode.
This will save time:



D {motion}: Cut the characters crossed by the motion command to the clipboard.
For example, DW cut a word and DFS cut the character from the current cursor to the next S to the clipboard.



Y {motion}: similar to the above, but it is a copy.



C {motion}: similar to d {motion}, but enters the insert mode.



DD: Cut the current row.



YY: copy the current row.



Cc: Cut the current row and enter the insert mode.



D: cut from the cursor position to the end of the row to the clipboard.



Y: copy the current row.



C: similar to D, and finally enters the insert mode.



X: Cut the current character to the clipboard.



S: similar to X, but enters the insert mode at last.



Paste



Pasting is simple. Press p.



Use multiple clipboard



Many editors only provide one clipboard. There are many Vim instances.
The clipboard is called a register in vim ).
You can list all register names and their contents defined currently. The command is ": Reg ".
It is best to use lower-case letters as the register name, because some of the upper-case letters are occupied by VIM.



The register command is double quotation marks.



For example, we want to copy the current row to register K. You should press "kyy.
(You can also use v "Ky. Why ?) The current row already exists.
Register K until you copy something into Register K. Now you can run the "kp" command
Paste the content in register K to the desired position.





Avoid repetition



Amazing. Command



In VI, input. (decimal point) will repeat the previous command you gave.
For example, if your last command is 'DW '(delete a word), VI will delete another word.



Use numbers



Using numbers is also a powerful and time-saving feature of vim.
You can use a number before many Vim commands. This number will tell Vim how many times this command needs to be executed.
For example:



3j moves the cursor down three rows.



10 DD will delete 10 rows.



Y3 "copies the content from the current cursor to the third quotation mark to the clipboard.



Numbers are very effective methods to extend the scope of the motion command.



Record macro



Sometimes, you will find that you repeat the same series of actions in each segment or line of the article.
Vim allows you to record a macro to fulfill your special needs.



Qregister: Record macro to register. Here, register is your register name.
For example, QA records and stores Macros in register.



Q: The Record of the ending macro.



@ Register: Use a macro with a register. For example, @ A will use Macros in register.



You must remember that macros only record your series of keys and repeat them.
They are not magic. Because there are many methods to accomplish the goal in Vim,
So sometimes you need to carefully select commands to record your macros. Because they will be executed in all the places where you want to execute it.





Use Vim to write code



Vim is an excellent editor for writing code, because it has some features dedicated Program Personnel.
Here are some common examples:



] P: similar to P, but it automatically adjusts the indent of the pasted text to adapt to the position of the current Code. Try it!



%: Matching curly braces, square brackets, and brackets. On the top of a bracket, press %,
The mouse will appear in the matching other half of the brackets.



>>: Indent all selected codes.



<: Similar to the preceding but not indented



GD: arrives at the position where the cursor is located, where the function or variable is defined.



K: Search for the word at the current position of the cursor in man.

(Slightly modified)

Original article addressHttp://chinahhucai.cublog.cn

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.