Common commands in the vim editor

Source: Internet
Author: User
1. enter the vim editor mode: (when entering the vim editor, the default mode is the command mode) enter vi on the terminal: Enter the vim editor vifilename: Enter the vim editor, and create a new filename File, position the cursor to the first line of the file. when vi is working, it will be created in the current directory... 1. enter the vim editor mode: (when entering the vim editor, the default mode is "command mode") enter vi on the terminal: Enter the vim editor vi filename: Enter the vim editor, A new filename file is created. when the cursor locates the first line of the file vi, it is created in the current directory. swp temporary file. this temporary file is also called a swap file. when vi finishes editing the current file and exits normally, it is a temporary file. swp is automatically deleted. when vi finishes editing the current file and unexpectedly exits, the temporary file is deleted. swp will be left behind vi + filename: Enter the vim editor, open the existing filename File, and move the cursor to the last line of the file when vi is working, will be created in the same directory as the edited file Create a name named. filename. when vi finishes editing the current file and exits normally. filename. swp is automatically deleted. when vi finishes editing the current file and unexpectedly exits, the temporary file is deleted. filename. swp will be left behind vi + # filename: Enter the vim editor, open the existing filename File, and move the cursor to the # line of the file (# must be replaced by a number) vi-R filename: Enter the vim editor and open the existing filename File in read-only mode. the edited file cannot be stored back, but can be saved as vi-r filename: restore the file vi-r from vi crash or abnormal exit. *. swp: swap files from. *. recover file vi-r in swp: list all swap files left in the current directory due to abnormal exit of vi to restore vi-L: list all swap files left in the current directory due to abnormal exit of vi for recovery 2. vi has three working modes: command mode, edit mode, and command item mode. Command mode: In this mode, you can input the vi command to process the file content (copy, delete, move, etc.), or press the cursor key to move the cursor. Edit mode: In this mode, you can enter content at the cursor. Command item mode: In command mode, after you enter a colon, the cursor will jump to the bottom line and then enter the command. Command mode --> enter commands such as a, A, I, I, o, and O --> Edit mode --> press ESC --> command mode --> enter colon --> command item Mode 3. run the exit vi command in command mode. enter: w to save the file. do not exit vi: w filename. save the content in vi to a non-existent filename File. do not exit vi: w! Filename saves the content in vi to an existing filename File, does not exit vi: w> filename appends the content in vi to the filename File, does not exit vi: n1, n2w filename stores the content from line n1 to line n2 to the nonexistent filename File: n1, n2w! Filename saves the content from line n1 to line n2 to the existing filename File: wq to save the file, exit vi: x to save the file, exit vi: ZZ to save the file, and exit vi: q: Do not save the file and exit vi (if the file has changed, you cannot exit): q! Do not save the file, exit vi: ZQ do not save the file, exit vi4. in command mode, you can enter the following command: (1) Move the cursor command h: Left l: right k: Top j: bottom left cursor key right cursor key up cursor key down cursor key (2) insert command I: insert content at cursor I: insert content in the first line: insert content A after the cursor: insert content o at the end of the current row: insert A new row O below the current row: insert A new row above the current row (3) delete command x: delete character X at the cursor: Delete a character d0 before the cursor: Delete a string of characters D from the first character of the row where the current cursor is located to the first character of the current cursor: delete a string of characters dd from the character where the current cursor is located to the last character in the row where the current cursor is located: delete all characters dw in the row where the cursor is located: delete a word starting from the character at the cursor, for example, a boy girl pig. if the cursor is at l, after dw, the result is: if the cursor of a boy girpig is at the space between l and p, after dw, the result is: a boy girlpig if the cursor is at B, after dw, the result is: a girl p Ig # dd: delete the #-1 line from the row where the cursor is located to the end (# must be replaced by a number) # x: delete # Characters starting from the character where the cursor is located (# must be replaced by digits): n1, n2 d: delete content from line n1 to line n2 (4) copy command: n1, n2 t n3: copy the content from line n1 to line n2 to the next line (5) of line n3 with the following command: n1, n2 m n3: move the content from line n1 to line n2 to the next line (6) in line n3 modify/replace command r: enter a character to replace a character at the cursor R: replace from the character at the cursor, followed by one character overwritten by the input character s: enter a string of characters to replace the character S at the cursor: first clear the row where the cursor is located, you can enter a string of characters in the current line cc: first clear the row where the cursor is located, you can enter a string of characters in the current line cw: modify a word for example: the AB cdef ghijk cursor is at d. After cw is input, the result is AB cde $ ghijk. if pqghd is input, the result is: AB cpqghd ghijk if you enter p and press ESC, the result is: AB c P ghijkC: modify the part starting from the cursor and following the line. for example, the AB cdef ghijk cursor is at d. after entering cw, the result is: AB cdef ghij $. after entering pqghd, result: AB cpqghd (7) cursor position command 0: Locate the beginning of the row where the cursor is located $: locate the end of the row where the cursor is located :#: locate Row # First row: $: locate Row/exp: locate string exp? Exp: move forward to the string exp/^ exp: Locate the row whose first line is the string exp/exp $: Locate the row whose last line is the string exp n, m? /Exp: Search for the string exp from n rows to m rows (this command is to be discussed) n, m |/exp: Search for the string exp from n rows to m rows (this command is to be discussed) (8) search replacement command: s/exp1/exp2/g: replace the current line string exp1 with the string exp2: n, m s/exp1/exp2/g: replace the string exp1 from row n to row m with the string exp2: g/exp1/exp2/g: replace the string exp1 of the current row with the string exp2: g/exp1/s // exp2/g: replace the string exp1 in the full text with the string exp2 (9) undo command u (10) buffer command by default, vi uses the unknown buffer, for example, although some parts of the file are deleted, they can still be revoked using the u name. Yy: place the row where the cursor is located in the unknown buffer zone Y: place the row where the cursor is located in the unknown buffer zone p: paste the content in the buffer zone to the next row of the cursor P: paste the content in the buffer to the previous line of the row where the cursor is located "# yy: put the row where the cursor is located into the buffer # (# a ~ is required ~ Z instead) "# p: paste the content in buffer # to the next line of the row where the cursor is located (# a ~ is required ~ Z instead) "# P: paste the content in the buffer # to the top row of the cursor (# a ~ is required ~ Z instead) "# d: delete the current row and put the content of the current row into the buffer # (11) other command: set nu: Display row number: set nonu: cancel row number: n locate to the next file: N locate to the previous file vi 1.c 2.c 3.c: open 3 files at a time first open 1.c, enter: n command, open 2.c, and then enter: after the n command is run, 3.cwill be opened, and after the: N command is input, 2.cwill be opened, and after the: N command is input, 1.cwill be opened. Ctrl + G: display the information of the current edited file Ctrl + L: refresh screen Ctrl + d: Flip back half screen Ctrl + u: flip forward half screen Ctrl + f: press Ctrl + B to flip the screen to the back.
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.