Vim tool usage

Source: Internet
Author: User
Tags ssh secure shell

1. The first line of the GG file and the end line of the G File

2 Vim auto-completion
Generally, CTRL + N is used to automatically complete the configuration. After vim7, C/C ++ functions, variables, or class members use Ctrl + X, CTRL + O can also be automatically completed.
If there are multiple incomplete items, you can press Ctrl + N multiple times to select the expected item.

3 Split (horizontal column) vsplit (vertical column)
# Split. Set the current directory (the current directory here is not the result of PWD, or the directory opened in current Vim)
# Split is to split the current directory (the current directory is the result of PWD) or the file that is currently open into columns.

4 Ctrl + W jump from one column to another

5 e is followed by the DIRECTORY parameter and exits to the specified directory.
E + directory (here is the directory, it cannot be a file, and E + file is useless) indicates to exit to the specified directory
E. exit to the current directory. The current directory here is the result of PWD. If you want to return to the specified directory, you can use: E to specify the directory, for example, E./src/scxsystemlib --- to return to the scxsystemlib directory.

6 nyy copy n rows YY copy a row p paste. y copy the highlighted text to the clipboard.

7 NDD Delete n rows dd delete one row. DB delete a word.

1. copy and paste the basic commands:

YY copies the entire row of the cursor. Or an uppercase y.

2. Copy two rows, YY or y2y.
Y ^ copy to the beginning of the line, or y0. Does not contain the character at which the cursor is located.
Y $ copy to the end of the row. Contains the character at which the cursor is located.
Copy a word from YW.
Copy two words (words) in the y2w format ).
Copy YG to the end of the file.
Copy y1g to the beginning of the file.
P lowercase P indicates that the cursor is appended to the lower case ).
P capital P indicates that the cursor is pasted to the Front (top ).

8By default, SSH Secure Shell client does not support Vim syntax highlighting. solution:

1. Select enable ANSI colors on SSH Secure Shell Client

2. Add export term = dtterm to/etc/profile

3. Generate the. exrc file in the home directory of the current user, add the syntax enable command to the file, and save the file.
In this way, the current user opens the vim command in any directory and the syntax is highlighted. Haha.

9 0 cursor to the beginning of the line, $ cursor to the end of the line

10 W next word, one word before B

11/At the time of searching, the next n characters, Shift + N are used to search for the same character in the opposite direction.

12 F (ILE) to display the current file name

13 CD specifies the current path. By default, PWD obtains the file path when Vim is opened.

14
Add the following command to the. exrc file generated by the current user's home directory:

Set tabstop = 4 ---- set the tab width
Set softtabstop = 4 ---- set the width of the soft Tab
Set shiftwidth = 4 ---- set the number of indented spaces to 4
Set autoindent ---- set Automatic indent: when the new line is enabled, the indentation distance of the last line of the new line is also the number of spaces. Use noautoindent to cancel settings
Set cindent ---- set the automatic indent mode in C/C ++ Language
Set expandtab ---- replace all tabs with spaces specified by tabstop (the command can be abbreviated as set et)

15
If set cindent is set and C/C ++ style is automatically indented, you can select a piece of code During encoding, press "=" to re-indent the code according to the C/C ++ style, and beautify the code. However, after automatic indentation is enabled, when Vim under the term is used, the format is often messy when the code is pasted. That is because the term does not know that the Code is being pasted. It directly "paste" the action and explains it to VIM as a type. Therefore, VIM adjusted the indentation for the code in the Set automatic indent format.
Unfortunately, the pasted Code contains indentation, so the layout is messy.
This problem won't occur in gvim, because it can know that it is being pasted.
After knowing the cause of this symptom, the solution is obvious: Disable all automatic indentation during pasting,
After the paste is completed, open it again. It is too tedious to manually disable auto indent. Vim provides a very useful command. You only need to enter ": Set paste" to disable all auto indentation. After pasting, enter ": Set nopaste" to re-open the original auto indent settings.
----- In actual use, the = number can be used for automatic formatting without the use of set cindent. Is it strange?

16 set number (set Nu); Set nonumber (set Nonu)

17 typographical code
You can use "v" to highlight a piece of code and then use "=" to automatically print the code.
If it is a file, you can use Gg = g for layout, or Gg v G = for layout, the effect is the same.

18. Cut, copy, and paste in visual selection mode
CTRL + V can only select the code of one rectangle. The size of the rectangle is determined by the width of the last row in the rectangle range;
The lower-case V is highlighted Based on the manually selected range. You can select the part of a row;
In uppercase, the value is selected in the entire row.
Cutting and copying in visual selection mode
Once you have a highlight selection, you may want to do something. The following are the most useful commands for the selected highlighted text:
D. Cut (delete) The highlighted text to the clipboard)
Y copy (or "Yank"). Vim uses "" to copy the highlighted text to the clipboard.
C cut the highlighted text to the clipboard. Similar to D, C will enter the insert mode.

Shift + V visualized rows
CTRL + V visualization Block

19 matching brackets
% Place the cursor on a curly braces {}, brackets [], or parentheses (), and press % to move the cursor to the corresponding curly brackets, Parentheses, or parentheses. this function can be used to resolve the issue of multi-layer nested logic code blocks.

20. Motion)
FX
Move the cursor forward to the next character X in the current row (obviously, X can be any character you want to move ). this is a very useful command. you can enter; to repeat the previous F command.
TX
It is basically the same as the above command. Except for moving the cursor to the character X rather than the character x itself, the only difference is that each time a character before X is matched, you must move the cursor behind the matching character. Otherwise, you will always find the current matching character and not find it next (silly ). (This is really useful)
FX
Move the cursor backward to the previous character X.

21. Move the current page
<C-F> move the cursor forward (down) a page (screen ).
<C-B> move the cursor back (up) a page.
G move to the end of the file
Numg moves the cursor to a row. (For example, 10g moves the cursor to 10th rows .)
Gg move the cursor to the beginning of the file
H move the cursor to the first line of the current page.
M moves the cursor to the middle line of the current page.
L move the cursor to the last line of the current page)

22 string Movement
* Read the string under the cursor and move it to the next position in the current file where the string appears. (For example, if the string where your cursor is located is "Bob,", after you press *, the cursor moves the next "Bob" in the file .)
# It works the same as the * command, except that the direction is the opposite. It is the position that moves to the last occurrence of the string.

23 Use of tags
Ma sets a bookmarks named a for the current cursor position. The name of the Bookmarks can be any lowercase letter. You cannot see the bookmarks, but it does.
'A move the cursor to bookmarks A. Note 'is not a single quotation mark. It is on the left of number 1 on most keyboards)
'. Move to the row you edited last time. this is "useful ". if you need to view some things in the file, you can use this command to return to the line you edited before, even though you didn't set it as a book.

24. Cut, copy, and paste in non-visual selection mode
If you know what you want to copy or cut, you can complete these operations without entering the visual selection mode. This is faster.
D {motion} Cut the text passed by motion to the clipboard. for example, DW cut a word starting with the current cursor to the clipboard, while DFS cut the text from the current row from the cursor to the next S to the clipboard.
Y {motion} copies the text of motion "passed" to the clipboard.
C {motion} Cut the motion "passed" text to the clipboard and enter the insert mode.
Dd cut the current row to the clipboard.
YY copies the current row to the clipboard.
CC cut the current row to the clipboard and enter the insert mode.
D. Cut the text from the current position of the cursor to the end of the line to the clipboard.
Y copies the entire line to the clipboard, which is similar to YY. (Yes, this command is not consistent with the operation method of command D. You can use y $ to perform operations similar to D)
C cut the text from the current position of the cursor to the end of the line to the clipboard and enter the insert mode.
X cut the current character. (similar to the backspace key in command mode)
S cut the current character and enter the insert mode.
Paste
Pasting is simple. move the cursor to the position you want to paste and enter p. in command mode, the P (uppercase) command is used to paste the content of the clipboard to the current position of the cursor, p command may be more in line with habits)

 

25 Vim suspension and return
Press ctrl-Z to exit vim and return to shell. Then, press the command FG and press enter in UNIX to return to the previously interrupted Vim session.
FG: foreground
BG: Background

26 LCD changes the current directory of the current window, that is, the value of PWD
For example:
LCD ~ /Cmcode/Unix/shared/xplatcommon/src --- enter the directory of the current window ~ /Cmcode/Unix/shared/xplatcommon/src set the PWD directory for this window.
In this way, the C. Command will exit ~ /Cmcode/Unix/shared/xplatcommon/src directory. It is a useful method. ^_^
LCD is used in combination with E. When the window is split, the LCD only modifies the current directory of the current window, and other windows are not affected.
~ The path/cmcode/Unix/shared/xplatcommon/src can be viewed through the command file.

27 scrolling command
CTRL + u tumble the screen forward (in the direction of the file header) to a half screen. The prompt is "up;
CTRL + D tumble the screen toward the back (the end of the file) to a half screen. Tip: d Indicates down;
You can add a number N before the two commands to roll n rows forward or backward. In addition, this value is remembered by the system. When we use Ctrl + U and CTRL + D commands to scroll the screen, the corresponding number of rows will be rolled.
CTRL + F tumble the screen to the end of the file (one page); prompt: F indicates forward;
CTRL + B tumble the screen to the beginning of the file (that is, one page ). Tip: B Indicates backward;
You can also add a number N before the two commands to move the screen n pages forward or backward;
CTRL + e scroll up a line;
CTRL + y scroll down a line.

28 move to the beginning or end of a row
The "$" command moves the cursor to the end of the current line. If you have a <End> key on your keyboard, you can do the same.
The "^" command moves the cursor to the first non-empty character in the current line,
The "0" command moves to the first position of a row, and the <Home> key can also perform the same function.
The "$" command accepts the number prefix. 1 $ indicates moving to the end of the current row; 2 $ indicates moving to the end of the next row; and so on.
"0" and "^" do not accept the number prefix.

29 move to a specific row
The "G" command moves the cursor to the end of the text;
The "GG" command moves the cursor to the beginning of the text;
Run the "NG" command to move the cursor to the first blank character in line N. You can use ": set number <enter>" to display the row number and use ": set nonumber <enter> to disable display;
The "N %" command moves the cursor as 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;
Tip: "H" indicates "home", "M" indicates "Middle", and "L" indicates "last ".

30 move between codes
"[]" Jumps up to the end of a function, and "] [" moves down to the end of a function;
"[[" Jump up to the start of the function, "]" Jump down to the end;
Note: The four commands require the "{" "}" symbol to be in the first column;
"[{" Jumps to the start of the current code block {}, and "[}" jumps to the end of the current code block;
"] {" "]}" "[(" [) ""] (""]) "[/" "]/" Has similar functions, only matching "()" "/*"

The "[I" command looks for the identifier under the cursor, not only in the current file, but also in all included files;
"[<Tab>" jumps to the first item. <tab> It Can Be <Ctrl + I>;
"N [<tab>" jumps to item n;
"] I" only lists items under the cursor

"[D" only searches for macros defined by "# define,
"] D" only lists items under the cursor
"GD" is only available in the current file and jumps to the place where it is used for the first time;
"GD" is only available in the current function. Note that the start symbol "{" of the function must be in the first column;

31. Enter d $ to delete the cursor from the current cursor to the end of the row. **

32 lecture II Section 3: About commands and objects

 
The format of the DELETE command D is as follows:

[Number] D object or d [number] Object

The meaning is as follows:
Number-indicates the number of times the command is executed (optional, 1 by default ).
D-Indicates deletion.
Object-indicates the object to be operated on by the Command (which is described below ).
 
A brief Object List:
W-from the current position of the cursor to the end of a word/word, including spaces.
E-from the current position of the cursor to the end of a word/word, but the space is not included.
$-From the current position of the current cursor to the end of the current row.

Note:
If you are brave in explorer, please press the key that represents the corresponding object in normal mode without using the command, then
The movement of the cursor is shown in the object list above.
 
 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Section 2 and section 4: Special Cases of object commands
 

** Enter dd to delete the entire row. **

In view of the high frequency of row deletion, VIM designers decided to simplify the whole row deletion, only on the same row
After hitting d twice, you can delete the entire line where the cursor is located.
 
1. move the cursor to the second line in the short paragraph below this section.
2. Enter dd to delete the row.
3. Move to the fourth row.
4. Enter 2dd. (Do you still remember the number-command-object mentioned above ?) Delete two rows.

1) Roses are red,
2) mud is fun,
3) violets are blue,
4) I have a car,
5) clocks tell time,
6) Sugar is sweet
7) and so are you.
 
 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Lecture 2 Section 5: Undo commands


** Enter U to undo the last command and enter U to correct the entire line. **

1. move the cursor to the line marked with ---> below this section and place it in the first error.
.
2. Enter X to delete the first letter you do not want to retain.
3. Then enter U to undo the last executed (once) command.
4. Use X to correct all errors of the row this time.
5. Enter an uppercase u to restore the original state of the row.
6. Enter U multiple times to undo the U and change the previous command.
7. Then enter the CTRL-R multiple times (Press ctrl key not open, then enter the r key), so that
You can run the recovery command to undo the undo command.

---> Fiix the errors oon thhis line and reeplace them witth undo.

These are very useful commands. The following is a summary of the second lecture.

. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

How to execute External commands in VIM

** Input :! Then, enter an external command to execute the command. **
1. Press what we are familiar with: the command sets the cursor to the bottom of the screen. In this way, you can enter the command.
2. Enter an exclamation point! This character allows you to execute external shell commands.
3. Take the LS command as an example. Enter! Ls <press enter>. This command will list your current directory
The content is the same as the result of entering the LS command at the command line prompt. If! Ls unavailable
Function, you can try :! Dir.

 

 

 

 

1. Merge rows
Shift + J ---- append the next row of the current row to the end of the current row

Delete and insert two columns
Delete column 1. move the cursor to the position to be operated. 2. Press ctrl V to enter the "visible block" mode and select the number of rows to operate on this column. 3.d Delete.
The insert column operation is slightly different. for example, we insert "()" before each row: 1. position the cursor to the position to be operated. 2. press ctrl V to enter the "visible block" mode and select the number of rows to operate on this column. 3. shift I (I) input the content to be inserted. 4.esc press twice to insert content in the selected area of each line. this makes it much easier.

 

 

 

Vim code folding
When writing C, (C ++) CPP Code, automatic folding can bring many benefits. The powerful Vim provides a shortcut. As follows:

1. Folding Mode
You can use the 'foldmethod' option to set the foldmethod: Set OFDM = *****.
There are 6 Ways to select the fold:
Manual manual definition folding
More indent indicates more fold
Expr uses expressions to define collapse
Syntax uses syntax highlighting to define collapse
Diff folds Unchanged text
Marker folds the mark in the text
Note that each folding method is incompatible. If expr is not used and marker is used, the indent and marker modes are used in turn.

When using this function, use the: Set OFDM = marker command to set it to the marker foldmethod ).
To make the folding take effect every time Vim is opened, add the settings in the. vimrc file, such as adding: Set OFDM = syntax, just like adding other initialization settings.

2. Fold command
After the folding method is selected, we can implement the folding we need for some code. Because I use more indent and marker, their usage is used as an example:
If the indent method is used, VIM automatically folds the middle part of the braces, and we can directly use these ready-made folding results.
In a foldable place (in the middle of braces ):
ZC folding
ZC folds all nested folding points in the range
Zo unfold and fold
Zo expands all nested folding points in the range
[Z to the start of the current fold.
] Z to the end of the currently opened fold.
ZJ moves down. To the start of the next fold. The closed folding is also included.
ZK moves up to the end of the previous fold. The closed folding is also included.

When using the marker method, you need to use a tag to identify code folding. The default value of the system is {And}. Do not change it :)
We can use the following command to create and delete a fold:
ZF creates a fold, for example, in the marker mode:
Zf56g: Creates code folding from the current row to 56 rows;
10zf, 10zf +, or zf10 labels, create code folding from the current row to the last 10 lines.
10zf-or zf10 labels, create code folding from the current row to the previous 10 lines.
At ZF %, create the matching brackets (), {}, [], <>, etc.) from the current row ).
ZD Delete is the folding of the cursor. Valid only when 'foldmethod' is set to "Manual" or "marker.
ZD loop Delete refers to the folding under the cursor, that is, nesting Delete fold.
Valid only when 'foldmethod' is set to "Manual" or "marker.
Ze removes all folds in the (eliminate) window.
Valid only when 'foldmethod' is set to "Manual" or "marker.

 

Summary of common replacement modes in VIM:

1. Simple replacement expression

: % S/four/4/g

The prefix of "%" indicates that replacement is performed in all rows. The last "G" Mark replaces all matching points in the row. If you only operate on the current row, remove %.

Use "\ <" to specify the start of a matching word:

: % S/\ <four/4/g

Obviously, errors will still occur when processing "fourty. Use "\>" to solve this problem:

: % S/\ <four \>/4/g

If you are coding, you may just want to replace the "four" in the comment, but keep the "four" in the code. Since this is difficult to specify, you can add a "C" mark in the replacement command, so that Vim will prompt you before each replacement:

: % S/\ <four \>/4/GC

2. Delete unnecessary spaces.

To delete unnecessary spaces behind each line, run the following command:

: % S/\ s \ + $ //

"\ S \ + $" indicates one or more (\ +) spaces (\ s) before the end of the line ($ ). The "to" part of the replacement command is empty :"//". In this way, the matching spaces are deleted.

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.