VIM basic Commands

Source: Internet
Author: User
Tags clear screen save file

label:

  1.
Vim
#Enter vim in the command line to enter the vim editor
2.
i
#Click the i button, the lower end shows --INSERT--
# Insert command, any character in vim may have a role
3.
Esc
# Exit i (insert) command for other commands
4.
:r filename
#Read in a file and write it to the current editor
5.
:w newfilename
#Write the contents of the editor to a new file
6.
:w
#Save the file during the editing process, equivalent to ctrl+s in the word
7.
:! command
Temporarily leave vi to execute the command display result in command line mode! E.g
:!ls
#Execute shell command ls during editing
8.
:sh
#Enter the shell command line, after executing the command, ctrl+d exits and re-enters the vim editor to continue editing.
Under the shell command, execute ctral+l to complete the clear screen.
9.
:wq
#Save file and exit
10.
ZZ
#Save the file and exit, same as the previous command, pay attention to capitalization
11.
:q!
# Forced to exit without saving
12.
:set number or :set nu
#Let the file in the editor display the line number
13.
:set nonumber or :set nonu
#In contrast to the previous command, the line number is not displayed.
14.
:help i
#View Insert Command Help
15.
u
#Undo the previous step
16.
/Fedora
#找Fedora characters
17.
:s /Fedora/Redhat
# Replace Fedora characters with Redhat (replace only the line where the cursor is located)
18.
Dw
# drop the word
Dd
19.
o
#Open a blank line
20.
Vim + filename
#[Edit the last line of the file for editing
twenty one.
Vim +n filename
#Enter the file nth line for editing
twenty two.
:1,.s/redhat/fedora
The #. number indicates the current line, that is, the line where the cursor is.
# Replace the first redhat character from the first line to the current line (.) as fedora
twenty three.
:1,.s/redhat/fedora/g
# Replace all redhat characters from the first line to the current line (.) to fedora, g
Global flag
twenty four.
:1,$s/redhat/fedora/g
#$ indicates the last line
# Replace all redhat characters from the first line to the last line to fedora
25.
:%s/redhat/fedora/g
#same command
26.
:%s/\/fedora/g
# Replace all redhat words in the first line to the last line as fedora
#字, instead of characters
27.
:f
#Display file content, status, etc.
#同ctrl+g command
28.
:e!
#current file, return to last saved
:e file
#Switch to edit file
29.
:n
#When editing multiple files (such as vim file1 file2), switch to the next file, with: e file
In conjunction with




                               

                             VIM command Daquan
Cursor control command
Command cursor movement
h Move one character to the left
j moves down one line
k moves up one line
l Move one character to the right
G moves to the last line of the file
w moves to the beginning of the next word
W moves to the beginning of the next word, ignoring punctuation
b moves to the beginning of the previous word
B moves to the beginning of the previous word, ignoring punctuation
L moves to the last line of the screen
M moves to the middle of the screen
H moves to the first line of the screen
e moves to the end of the next word
E moves to the end of the next word, ignoring punctuation
(move to the beginning of the sentence
) moved to the end of the sentence
{ Move to the beginning of the paragraph
} Move to the beginning of the next paragraph
0 (number), | moved to the first column of the current row
^ Move to the first non-null character of the current line
$ moves to the last character of the current line
+, Enter moves to the first character of the next line
- Move to the first non-null character of the previous line
Add text to vi
Command insert action
a Insert text after the cursor
A inserts text in the current line
i insert text before the cursor
I insert text before the current line
o Insert a new line below the current line
O Insert a new line above the current line
s delete the character at the cursor and enter insert mode
S delete the line where the cursor is located and enter insert mode
:r file reads the file file content and inserts it after the current line
:nr file reads the file file content and inserts it after the nth line
Esc back to command mode
^v char ignores the specified meaning of char when inserting, this is to insert special characters
Delete text in vi
Command delete operation
x delete the character at the cursor
Dw delete to the beginning of the next word
dG deletes the line until the end of the file
Dd delete the entire line
Db delete the word in front of the cursor
:n,md deletes n lines from the mth line
d, d$ is removed from the cursor to the end of the line
^h, when the backspace is inserted, delete the previous character
^w Insert, delete the previous word
Modify vi text
The number in front of each command indicates the number of times the command is repeated.
Command replacement operation
Rchar replaces the current character with char
R text escape Replace the current character with text until the Esc key is pressed
Stext escape replaces the current character with text
S or cctext escape replaces the entire line with text
Cwtext escape changes the current word to text
Ctext escape changes the rest of the current line to text
cG escape is modified to the end of the file
Ccursor_cmd text escape changed from the current position to the cursor command position to text
Find and replace in vi
Command find and replace operation
/text finds text forward in the file
?text finds text backwards in the file
n Repeat lookup in the same direction
N repeats the search in the opposite direction
Ftext finds text forward on the current line
Ftext finds text backwards on the current line
Ttext looks for text forward on the current line and positions the cursor at the first character of text
Ttext looks for text backwards on the current line and positions the cursor at the first character of text
:set ic ignores case when looking up
:set noic is case sensitive
:ranges/pat1/pat2/g replace oldtext with newtext
:m,ns/oldtext/newtext pass n on m lines, replace oldtext with newtext
& repeat the last :s command
:g/text1/s/text2/text3 find the line containing text1 and replace text2 with text3
:g/text/command Runs the command represented by command on all lines containing text
:v/text/command Runs the command represented by command on all lines that do not contain text
Copy text in vi
Command copy operation
Yy puts the contents of the current line into a temporary buffer
Nyy puts the contents of n lines into a temporary buffer
p puts the text in the temporary buffer into the cursor
P puts the text in the temporary buffer before the cursor
"(a-z)nyy copy n lines into a nameable buffer with the name inside the parentheses, omitting n for the current line
"(a-z)ndd deletes n lines into a namable buffer whose name is inside the parentheses, omitting n to indicate the current line
"(a-z)p puts the contents of the nameable buffer whose name is parentheses after the current line
"(a-z)P puts the contents of the nameable buffer named parentheses before the current line
Undo and repeat in vi
Command undo operation
u Undo the last modification
U Undo all modifications to the current line
Repeat the last modification
, repeat the previous f, F, t, or T lookup command in the opposite direction
; Repeat the previous f, F, t or T lookup command
"np retrieves the last nth deletion (there is a certain number of deletions in the buffer, usually 9)
n Repeat the previous / or ? find command
N repeats the preceding / or ? command in the opposite direction
Save text and exit vi
Command save and / or exit operation
:w saves the file but does not exit vi
:w file saves the changes in file but does not exit vi
:wq or ZZ or :x save the file and exit vi
:q! Do not save the file, exit vi
:e! Give up all changes, save the file from the last time

Start editing again
Options in vi
Option
:set all print all options
:set nooption turn off option option
:set nu prints the line number before each line
:set showmode shows whether it is an input mode or a replacement mode
:set autoindent inherits the indentation of the previous line, especially for multi-line comments
:set smartindent provides automatic indentation for C programs
:set list displays tabs (^I) and end-of-line symbols
:set ts=8 set tab stops for text input
:set window=n Set text window to display n lines
:set number shows the number of rows
:set nonumber cancels the number of rows displayed
Status of vi
Option
:.= Print the line number of the current line
:= Number of lines in the print file
Ctrl+g displays the file name, current line number, total number of lines in the file, and percentage of file location
:l Use the letter "l" to display many special characters, such as tabs and newlines
Locate paragraphs and place markers in text
Option
{ Insert { to define a paragraph in the first column
[[ Back to the beginning of the paragraph
]] Move forward to the beginning of the next paragraph
m(a-z) marks the current position with a letter, such as mz for the mark z
‘(a-z) moves the cursor to the specified marker, such as moving to z with ‘z
Connect lines in vi
Option
J connects the next line to the end of the current line
nJ connection behind n lines
Cursor placement and screen adjustment
Option
H Move the cursor to the top line of the screen
nH Move the cursor to the nth line below the top line of the screen
M Move the cursor to the middle of the screen
L Move the cursor to the bottom line of the screen
nL moves the cursor to the nth line on the bottom line of the screen
^e(ctrl+e) scrolls the screen up one line
Ctrl+y scrolls the screen down one line
Ctrl+u will scroll half a page
Ctrl+d rolls the screen down half a page
Ctrl+b will scroll one page on the screen
Ctrl+f scrolls the screen down one page
Ctrl+l redraw screen
Z-return sets the current line to the top line of the screen
Nz-return sets the nth line under the current line to the top line of the screen
z. Set the current line to the center of the screen
Nz. Set the nth line on the current line to the center of the screen
Z- sets the current line to the bottom line of the screen
Nz- sets the nth line on the current line to the bottom line of the screen
Shell escape command in vi
Option
:!command Executes the shell command command, such as :!ls
:!! Execute the previous shell command
:r!command Read the input of the command command and insert it, for example: r!ls will execute ls first, then read in the content
:w!command takes the currently edited file as the standard input for the command command and executes the command command, such as :w!grep all
:cd directory changes the current working directory to the directory represented by directory
:sh will start a subshell and return ^ with ^d(ctrl+d)
:so file Read and execute commands in the shell program file
Macros and abbreviations in vi
(Avoid using control keys and symbols, do not use the characters K, V, g, q, v, *, = and function keys)
Option
:map key command_seq Define a key to run command_seq, such as :map e ea, whenever you can e move to the end of a word to append text
:map displays all defined macros in the status line
:umap key remove the macro of the key
:ab string1 string2 Defines an abbreviation so that when string1 is inserted, string1 is replaced with string2. When you want to insert text, type string1 and press Esc, the system inserts string2
:ab Show all abbreviations
:una string cancel string abbreviation
Indent text in vi
Option
When ctrl+i or tab is inserted into the text, the width of the movement is inserted, and the width of the movement is defined in advance.
:set ai turn on automatic indentation
:set sw=n sets the movement width to n characters
n> Move n lines to the right by a width, for example 3>> to move the next three lines and each line to the right by a moving width

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.