Complete VI Editor-are you using VI well?

Source: Internet
Author: User
Tags control characters

1) File Operations
Vi FileName open the file FileName and place the cursor at the beginning of the first line.
Vi + n FileName open the file FileName and place the cursor at the beginning of line n.
Vi + FileName open the file FileName and place the cursor in the last line.
Vi +/pattern File open the File and place the cursor at the first string matching pattern.
Vi-r FileName is restored after a system crash occurs when the file name is edited by vi last time.
Vi File1... Filen open multiple files and edit them one by one.
: %! Xxd view the current file in hexadecimal format
: %! Xxd-r returns normal mode from hexadecimal
: N1, n2 co n3 copy the content from line n1 to line n2 to line n3.
: N1, n2 m m3 move the content from line n1 to line n2 to line n3.
: N1, n2 d Delete content between n1 row and n2 row.
: N1, n2 w filename: Save content from line n1 to line n2 to file filename
: N1, n2 w! Command takes the content from line n1 to line n2 in the file as the Command input and runs it,
If n1 and n2 are not specified, the entire file content is used as the Command input.
: R! Command: place the output result of the Command to the current line.
: Nr <File> insert <File> to row n
: So <File> Read <File>, and then execute the commands in the file (the commands in the file should all be some ex commands)
: L1, l2w <File> write the text between line l1 and line l2 to <File>
: W> <File> Add to <File> end. You can also use the row number.
: E! Reedit the current file and ignore all modifications.
 
· (, [,],) Corresponding display
% Displays the corresponding items of the current (, [, {,},],)
) Display the corresponding (
) Display the corresponding [
] Display the corresponding {

· (, [,],) Data selection
DaB deletes {} and its content (in non-v visual mode)
DiB deletes content in {}
Content in AB selection ()
Content in ib selection () (excluding ())
Content selected in AB {}
Select Content in iB {} (excluding {})
· Syntax prompt and Automatic completion
★<C-N> <C-P> Automatic completion of words in insert mode
★<C-X> <C-L> automatic line completion (super useful)
· Set ctags
# Ctags-f/usr/share/vim/vim63/funcs. tags-R/opt/j2sdk/src
/Usr/src/kernels/2.6.9-5. EL-i686
^ P automatically fills in similar context items
^ N auto-completion ~ /. Similar functions in tags
^ [Display ~ /. The prototype of the function under the cursor in tags,
Exit the function by ^ t.
: The pta function name preview window quickly opens the file where the corresponding function is located, and positions the cursor at the beginning of the corresponding function
K. man manual for displaying C functions under the cursor
· Variable locating
Gd goes to the definition of the local variable under the cursor
Gd goes to the definition of the global variable under the cursor

· Compilation options
: The cn command will take you to the next error location, regardless of the file in which it is located.
: The cc command will show you the compiler output information of the current error;
: Cl will generate a list of all project errors in the column for viewing these errors
 
3] move the cursor
· Character
H move the cursor one character to the left.
L move the cursor one character to the right.
· Word
W or W move one word to the beginning of the word to the right of the cursor.
B or B move one word left to the beginning of the word.
E or e move one word to the end of the word to the right of the cursor.
· Sentence} move the cursor to the end of the sentence.
(Move the cursor to the beginning of the sentence.
· Segment) move the cursor to the beginning of the paragraph.
{Move the cursor to the end of the paragraph.
· Move a row up with the cursor k or Ctrl + p.
J or Ctrl + n move the cursor down a row.
Enter: move the cursor down a row.
NG cursor moves to the beginning of line n.
N + move the cursor down n rows.
N-move n rows above the cursor.
N $ move the cursor to the end of line n.
0 move the cursor to the beginning of the current row.
$ Move the cursor to the end of the current row.
· Screen
H move the cursor to the top line of the screen.
M move the cursor to the middle line of the screen.
L move the cursor to the last line of the screen.
Ctrl + u to the first half of the screen.
Ctrl + d flip the half screen to the end of the file.
Ctrl + f.
Ctrl + B.
Nz rolls row n to the top of the screen. If n is not specified, the current row is rolled to the top of the screen.
4} insert
# Insert the row number in the file (not the display line number, but the insert !)
★: G/^/exec "s/^/". strpart (line ("."). "", 0, 4)
· Cursor
I insert before the cursor.
A is inserted after the cursor.
· Line
I is inserted at the beginning of the current row.
A is inserted at the end of the current row.
O Insert a new row under the current row.
O Insert a new row above the current row.
 
5) replace
R replaces the current character.
R replaces the current character with the subsequent character until you Press ESC.
S starts from the current cursor position and replaces the specified number of characters with the input text.
S deletes the specified number of rows and replaces them with the input text.
6) modify
Ncw or nCW modifies a specified number of characters.
NCC modifies a specified number of rows.
: R filename: insert the file filename under the current row.
7) search and replace
/<C-R> <C-W> write narrow word <cword> to search Command Line
/<C-R> <C-A> write generalized word <cWORD> to search Command Line
: G/str/s/str1/str2/g
The first g indicates that each row including s1 is replaced,
The second g Represents replacing all of the rows.
Replace str1 with str2 for all rows including str
: % S/f $/for $/g Replace the "f" (f key and Space key) at the end of each line with the
: % S/^/mv/g Add "mv" (mv key and Space key) to the header of each line)
: S/fred/<c-r> a/g replace fred with the content in register.

: G/<input | <form/p displays rows containing <input or <form

# Replacing a visual area
# Select a region and enter s/Emacs/Vim/. vim automatically enters the mode.
: '<,'> '<,'> Before s/Emacs/Vim/g '<,'> is automatically added by vim.

# Search for multiple documents
: Bufdo/searchstr
: Argdo/searchstr
 

Copy and cut
The position of two characters before and after the xp Switch
Exchange the positions of the upper and lower rows of ddp
: G/fred/t $ copy the row from fred to the end of the file (EOF)
 
9) window operations
: Vne [filename]
: Sp [filename]
: S [filename]
: New [filename]
: ^ W + ^ r swap the positions of the two windows
^ W = window width
: Res-n window height reduced by n
: Res + n increase the window height n
: Vert res-n
: Vert res + n
10) convert DOS text to Unix text
: 1, $ s/^ M/g
11) bookmarks
When reading and writing large program files, it is very helpful to use the tag (bookmarks) function to locate them.
Move the cursor to the position you want to mark. If you make a tag named "debug1", you can enter the tag command "mdebug1" in command mode, and then press the Enter key, A tag named "debug1" is ready.
Next, you can move the cursor to another position at will. When you enter "'debug1" in command mode, you can quickly return to the first line of the line marked by "debug1.

12) Delete
: % S/r // g press enter to delete DOS ^ M
: % S = * $ = Delete the blank space at the end of the row
: % S/^ (. *) n1/1 $/delete duplicate rows
: % S/^.pdf/newusers/only deletes the first pdf.
: % S/<! -- _. --> // Delete the multi-line comment (comment? Why do we say "again ?)
: G/^ s * $/d delete all empty rows
: G! /^ Dd/d Delete the row without the string 'dd'
: V/^ dd/d same as above (translated as v = g !, Is not matched !)
: G/str1/,/str2/d delete all the rows from the first entry containing str1 to the first entry containing str2.

: V/./,/./-1 join compressed empty rows
: G/^ $/,/./-j compressed empty rows
Ndw or ndW deletes n-1 characters starting at and following the cursor.
Delete d0 to the beginning of the row.
D $ Delete to the end of the row.
Ndd deletes the current row and the next n-1 row.
Delete a character from x or X.
Ctrl + u Delete the text entered in the input mode.
^ R restore u
J. Merge the next row to the end of the current row
V select a row
^ V press ^ V to select a rectangle.
Aw select word
Iw internal words (no space)
As Select sentence
Is Select sentence (no space)
Ap selection Section
Ip selection section (no space)
D. Delete it to the end of the row.
X, y Delete and copy contain highlight Area
Dl deletes the current character (same as the x command)
D0 is deleted to the starting position of a row.
D ^ Delete the first character (excluding spaces or TAB characters) to a row)
Dw is deleted to the end of a word.
D3w is deleted to the end of the third word.
Db is deleted to the start position of a word.
DW is deleted to the end of a word with a space as the separator.
DB is deleted to the start position of a word with space as the separator.
D7B is deleted to the start position of the first seven words with spaces as separators.
D) Delete to the end of a statement
D4) to the end of the fourth statement.
D (delete to the start position of a statement)
D) delete it to the end of a paragraph.
D {Delete to the beginning of a paragraph
D7 {Delete the first 7th paragraph positions before the start position of the current paragraph
Dd deletes the current row
D/text: Delete the position specified by "text" from the text,
Forward until the content of the next occurrence (but not including the word)
Dfc deletes the position where the character "c" appears from the text and continues until the next occurrence of the character (including the character ).
Dtc deletes the content from the current row until the next character "c" appears
D. Delete it to the end of a row.
D $ Delete to the end of a row
5dd Delete the contents of the five rows starting from the current row
Delete dL content until the last line on the screen
DH Delete until the first line of content on the screen
Delete content from the dG until the end of the work Cache
D1G Delete content that begins in the work Cache
Modify command operation
R change current character
Cw to the end of a word
Modify c3w to the end of the third word
Change cb to the start position of a word
CW changes to the end position of a word with a space as the separator.
CB is changed to the start position of a word with spaces as the separator.
C7B is changed to the start position of the first seven words with spaces as separators.
C0 to the end of a row
C} modify to the end of a statement
C4} changed to the end of the fourth statement.
C (modify to the starting position of a statement)
C) modify it to the end of a paragraph.
C {modify to the start position of a paragraph
C7 {change to the first 7th paragraph positions before the current paragraph start position
Ctc modifies the content of the current row until the next character c appears
C. modify it to the end of a row.
Cc modify current row
5 cc modify the content of 5 rows starting from the current row
. Repeat the previous modification!

13} Set options
Set all lists the settings of all options.
Set term to set the terminal type.
Set ignorecase ignores case sensitivity in search.
Set list displays the stop (^ I) and end signs ($ ).
Set number indicates the row number.
Set showmode indicates the mode in which the user is located.
Set report displays the number of rows in the Country modified by the line-oriented command.
Set terse displays brief warning information.
When set warn is transferred to another file, No write information is displayed if the current file is not saved.
Set autowrite in ": n" and ":!" The file is automatically saved before the command.
Set nomagic allows you to use special characters that do not contain \ In search mode.
Set nowrapscan prohibits vi from starting from the other end when the search reaches both ends of the file.
Set mesg allows vi to display the information that other users write to their terminal using write.
Autoindent (ai) noai makes the new line automatically indent, and alignment with the position of the upper (lower) Row
After each command of autoprint (ap) ap, the changes are displayed.
Autowrite (aw) noaw at: n ,:! The file is automatically saved before the command.
Beautify (bf) nobf ignores all control characters (except tab, newline, and formfeed) during input ))
Directory = (dir =)/tmp directory name for storing the buffer
Usage of edcompatible noedcompatible Class ed when replacing
Errorbells (eb) errorbells rings when an error occurs
Exrc (ex) noexrc allows the. exrc file to be placed outside the home directory.
Hardtabs = (ht =) 8. Set the boundary of the hard tabulation.
Ignore case (ic) noic in the regular expression ignore case sensitivity
Lisp nolisp open the lisp Mode
List nolist displays all tabulation keys and the end of a row
Magic agic can use more regular expressions
Mesg allows sending messages to terminals
Number (nu) nonumber display row number
Open allows openness and Visualization
Optimize (opt) optimize optimizes throughput, and press Enter when printing
Paragraphs = (para =) IPLPPPQPPLIbp sets the {&} delimiter
In prompt command mode, the following prompt is displayed:
Readonly (ro) noro unless used! No. Otherwise, the file cannot be saved.
Redraw noredraw redraws the screen when editing
Remap allows macros to point to other macros
Report = 5 if the number of affected rows is greater than this number, report
The number of scrolling screens in the scroll 1/2 window, which is also the number of lines output by the z command (twice the size of the screen)
Sections = shnhh hu defines the end of the section (when the command [[and] is used)
Shell = (sh =)/bin/sh default SHELL. If the environment variable SHELL is set, use the variable
Shiftwidth = (sw =) 8 number of characters that are moved when the shift command is used
Showmatch (sm) nosm shows matching conditions of {,}, (,), [, or]
Showmode noshowmode shows the mode in which you are in.
Do not update the display immediately after slowopen (slow) is inserted.
Tabstop = (ts =) 8. Set the length of tabstop.
Taglength = (tl =) 0 Number of important characters (0 indicates all characters)
Tags = tag,/usr/lib/tags defines the file path containing the tag
Term = set the terminal type
Terse noterse displays brief error messages
Timeout (to) timeout: the keyboard ing times out after one second
Ttytype = set the terminal type
Warn displays "No write since last change" Information
Window = (w =) number of rows in the visible mode
Wrapmargin = (wm =) 0 right margin. If it is greater than 0, the rightmost word will be broken, leaving n blank positions
Wrapscan (ws) ws finds the end of the file and starts starting with another header.
Writeany (wa) nowa can be saved to any file
14} special characters
^ The matched string is at the beginning of the line.
$ The matched string is at the end of the line.
. Used in a pattern string to indicate any single character.
In command mode, repeat the previous command.
* In a mode string, the prefix can appear multiple times.
[] Is used in a mode string to indicate characters in the specified azimuth, where "-" indicates a word.
Character range. Use ^ to indicate characters that are not in a certain range.
ESC switches from the insert status to the command status
^ [Same function as ESC
15] case-sensitive Conversion
Guu row lower case
GUU rows in uppercase
G ~~ Line flip (case sensitive)
<G id = "1"> guw </G>
GUw uppercase (narrow words)
G ~ W-word flip (narrow words)
VEU uppercase (generalized)
VE ~ Word flip (generalized word)
GgguG writes the entire article in lower case (ft! Bt !)
16) Jump to the footprint. Jump to the last modified line (super useful) (ft, how is this comment)
'. Not only the last modified line, but also the modification point.
<C-O> jump back to your redirect record (from the last time)
<C-I> jump forward along your jump record in turn
: Ju (mps) to list your jumps
17) command history
: History: List historical Command records
: His c command line command history
: His s SEARCH Command history
Q/window for searching command history
Q command line command history window
: <C-F> history Command record window
18] registers
# List Registers (Registers)
: Reg: display all current registers
"1 p indicates referencing register, 1 indicates a register named 1, and p indicates the paste command.
Explanation:
"Also used to define register
Enter "to define register
Enter the name, for example, 0 ~ 9, ~ Z
Then run the delete or Copy command, such as dd or y,
Or d (delete the selected part) or y (copy the selected part) in visual mode)
The deleted or copied part is stored in the named register.
Observation: a special register, "", contains an anonymous delete/copy
When you execute dd or y, the affected part is saved "".
19} command line
"Ayy @ a runs the current line as a Vim command
Explanation: "ayy defines the current row to register a, and @ a executes the commands stored in register.
10yy copy 10 rows below the current row
11} sorting
: %! Sort-u uses the sort program to sort the entire file (rewrite the file with the result)
! 1) sort-u sort the current section (it can only be used in normal Mode !!)
:., + 5! Sort sorts the current row and the following five rows
20) Column Operations
: % S = [^] + $ = & = copy the last column
: % S = f + $ =& & = same features
: % S = S + $ = & ft, same
: S/(. *). *)/2 "1/Use: Split two fields
: % S (w + s +) str1: 1str2: process column, replace all str1 In the third column
: % Sw +) (. * s +) (w +) $: 321: swap the first and last columns (4 columns in total)

·. Vimrc
"Use Vim settings, rather then Vi settings (much better !).
Set nocompatible
"C-style indent
: Set expandtab "use spaces instead of tabs
: Set cindent shiftwidth = 4
"Auto indent
: Set ai

"Syntax
": Set filetype = java
: Set syntax = java
"Type),],}, display (,[,{
: Set showmatch
"Manual folding
: Set foldmethod = manual

"Tag File Location
Set tags =/usr/share/vim/vim63/funcs. tags
"In insert mode, press [Ctrl] N to automatically complete any class, method, or field name.
Set complete + = k
"Don't bother me with your voice!
Set visualbell
"History
: Set history = 50
"Display row and column positions
: Set ruler
"Set character encoding
Set fileencodings = gb2312
"Set encoding = euc-cn
"Set ruler
Set ruler
"Display current command
Set showcmd
"Incsearch
Set incsearch
"Allow backspacing over everything in insert mode
Set backspace = indent, eol, start
"Automatically detect file types
: Filetype on

"Shortcut key definition """"""""""""""""""""""""""" """"""""""""""""""""""""""""""""
If version> = 600
"Search
"Map <C-H>/
"Search function prototype
Map <C-H> yiw/^ \ (\ s \) * \ (sub \) \ | \ (function \) \ (\ s \) * <C-r> "<CR>
"Continue searching
Map <F3> n

"Use <F10> to display the list of classes, attributes, and methods
Map <F2>: Tlist <CR>
"Compile the current program with the Compilation Program defined by compiler
"Map <F9>: make" % "<CR>
"Use <F11> for php syntax Detection
Map <F11> :! Php-l "%" <CR>
Map; w: call FormatText () <CR>
"Search for words under the current cursor
Map <F4>/<C-R> <C-W> <CR>
Endif
""""""""""":: Shortcut Key definition """""""""""""""""""""""""""""""""""""" """""""""""""""""""
"Format File """"""""""""""""""""""""""" """"""""""""""""""""""""""""""""
Function FormatText ()
: % S/\ t // g "to replace \ t with four spaces
: % S/* $ // g "Remove spaces at the end of the line
Endfunction
"Format File """"""""""""""""""""""""""" """"""""""""""""""""""""""""""""
 
"Display the tabs and extra spaces at the end of a line """""""""""""""""""""" """""""""""""""""""""""""
Set listchars = tab:>-, trail :~
Set list
Syntax match Trail "+ $"
Highlight def link Trail Todo
"""""""""":: Display the tab and extra spaces at the end of a line """""""""""""""""""""""""""""""" """""""""""""
Function LastMod ()
If line ("$")> 5
Let l = 5
Else
Let l = line ("$ ")
Endif
Exe "1,". l. "/CnsProgCMTime/s/CnsProgCMTime. */CnsProgCMTime ".
Strftime ("% Y-% m-% d % H: % M: % S"). "/e"
Endfunction
"Map <F5>: call LastMod () <CR>
"Automatically adds an asterisk """"""""""""""""""""""" """""""""""""""""""""""""""""
"When Using comments such as/*... */, r inserts a value when pressing enter in insert mode *;
"O is inserted when" o "or" O "is used in Normal Mode *
: Set formatoptions = tcro
""""""""""":: Automatically add the * Asterisk """""""""""""""""""""""""""""""""" """"""""""""""""
 
Vi Extension Regular Expression
| Indicates alternating, for example, home | house indicates matching the home or house
() Used for grouping, such as home | house can be written as ho (me | use)
* You can use () (home | house) * to match home, homehouse, househome, and homehousehomehouse.
\ (... \) The actual matched text can be searched by, and so on in the replacement part of the replacement command
+ The regular expression can be matched once or multiple times, including a single character, (), or \ (... \).
Ho (use | me) + indicates that at least house or home cannot be empty.
? Indicates the occurrence or absence of zero or one occurrence of the previous regular expression. Such as free? D will match fred or freed. This cannot match other texts.

Defines the range expression. The interval expression describes the number of duplicates. Same \
(Home | house) can only match homehome, homehouse, househome, househouse
 

Compilation
· Use a compiler to compile the current file
:! Perl-c %
:! Php-l % Use php to check errors
:! Php-q % Use php to run the program

· Compile the current program as a. out and execute this./a. out
:! Gcc %-o a. out &./a. out
Execution result
Old umask is % #0
New umask is % #0
· Compilation error message
: Cl! List details of all error messages
: Cl: list the current error message
: Cn: list the next error message
: Cp: list the previous error message

: Number of latest cnew error messages
: Number of old cold error messages
: Cwin or: copen open the error message window
: Cclose close the information window opened above
 
Auto-completion
CTRL-X CTRL-F file name
CTRL-X CTRL-L whole line
CTRL-X CTRL-D macro definition (and search for included files)
CTRL-X CTRL-I current file and included file
Words in CTRL-X CTRL-K dictionary files
Words in CTRL-X CTRL-T synonym dictionary files
CTRL-X CTRL-] Mark
CTRL-X CTRL-V Vim command line
 
C Program Design
The gd reversed color shows all the occurrences of the local identifier string under the cursor and goes to the first occurrence.
The gD reversed color shows all the occurrences of the global identifier string under the cursor and goes to the first occurrence.
Bytes --------------------------------------------------------------------------------------
: Checkpath: list all include header files
Bytes --------------------------------------------------------------------------------------
[I display function definitions
Extern _ mode_t umask (_ mode_t _ mask) _ THROW;
Bytes --------------------------------------------------------------------------------------
[+ CTRL + I jump to [Function Definition marked by I
Region [I
List the current file and the lines containing the current characters in the file
Else [d
Display the Definition Statement of the identifier defined by # define
Printf (| tom );
# Define tom "yaoshuyin"
Bytes --------------------------------------------------------------------------------------
CTRL +] Jump to the label at the current position of the cursor (for example, the definition of a function ).
CTRL + T jump to the place before executing CTRL-] command.
: Tselect selects one from a series of matching tags.
[// Jump to the start position of the comment.
]/Jump to the end of the comment.
----------------------------------------------------------------
[# Return to unclosed # if, # ifdef, or # else.
] # Move forward to unclosed # else or # endif.
[(Return to unclosed.
) Forward to the unclosed.
[{Jump to the nearest.
} Jump to the nearest.

· Character Set and hexadecimal conversion
Ga displays the ascii, hex, oct,... heap conversion of the characters at the cursor
For example, press ga before s
<S> 115, Hex 73, Octal 163
Column block Operation
<C-V> selected column Blocks
I <string> <ESC> Add a string to the beginning of each line of the block
A <string> <ESC> Add A string at the end of each line of the block
C <string> <ESC> modify the selected column block (delete and enter new content)
C <string> <ESC> modify the selected column block (delete and enter new content)
> Left-shift Tab width
File Name and Path in vimrc
For example, edit vi tmp \ file. java under D: \ cnscnprogrames \ Vim \ vim63 \.
(That is, D: \ cnscnprogrames \ Vim \ vim63 \ tmp \ file. java)

Expand ("%") # tmp \ file. java
Expand ("%: p") # D: \ cnscnprogrames \ Vim \ vim63 \ tmp \ file. java
Expand ("%: h") # tmp
Expand ("%: t") # file. java
Expand ("%: e") # java
Expand ("%: t: r") # file
Expand ("%: r") # tmp \ file
 

· A simple Compiler (compile and execute common programs)
# Vi ~ /. Vimrc
"Define F9 to compile and execute programs
Map <F9>: call ProgCompileRun () <CR>
Func ProgCompileRun ()
"Save the current file first
: W

"Get the program suffix, such as javaphp pl py
Let ext = expand ("%: e ")
"Compile Java
If ext = "java"
If expand ("%: h ")! = ""
:! Javac % & java-classpath expand ("%: h") %: t: r
Else
:! Javac % & java %: t: r
Endif
Return
Endif
"Execute php
If ext = "php"
:! Php %
Return
Endif

"Compile and execute c/cc/cpp
If ext = "c" | ext = "cc" | ext = "cpp"
:! Gcc %-o a. out &./a. out
Return
Endif

"Compile and execute perl
If ext = "pl"
:! Perl-c %
Return
Endif

"Compile and execute python
If ext = "py"
:! Python %
Return
Endif
Endfunc

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.