Advanced use of VIM

Source: Internet
Author: User

 

 

1) Some Common Vim configurations, in ~ /. Syntax on in vimrc supports syntax highlighting set nu to display row numbers
Set Nonu does not display the row number set AI sets automatic indent set shiftwidth = 4 sets automatic indent 4 spaces, of course you need to set Automatic indent first. set STS = 4: Set softtabstop to 4. after entering the tab, 4 cells are displayed. set tabstop = 4 the actual tab is 4 spaces, instead of the default 8. after the set expandtab is input, VIM fills the tab with appropriate spaces.: Set HLS open search highlight

: Set nohls Cancel search highlight

: Set list: Display special characters: Set nolist 2) Multi-file Editing: e filename Vim will open a new file in the original window. If the old file has been edited, save it.: SP filename Vim Splits a horizontal window and opens a new file in the window.: VSP filename Vim Splits a window on the right and opens a new file in the window. if: SP or: VSP does not specify a file, open the original file in a new window. CTRL + W to switch to multiple windows opened by VIM. CTRL + w Q to close the current Windows in multiple windows. The upper and lower windows of the opened two files are distributed: vim-O file1 file2
Distribution of left and right windows of the opened two files: Vim-O file1 file2

 

3) Search for/XXX (? XXX) indicates searching for strings matching XXX in the entire document,/indicates searching down ,? Indicates searching up. after finding it, enter n to find the next match and enter n to search for it. * (#) when the cursor stays on a word, enter this command to search for the next (top) word that matches the word. similarly, enter n to search for the next match, and enter n to search for the opposite direction. (you can use Shift + * or SHIFT + # To find the word where the current cursor is located.) g * (G #) this command is similar to the previous command, however, it does not completely match the word where the cursor is located, but matches all strings containing the word. % This command looks for the parentheses matching the cursor location, including () [] {}.

In vim, search for:/Str. If there is a space, you need to:/STR str2/ 

 

4) move the cursor W (e) move the cursor to the next word. b. move the cursor to the previous word. 0 move the cursor to the beginning of the line. ^ move the cursor to the starting character of the line. $ move the cursor to the end of the row. flip forward and backward Ctrl + F and CTRL + B. % jump to matching brackets. G (SHIFT + G)-go to the last line in the vim Editor (end of the file) 1G-Goto line number 1 (file start) 20g-Goto line number 20 5) Copy, delete, and paste

X indicates that the current character is deleted.

In VI, y indicates copying, d Indicates deleting, and P indicates pasting. copy and delete are combined with the cursor movement command. You can see the following examples. YW indicates copying the content from the current cursor to the end of the word where the cursor is located. DW indicates deleting the content from the current cursor to the end of the word where the cursor is located. y0 indicates copying the content from the current cursor to the beginning of the row where the cursor is located. d0 indicates deleting the content from the current cursor to the beginning of the row where the cursor is located. Y $ indicates copying the content from the current cursor to the end of the row where the cursor is located. d $ deletes the content from the current cursor to the end of the row where the cursor is located. yfa indicates copying the content from the current cursor to the first a character after the cursor. DFA indicates to delete the content from the current cursor to the first a character after the cursor. in particular, YY indicates copying the row where the cursor is located. dd indicates deleting the row where the cursor is located. d: Delete the content from the current cursor to the end of the row where the cursor is located.

Delete the entire document: 1, $ d

Delete the line break symbol after a line:In the command status, press the "J" key on a line to join the next line. 

U undo the last operation, CTRL + R restore the last undo.

Delete all blank lines (^ indicates the beginning of the line, \ s * Indicates zero or multiple blank characters, and $ indicates the end of the line): G/^ \ s * $/d

6) replace a single character with R, replace multiple characters with R, replace one character with S, and replace the whole line with S.: S/Vivian/sky/Replace the first Vivian in the current row with Sky: S/Vivian/sky/g, replace all Vivian in the current row with Sky: N, $ S/Vivian/sky/Replace the first Vivian from row n to row N with Sky: N, $ S/Vivian/sky/g replace all Vivian from row n to row N with skyn as a number. If n is ., indicates starting from the current row to the last row: % S/Vivian/sky/(equivalent to: G/Vivian/S/sky/) Replace the first Vivian of each row with Sky: % S/Vivian/sky/g (equivalent to: G/Vivian/S // sky/g) can be used as the separator to replace all Vivian in each line with Sky, in this case, the/in the middle will not be used as the separator: s # Vivian/# Sky/# Replace the first Vivian/in the current line with Sky /: % S +/oradata/apras/+/user01/apras1 + (replace with +/):/oradata/apras/Replace with/user01/apras1/

 

: 2nd S/Part1/Part2 # Replace 50th rows with Part2 with 1st rows to rows with Part1 lines: 2nd S/Part1/Part2/G # Use Part2 to replace all Part1 :., + 3 S/Part1/Part2 # Replace the current row with Part2 and 1st Part1 in each row in the three rows following the current row

:., + 3 S/Part1/Part2/G # Replace the current row with Part2 and all Part1 in the three rows following the current row 

 

For example, add XXX: % S/\ (. * \)/\ 1 XXX/g to each line.

 

7) Visual block status

Press Ctrl + V to enter and select the column blocks. For example, 4l selects the next four columns of the cursor and seven rows starting with the 7j cursor. After 4l and 7j are executed, select seven rows and four columns of the block, you can also use hjkl after Ctrl + V to control the selected rows and columns. You can use y, D, and other operations to copy and delete the selected rows and columns. For example, remove spaces before multiple rows.

(Block EDIT: edit a row. The effect is applied to all selected rows.) For example, comment multiple rows: Ctrl + V after selecting a block, then, I enters the editing state and enters # At the beginning of the first line. After the ESC exits twice, all selected rows are automatically added before # to annotate all rows.

Shift + V can be used to select multiple rows, and then: S/AAA/BBB/g to replace the selected rows.

Shift + V: after multiple rows are selected, execute d to delete the selected multiple rows.

Http://man.chinaunix.net/newsoft/vi/doc/visual.html
 

8) Others

View the files opened by VIM:Under VIM: F

Remove the ^ m: dos2unix ufile at the end of the blank line or line in VIM

Sort cannot be used in VIM: You can only execute sort-u File> file_new under terminal. Diff file in VIM: if a file has been opened : Vert diffsplit file2. If the file has not been opened, run vi-O test_vi.txt test_vi2.txt. The two files are arranged left and right. If the two files need to be arranged up and down, run vi-O test_vi.txt test_vi2.txt. Vimdiff can compare up to four files at the same time. Run the command without exiting Vim. For example, in the perl file, run the command without exiting ,:! Perl %. If you have already executed the preceding command, you only need :!!  

Complete!

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.