Linux VIM Basic command

Source: Internet
Author: User
Tags save file

Linux VIM command:
Vim Enter vim on the command line to enter the vim editor
Esc exits the i (insert) command for other commands
:sh Enter the shell command line, after running the command, ctrl+d exits again and enters vim editing to continue editing under the shell command.

Run ctral+l to finish clearing
:set number or :set nu causes the file in the edit to display the line number
:set nonumber or :set nonu Contrary to the previous command, no line number is displayed
:help i View insert command help
/Fedora Find Fedora Characters
:s /Fedora/Redhat Replace Fedora characters with Redhat (just replace the line where the cursor is)
:%s/ro../r Replace ro.. with r. . indicates random characters
:1,.s/redhat/fedora . indicates the current line, that is, the line where the cursor is located # Replace the redhat character of the first line to the current line (.) for the first time.
:1,.s/redhat/fedora/g Replace all the redhat characters from the first line to the current line (.) to fedora, g
Global flag
:1, $s/redhat/fedora/g $ indicates the last line # Replace the redhat character from the first line to the last line with fedora
:%s/redhat/fedora/g Same as the previous command
:%s/\/fedora/g Replace the redhat word that appears in line 1 to the last line with the fedora# word instead of the character
:%s/ro/\r&/gc % indicates the full text, and s indicates replacement. Replace ro with a newline. & indicates the character ro to be replaced before. g means that the replacement is always replaced with no g, and c is used to confirm whether to replace.
The whole sentence means replacing ro with a newline followed by a ro
:f #display file content, status, etc. with the ctrl+g command
Vi filename means to edit the filename file. If the file does not exist, create the file. If you do not specify a file name when you enter vi, use ":n filename" to specify the file name.


:e! Give up all changes, not including saved
:e file name Open the specified file
D delete all characters after the current line cursor
y and y$ are the same characters that copy the cursor from the beginning of the line to the end of the line.
:w! File Name Overwrites existing files with the currently displayed text
:n,mw! file name Forces the nth to mth lines of the currently displayed text to the specified file
w cursor jumps to the beginning of the next word
e The cursor jumps to the end of the word in turn
Replacement text
Cc replaces the current line
C and c$ replace all characters from the cursor to the model in the current line
Cw assumes that the cursor is at the head or middle of the word, replacing it from the cursor to the end of the word. Suppose the cursor is at the end of the word. Replace the last letter
Ce assumes that the cursor is at the head or middle of the word. Replace from the cursor to the end of the word, assuming the cursor is at the end of the word, then replace the tail from the last letter to the end of the next word
Cb Suppose the cursor is in the middle of the word and is replaced by the cursor at the head of the word, assuming the cursor is at the head of the word. Then replace the previous word

Buffer
The buffer is divided into a digital buffer and a letter buffer
Digital buffer:
Each dd delete operation saves the deleted content into a digital buffer, and the first buffer is always the last deleted content. Each delete operation saves the contents of buffer 1 into buffer No. 2. No. 2 is deposited in No. 3. And so on. The call can be commanded with "np (n represents the number in 1-9)
Letter buffer:
There are 26 letter buffers, respectively a-z
The command "add" means to delete this line. Save this line in the a buffer.

Call can be commanded with "ap"

Cursor control command
Command cursor movement
: Up can repeatedly enter the previous command
h Move one character to the left
j moves down one line
Nj moves n lines down
k moves up one line
l Move one character to the right
Gg or :1 move to the first line of the file
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. Ignore punctuation
e moves to the end of the next word
E moves to the end of the next word. Ignore punctuation
b Move to the beginning of the current word or 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
(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 at the end of 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. 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
Delete text in vi
Command delete operation
x Delete the character at the cursor and delete it backwards
Nx deletes the n characters after the start of the cursor, excluding other lines
X delete the word before the cursor and delete it forward
Dl delete a character
Dw delete to the beginning of the next word
Daw delete current word
Nn words after dnw or ndw deletion
Dap delete a paragraph
dG deletes the cursor line and afterwards until the end of the file
Dd delete the entire line
Nd delete the n lines under the cursor
Db delete the word in front of the cursor
:n,md delete n lines to m lines
d, d$ is removed from the cursor to the end of the line
d$ is removed from the cursor to the end of the line
d,d^ is deleted from the cursor to the beginning of the line
D^ delete from the cursor to the beginning of the line
Change vi text
The number in front of each command indicates the number of times the command is repeated.
Command replacement operation
G~~ The uppercase and lowercase inversion of the line where the cursor is located
Guu cursor line is lowercase
The letter of the gUU cursor is capitalized
gUaw converts the current word to uppercase
gUap converts the current paragraph to uppercase
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 changes to the end of the file
Find and replace in vi
Command find and replace operation
f character Find the next character in this line
; Move to the next character searched with f, used with f above
, when moving to the character found on the previous f. Contrary to
* Full text search moves to the next word that is the same as the current word of the cursor
/text finds text forward in the file
?text finds text backwards in the file
n Find it repeatedly in the same direction
N repeatedly looking in the opposite direction
:set ic ignores uppercase and lowercase when looking up
:set noic is sensitive to uppercase and lowercase when looking up
:ranges/pat1/pat2/g replace oldtext with newtext
:m,ns/oldtext/newtext passes n on the m line. Replace oldtext with newtext
& repeated last :s command
:g/text1/s/text2/text3 Finds the line that includes text1. Replace text2 with text3
:g/text/command Runs the command represented by command on all lines that include text
:v/text/command Runs the command represented by command on all lines that do not include text
Copy text in vi
Command copy operation
Yy puts the contents of the current line into the temporary buffer
Nyy puts the contents of n lines into the temporary buffer
p puts the text in the temporary buffer into the cursor
PPut the text in the temporary buffer before the cursor
Np puts the text in the temporary buffer n times after the cursor
nP puts the text in the temporary buffer n times before the cursor
Undo and repeat in vi
Command undo operation
u Undo the previous step
U Undo all changes to the current line
Ctrl+r is typed repeatedly, as opposed to undo
Repeated last change
, repeat the previous f, F, t, or T lookup command in the opposite direction
; Repeat the previous f, F, t or T lookup command
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 save changes in file without exiting 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, edit from the last save file
:set go= go represents the abbreviation of gui option. Equal to empty is nothing
Vim file1 file2...filen can open multiple files (entered in the shell)
Vim + filename to edit the last line of the file
Vim +n filename Enter the nth line of the file for editing
:e xx.txt open a file
:open file Open a file, same as above
:tabnew D:\text.txt Open a file in the new tab, type: tabnew D:\t and press tab, you will automatically complete the text.txt. Assume that the tab is pressed incorrectly a few times until it is correct.
:bn file switch, next file
:bp file switch. Previous file
Ctrl+6 file switch, next file
:split multiple files arranged up and down, able to exit the current form by :q
:vsplit multiple files arranged side by side
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 suitable for multi-line gaze
:set smartindent provides its own active 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 form to display n lines
Status of vi
Option
Ctrl+g displays the file name, the current line number, the total number of lines in the file, and the percentage of the file location
:l Use the letter "l" to display a lot of 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
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
Ctrl+e 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+o exits the insert mode and enters the normal mode. After the operation, it enters the insert mode.
Ctrl+p edit mode to actively complete
Zz Redraw the screen so that the current line is moving in the middle of the 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
:syntax on syntax highlighting
:syntax enable syntax highlighting
:syntax clear cancel highlighting
:color color followed by a space and then press the table to switch the color scheme
:set hls highlights the word

Macro
Qa (q means to start recording the macro, the name of the macro is a)
Rerecord operation
It is best to press q (indicating the end of the macro recording)
Use: @a, the macro runs once.

[email protected]. Indicates that the macro a is run 6 times.

Shell escape command in vi
Option
:!command Run the shell command command, such as :!ls
:!! Run a previous shell command
:r!command Reads the input of the command command and inserts it. For example: r!ls will run ls first. Then read in the content
:w!command Take the currently edited file as the standard input for the command command and run the command command. Such as :w!grep all
:cd directory Change the current working folder to the folder represented by directory
:sh will start a subshell. Use ^d(ctrl+d) to return vi
:so file Read and run 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 Defines a key to run command_seq, such as :map e ea. Either 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. Insert the width of the move. The movement width is defined in advance
:set ai open yourself to actively indent
:set sw=n sets the movement width to n characters
>> Move the current line to the right by one width
n>> Move n lines to the right by a width. For example, 3>> moves each of the next three lines to the right by a movement width. It is assumed that omitting n means moving the current line to the right by one width.


>G moves the current line to the end of the document by one width to the right

Visual mode
In order to facilitate the selection of text. VIM introduces the Visual mode.

To pick a piece of text, first move the cursor to the beginning of the paragraph. Press v in normal mode to enter visual mode. Then move the cursor to the end of the paragraph to select the content (move to the beginning of the line ^, the end of the line $). Need to pay attention. The character where the cursor is located is included in the selection.

At this point, you can perform some operations on the selected text. The frequently used (visual mode) commands are:
x or d cut (ie delete, the selected text at the same time enters the clipboard)
y copy
r character Replace all characters with new characters
u U ~ each is all lowercase, uppercase, reverse uppercase and lowercase
When the command is entered. VIM will return to normal mode, where you can press p or P to paste. Commands for copying and pasting in normal mode:
v Enter visual mode
p or P pastes the contents of the clipboard at the current position, p sticks to the character behind the cursor, P sticks to the front
I have to admit that although the visual mode was introduced. Copying and pasting in VIM is still a cumbersome operation, which is perhaps the only drawback of VIM. In addition, VIM introduces the Select mode. Similar to the visual mode.

Combine some key definitions and option settings.

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.