Common commands for Vim

Source: Internet
Author: User

Vim is an enhanced version of Vi, it does not have a menu. Only command


Insert


A


Inserts text after the current cursor


A


Inserting text at the end of a bank


I


Insert text before the cursor


I


Inserting text at the beginning of the bank


O


Insert a new line under the cursor


O


Insert a new line on the cursor


H


Move left one character


J


Move down one character


K


Move down one character


I


Move one character to the right


$


Move to the end of the current line


0


Moves to the beginning of the current line (number 0)


H


Move top of screen


M


Move to the center of the screen


L


Move the lower end of the screen


Gg


To the first line of the entire file


G


To the last line of the entire file


NG


To line N, for example 5G to line 5th of the entire file


: N


to Nth row


Delete


X


Delete the character at the cursor location


Nx


Delete n characters at cursor location


Dd


Delete the cursor in the row


Ndd


Delete n rows, for example: 5DD Delete 5 rows


Dg


Delete the content at the end of the cursor


D


Delete from the cursor to the end of the line


: n1,n2d


Deletes the specified range of rows, indicating that the deletion starts from the N1 line to the N2 row. Example: 8,20d to delete the contents from line 8th to line 20th


Copy and paste


Yy,y


Copy when moving forward


Nyy,ny


Copy n rows below the current row


Dd


Cut the current line (remove and paste first)


Ndd


Cut the following n rows of the current row


P


Pastes the contents on the next line of the current cursor (lowercase)


P


Pastes the contents on the previous line of the current cursor (uppercase)


D


Delete the end of the line from where the cursor is to the current line


: n1,n2d


Deletes the specified range of rows, indicating that the deletion starts from the N1 line to the N2 row. Example: 8,20d to delete the contents from line 8th to line 20th


Replace and cancel


R


Replace the character at which the cursor is located


R


Replace the character at the beginning of the cursor and press ESC to end.


Example: This is a test document


If the current cursor is at position A, you enter R in command mode. What you start typing will replace the a test document.


U


Cancel the previous action


Search and replace


/string


/means looking up a string from the previous document


When you press N, the character jumps to the place where it appears, and when you press N, it jumps up to the last occurrence.


Ignore case when opening search: Set IC


Ignore case when search is closed: Set Noic


N


Query where characters appear in the past


N


Find the position where the character appears from the backward forward


:%s/old/new/g


Replaces the specified string with the full text, and old represents the character to be replaced. New indicates the character to be replaced


For example, replace all 123 characters in a document with 789


:%s/123/789/g


: n1,n2s/old/new/g


Replaces the specified string within a certain range, similar to the full text. Only in a certain range. N1 and N2 are indicated in a certain line.


Example: Replace 789 in line 10th to 12 with 007 characters


: 10,12s/789/007/g


You can also put: 10,12s/789/007/g after the G to the C character (: 10,12S/789/007/C), a confirmation message will appear to indicate whether you want to replace (similar to Windows: Are you sure you want to delete it?). )


Replace with 007 (y/n/a/q/l/^e/^y)?


Y: Replace


N: Do not replace


A: Replace All


Q: Cancel the replacement


Save and exit


: W


Save only do not exit


: w a.txt


Save to A.txt file (save As)


: Q


Exit only


: q!


Exit does not save


: Wq


Exit and save


: wq!


Force exit and save (only for the owner or root of the file)


Zz


Exit and save (equivalent to: WQ)


Other Tips


Import file


: R file Name


Example 1: Import the contents of the hello file to the current cursor position


: R Hello


Import command Execution results


: R! Command


Example 2: Importing the current time of the system to the current cursor


: R!date


Execute commands in VI


:! Command


For example, when you are editing a file in VI, you want to see what is in the/root/test directory, you can execute the following command


:! Ls/root/test


The contents of the test directory will appear, prompting you to press any key to return to the VI edit


Press ENTER or type command to continue


Defining shortcut keys


: Map Shortcut Trigger command


Example 1: No matter where the cursor is in the current line, when Ctrl+b is pressed. Remove the # comment from the beginning of the line.


The command is as follows


: Map ^b 0x


Example 2: No matter where the cursor is in the current line, when Ctr+p is pressed. Add a # comment to the beginning of a line


: Map ^p i#<esc>


(^ means the CTRL key) ^B is pressed by ctrl+v+b or CTRL + V Ctrl+b. If the ^ is pressed through CTRL, the display is


^p is green.


Cancel shortcut keys


: Unmap shortcut keys


Example: Cancel Ctrl+b shortcut keys


: Unmap ^b


Continuous line Comment


: n1,n2s/^/#/g


: n1,n2s/^/#//g


: n1,n2s/^/\/\//g


Example 1: Add # 5 to 10 lines at the beginning of the line


The command is as follows


: 5,10s/^/#/g


^ means back to the beginning


Example 2: Canceling 5 to 10 lines beginning with #


: 5,10s/^#//g


Example 3:5 to 10 line header Add//


: 5,10s/^/\/\//g


where \ Represents the escape character, \/\/is not the letter W but \ and/


Replace


: AB ABC [email protected]


Indicates that when you enter ABC, it is automatically converted to [email protected]


Example 1: If you frequently enter a string of characters in VI and do not want to define shortcut keys. You can use the Ab method. If, you often enter [email protected]. You can do this as follows


: AB [email protected]


This command will be when you enter ABC in VI, enter or space, the ABC character will become [email protected]


Configuration VI


VI configuration file in each user's home directory under the. vimrc file, modify the/ETC/VIMRC file if you want each user to take effect


If you do not have this file, you can create a new one, pay attention to permissions issues


For example, VI/ROOT/.VIMRC file, enter the following simple configuration


Syntax on


Set Nu


Set Autoindent


There are syntax highlighting when using VIM, but there is no syntax highlighting solution when using VI?


Set Vim's alias to VI: Alias Vi=vim


This article is from the "Home of Linux" blog, please be sure to keep this source http://pyhton.blog.51cto.com/8763915/1693500

Common commands for 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.