Vim basic command learns to be able to basically operate Linux

Source: Internet
Author: User
Tags save file

1.
vim
#Enter vim in the command line to enter the vim editor
2.
i
#Press the i key once, --INSERT--
#Insert command, any character may have effect in vim
3.
Esc
#Exit i (insert) command for other commands
4.
: r filename
#Read in a file content and write it to the current editor
5.
: w newfilename
#Write the contents of this editor to a new file
6.
: w
#Save the file during editing, equivalent to ctrl + s in word
7.
:! command
Leave vi for a while and execute command 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 vim to edit.
Under the shell command, execute ctral + l to clear the screen
9.
: wq
#Save the file and exit
10.
ZZ
#Save the file and exit, the same as the previous command, pay attention to capitalization
11.
: q!
#Force exit without saving
12.
: set number or: set nu
#Make the line number of the file being edited
13.
: set nonumber or: set nonu
#Contrary to the previous command, no line number is displayed
14.
: help i
#View insert command help
15.
u
#Undo the previous step
16.
/ Fedora
#Find Fedora Characters
17.
: s / Fedora / Redhat
#Replace Fedora characters with Redhat
18.
dw
#Delete words
dd
#Delete line
19.
o
#Open blank line
20.
vim + filename
#Edit the last line of the file
twenty one.
vim + n filename
#Enter the nth line of the file for editing
twenty two.
: 1, .s / redhat / fedora
The #. Number indicates the current line, that is, the line where the cursor is located
#Replace the first redhat character from line 1 to the current line (.) With fedora
twenty three.
: 1, .s / redhat / fedora / g
#Replace all occurrences of redhat characters from line 1 to the current line (.) With fedora, g
Global flag
twenty four.
: 1, $ s / redhat / fedora / g
# $ Means the last line
#Replace all occurrences of redhat characters from line 1 to the last line with fedora
25.
:% s / redhat / fedora / g
#Same as the previous command
26.
:% s / \ / fedora / g
#Replace all occurrences of redhat words from line 1 to the last line with fedora
# 字, not characters
27.
: f
#Show file content, status, etc.
#Same as ctrl + g
28.
: e!
#Current file, return to last save
: e file
#Toggle editing files
29.
: n
#Switch to the next file when there are multiple files during editing (such as vim file1 file2), with: e file
In conjunction with




                              

                             VIM Command Daquan
Cursor control command
Command Cursor Move
h move one character to the left
j Move down one line
k move up one line
l Move one character to the right
G moves to the last line of the file
w move to the beginning of the next word
W moves to the beginning of the next word, ignoring punctuation
b move 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 line of the screen
H Move to the first line of the screen
e move 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
) Move to the end of the sentence
{Move to the beginning of the paragraph
} Move to the beginning of the next paragraph
0 (number), | move to the first column of the current row
^ Move to the first non-empty character on the current line
$ Moves to the last character of the current line
+, Enter move to the first character of the next line
-Move to the first non-empty character of the previous line
Add text in vi
Command Insert Action
a Insert text after cursor
A inserts text on the current line
i Insert text before the cursor
I insert text before the current line
o Insert new line below 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 under the cursor and enter insert mode
: r file read the contents of file file and insert it after the current line
: nr file read the contents of file file and insert it after line n
Esc returns 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 delete line until end of file
dd delete entire line
db delete the word before the cursor
: n, md delete n lines starting from line m
d, d $ delete from cursor to end of line
^ h, backspace delete previous characters when inserted
^ w delete the previous word when inserted
Modify vi text
The number in front of each command indicates how many times the command is repeated
Command Replace operation
rchar replaces the current character with char
R text escape replaces the current character with text until the Esc key is pressed
stext escape replace current character with text
S or cctext escape replace 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 to the end of the file
ccursor_cmd text escape is changed from the current position to the position of the cursor command
Find and replace in vi
Command Find and Replace
/ text look forward text in file
? text find text backward in the file
n Repeated search in the same direction
N repeated lookups in opposite directions
ftext find text forward on current line
Ftext finds text backwards in the current line
ttext searches text forward on the current line and positions the cursor at the first character of text
Ttext finds text backwards on the current line and positions the cursor at the first character of text
: set ic ignore case when searching
: set noic case sensitive when searching
: ranges / pat1 / pat2 / g replace oldtext with newtext
: m, ns / oldtext / newtext passes n through m, replacing oldtext with newtext
& Repeat the last: s command
: g / text1 / s / text2 / text3 finds lines containing text1 and replaces 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 Place text in the temporary buffer after the cursor
P Place text in temporary buffer before cursor
"(a-z) nyy copies n lines into a nameable buffer whose name is in parentheses, omitting n means the current line
"(a-z) ndd deletes n lines into a nameable buffer whose name is in parentheses, omitting n means the current line
"(a-z) p puts the contents of the namable buffer named parentheses after the current line
"(a-z) P puts the contents of the namable buffer named parentheses before the current line
Undo and repeat in vi
Command Undo operation
u Undo the last modification
U Undo all changes to the current line
. Repeat last modification
, Repeat the previous f, F, t or T find command in the opposite direction
; Repeat previous f, F, t or T find command
"np retrieves the last nth deletion (a certain number of deletions are stored in the buffer, usually 9)
n repeat the previous / or? find command
N Repeat previous / or? Commands in opposite directions
Save text and exit vi
Command Save and / or Exit
: w save file without exiting vi
: w file save changes in file without exiting vi
: wq or ZZ or: x save the file and exit vi
: q! Don't save the file, exit vi
: e! Discard all changes, edit from last saved
in vi Option
Option Function
: set all print all options
: set nooption
: set nu print line number before each line
: set showmode show input mode or replace 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 tab (^ I) and end-of-line symbols
: set ts = 8 set tab stops for text input
: set window = n Set the text window to display n lines
: set number shows the number of lines
: set nonumber suppress line number
vi status
Option Function
:. = Print the line number of the current line
: = Print the number of lines in the file
ctrl + g displays file name, current line number, total number of files, and percentage of file position
: l Use the letter "l" to display many special characters, such as tabs and newlines
Position paragraphs and place marks in text
Option Function
{Insert {in the first column to define a paragraph
[[Back to the beginning of the paragraph
]] Move forward to the beginning of the next paragraph
m (a-z) mark the current position with a letter, such as mz
‘(A-z) moves the cursor to the specified mark, such as‘ z to move to z
Concatenate lines in vi
Option Function
J joins the next line to the end of the current line
nJ concatenate n lines
Cursor placement and screen adjustment
Option Function
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) scroll the screen
ctrl + y scroll down the screen
ctrl + u scroll the screen half a page
ctrl + d Scroll down half a page
ctrl + b Scroll the screen one page
ctrl + f Scroll down the screen
ctrl + l redraw the screen
z-return put the current line as the top line of the screen
nz-return sets the nth line below the current line as the top line of the screen
z. Center the current line on the screen
nz. Center the nth line on the current line
z- Make the current line the bottom line of the screen
nz- Set the nth line on the current line as the bottom line of the screen
Shell escape commands in vi
Option Function
:! command executes a shell command, such as:! ls
: !! execute previous shell command
: r! command reads the command command and inserts it, for example: r! ls executes ls first, then reads the content
: w! command takes the currently edited file as the standard input of 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 launch a subshell, returning vi using ^ d (ctrl + d)
: so file read and execute commands in shell program file
Macros and abbreviations in vi
(Avoid using control keys and symbols, don't use the characters K, V, g, q, v, *, =, and function keys)
Option Function
: map key command_seq defines a key to run command_seq, such as: map e ea, e can be moved to the end of a word at any time to append text
: map displays all defined macros in the status line
: umap key macro to delete the key
: ab string1 string2 Defines an abbreviation such 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 Function
ctrl + i or tab When inserting text, insert the moving width. The moving width is defined in advance.
: set ai turn on automatic indentation
: set sw = n sets the shift width to n characters
n> moves n lines to the right by one width, for example 3 >> moves the next three lines to the right by one movement width

vim basic commands learned basic operations on linux


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.