One 、vi Working mode of
vi There are two working modes :
  Edit mode : The mode used to input and edit files , The user's typing will be displayed on the screen , Keys are not interpreted as command execution , It's written as text to the user's file .
  Command mode : To edit 、 Save and exit file mode .
    function vi after , First, enter command mode . Any character entered at this point is considered an instruction , The command you type will not appear on the screen .
    Switch from command mode to edit mode , You can press “Insert” Key or “i” key , Switch from edit mode to command mode , May press “Esc” key .
    The status line is the line at the bottom of the screen , By vi The editor is used to feed back the results of editing operations . Error messages or informative messages are displayed on the status line .

Two 、vi Start and exit of
1. start-up vi
    Type... Under the system diagram characters “vi”+“ Space ”+“ file name ”, Such as :vi test.txt
    vi You can automatically load the file you want to edit or open a new file .
2. sign out vi
In command mode, type the following command to exit vi:
  :q If the user only reads the contents of the file without modifying it , You can enter... In command mode “:q” sign out vi.
  :q! If the user changes the contents of the file , And decided to abandon the modification of the document , Then use “:q!” command .
  :w! Force to save one vi file , If the file already exists , Then cover .
  :wq Save file and exit vi.
  ZZ Quickly save the contents of a file , And then quit vi. Function and “:wq” identical .
  :w filename   amount to “ Save as ”.
  :n,mw filename   Will be the first n-m Line text is saved to the specified file filename in .

3、 ... and 、vi Long instructions and short instructions
  Long instruction : Start with a colon , After typing the colon , A colon prompt appears at the end of the screen , Waiting for the user to type instructions , Enter the command and press enter ,vi Will execute the command .
  Segment instruction : Similar to shortcut keys , After typing the short command ,vi Do it without any hint .
1. Input and output commands
  a Enter the text behind the cursor
  A Enter text at the end of the current line
  i Enter text in front of the cursor
  I Enter text at the beginning of the current line
  o Enter a new line after the current line
  O Enter a new line before the current line
2. Cursor movement command
  b Move to the beginning of the current word
  e Move to the end of the current word
  w Move a word back
  h Move one character forward
  l Move one character back
  j Move one line down
  k Move a line up
3. Delete operation command
  x Delete the character where the cursor is
  dw Delete the word where the cursor is
  dd Delete current row
  d$ Delete all characters from the cursor to the end of the line
  D And “d$” The same command
4. Replace the operation command
  r Replace the character of the cursor
  R Replace character sequence
  cw Replace a word
  ce And “cw” The same command
  cb Replace the character before the cursor
  c$ Replace all characters from the cursor position to the end of the line
  C And “c$” The same command
  cc Replace the current line
5. Query operation command
  /abc Backward query string “abc”
  ?abc Want to query the string before “abc”
  n Repeat the previous query
  N Repeat the previous query , But in the opposite direction
6. Copy and paste command
  yw Copy the word where the cursor is to the clipboard  
  y$ Copy the character from the cursor to the end of the line to the clipboard
  Y And “y$” The same command
  yy Copy the current line to the clipboard
  y Copy the selection to the clipboard
  p Paste the contents of the clipboard behind the cursor
  P Paste the contents of the clipboard in front of the cursor

for example :

1、 Copy

1)  Single line copy

    In command mode , Move the cursor to the line to be copied , Press “yy” replicate ;

     2) Multi line copy

     In command mode , Move the cursor to the first line to be copied , Press “nyy” Copy n That's ok ; among n by 1、2、3……

    2、 Paste

    In command mode , Move the cursor to the line to paste , Press “p” Paste

7. Other operating commands
  v Select text content
  u Cancel the last operation
  U You can restore all changes to the cursor line
  J Link two lines of text together
  :set Used to set up or browse vi The current options of the system ( Add parameters “all” View a list of all the options after )
  :X Simply encrypt the edited file

Four 、vi Advanced applications for
1. Set up vi Environmental Science
    vi Editor behavior can be defined by setting edit parameters .
    Use vi Of set Command to set .vi You must be in command mode before setting , But the change in options is temporary , It is only valid when the user is currently editing a reply , When the user exits vi Ed

When editing , Settings are discarded .
  autoindent Options
    Align the new line with the beginning of the previous line , abbreviation :ai
    This option aligns each new line you type with the beginning of the previous line . This option is very useful when programming in a programming language . The default value for this option is noai.
  ignorecase Options
    Ignore case under search command , abbreviation :ic
    vi The editor provides case sensitive search , To make vi Ignore case , type “:set ignorecase”, To return the case sensitive state , type “:set noignorecase”.
  magic Options
    In search , Special characters are allowed .
    Some symbols have special meanings when used in search . When the user starts these symbols with nomagic when , These symbols no longer have special meanings .
  number Options
    According to the line Numbers , abbreviation :nu
    vi Generally, the editor does not display the line number of each line . You can change number Option to display the line number .
  report Options
    Tells the user the line number of the last command action line .
    vi The editor doesn't give any feedback on the user's editing work , If you want to see your own feedback on the screen , Users can edit report Options to achieve .
  scroll Options
    Set to use in command mode “Ctrl+d” The number of lines that command scrolling .
  shiftwidth Options
    Sets the number of indented spaces , Generally speaking, it is related to autoindent Use together , abbreviation :sw
  showmode Options
    In the right corner of the screen vi Editor Mode , abbreviation :smd
  terse Options
    Shorten error messages .
  wrapmargin Options
    Set the right border to a certain number of characters , abbreviation :wm
    The user's terminal screen is usually 80 Column , When typing to the end of a line , The screen starts a new line , That's the line wrap around . When the user presses enter , The screen also starts a new line .

therefore , The length of a line on the screen can be 1-80 Any length between characters . however ,vi The editor only works when the user presses enter , To generate a new line in the user file . If the user is

Press enter to type 120 Characters , The text you type appears to be 2 That's ok , But actually in the file , this 120 Two characters only in 1 In line . If you want to limit the line length , One is arriving at the screen

Press enter at the end of the line to wrap , The other is to set wrapmargin Options enable vi The editor automatically inserts a carriage return .

2. Abbreviation operators
    Abbreviation operators “:ab” The command causes the user to specify abbreviations for any string , This function can help the user improve the input speed . Users can choose an easy text for the text they often enter

Abbreviation of input .
    for example : type “:ab uox Unix Operating System” command , When vi In text input mode , User type “uox” And then when you type in a space ,vi Automatically type

“uox” Turn into “Unix Operating System”.
    Abridge “:unab” Command can cancel an abbreviation , for example : type “:unab uox” Command is enough .
    Want to list all the abbreviations that have been set , type “:ab” Command is enough .
3. Macro operators
    Macro operators “:map” Commands enable the user to assign a series of keys to a key . Give the user a shortcut key in command mode .
    for example : type “:map q dd” command , After that, in instruction mode , Every time a user types q when ,vi The editor automatically executes “dd” command .
    Cancel macro operation “:unmap” command .
    Users can also use it map The command specifies the function key , for example : type “:map #n dd” command , among “#n” Representative to n Function key for function key number .
4.“.exrc” file
    The user is in vi All options set in the editor are temporary , When the user exits , They all fail . To make these settings permanent , You can save the settings of the options to a file

“.exrc” in .
    When the user opens vi Editor time , It automatically looks at... In the user's current working directory “.exrc” file , And set the editing environment according to the content in the file . If vi Not in the current eyes

We found that “.exrc” file , Will find the user's home directory , And based on where it was found “.exrc” File settings editing environment . If vi One “.exrc” The file was not found , be

Use default values for options .
    “.exrc” The file belongs to the startup file .
for example : Build a “.exrc” file .
Type... In the terminal “vi .exrc”, enter .
stay vi Type... In the editor :
  set autoindent
  set shiftwidth=2
  set nomagic
  set number
  set scroll=20
  set showmode
Save to exit
5. function Shell command
    Users can go to vi Run from the command line of Linux Shell command , So that users can temporarily put aside vi To run the Shell command .
    vi The editor uses “!” To inform vi Behind it is a Shell command . for example : type “:!date” command ,vi The editor will clear the screen , Output current system time , Then press any key to return to vi Editor , And can continue to edit .
    If the user system will Shell The result of command execution is added to the file text , have access to “:r!” And corresponding Shell Command to write the result of the command to the user file text .

 

Cursor control command
command Cursor movement
h or ^h One character to the left
j or ^j or ^n Move down one line
k or ^p Move up one line
l Or space Move one character to the right
G Move to the last line of the file
nG Move to page n That's ok
w Move to the beginning of the next word
W Move to the beginning of the next word , Ignore punctuation
b Move to the beginning of the previous word
B Move to the beginning of the previous word , Ignore punctuation
L Move to the last line of the screen
M Move 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 Move to the end of the next word , Ignore 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 or | Move to the first column of the current row
n| Move to... On the current line n Column
^ Move to the first non empty character on the current line
$ Move to the last character of the current line
+ or return Move to the first character on the next line
- Move to the first non empty character on the previous line
stay vi Add text
command Insertions
a Insert text after cursor
A Insert text in the current line
i Inserts text in front of 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
:r file Read in file file Content , And insert after the current line
:nr file Read in file file Content , And put it in the second n After line
escape Go back to command mode
^v char Ignore... When inserting char The designated meaning of , This is to insert special characters
stay vi Delete text from
command Delete operation
x Delete the character at the cursor , Can be in x Plus the number of characters to delete
nx Delete from the current cursor n Characters
X Delete the character before the cursor , Can be in X Plus the number of characters to delete
nX Delete forward from the current cursor n Characters
dw Delete to the beginning of the next word
ndw Delete from the current cursor n A word
dG Delete row , Until the end of the document
dd Delete entire line
ndd Delete from the current line
db Delete the word in front of the cursor
ndb Delete from current line n word
:n,md From m Line starts to move forward n That's ok
d or d$ Delete from cursor to end of line
dcursor_command Delete to cursor command , Such as dG Delete from the current line to the end of the file
^h or backspace Insertion time , Delete the preceding characters
^w Insertion time , Delete the preceding words
modify vi Text
The number before each command indicates the number of times the command has been repeated
command Replace operation
rchar use char Replace the current character
R text escape use text Replace the current character until Esc key
stext escape use text Instead of the current character
S or cctext escape use text Instead of the whole line
cwtext escape Change the current word to text
Ctext escape Change the rest of the current line to text
cG escape Modify to the end of the file
ccursor_cmd text escape Change from the current position to the cursor command position to text
stay vi Find and replace
command Find and replace operations
/text Look forward in the file text
?text Look back in the file text
n Repeat the search in the same direction
N Repeat the search in the opposite direction
ftext Look forward on the current line text
Ftext Look backward in the current line text
ttext Look forward on the current line text, And position the cursor at text First character of
Ttext Look backward in the current line text, And position the cursor at text First character of
:set ic Ignore case when searching
:set noic The lookup is case sensitive
:s/oldtext/newtext use newtext Replace oldtext
:m,ns/oldtext/newtext stay m Yes n, use newtext Replace oldtext
& Repeat the last :s command
:g/text1/s/text2/text3 Find contains text1 The line of , use text3 Replace text2
:g/text/command In all cases including text The line running command The command represented
:v/text/command In all not including text The line running command The command represented
stay vi Copy text in
command Copy operation
yy Put the contents of the current line into a temporary buffer
nyy take n The contents of the line are put into a temporary buffer
p Put the text in the temporary buffer behind the cursor
P Put the text in the temporary buffer in front of the cursor
"(a-z)nyy Copy n The row is placed in a named buffer named in parentheses , Omit n Indicates the current line
"(a-z)ndd Delete n The row is placed in a named buffer named in parentheses , Omit n Indicates the current line
"(a-z)p Place the contents of a named buffer named parenthesis after the current line
"(a-z)P Place the contents of a named buffer named parenthesis before the current line
stay vi Undo and repeat in
command Undo operation
u Undo the last modification
U Undo all changes to the current line
. Repeat the last modification
, Repeat the previous... In the opposite direction f、F、t or T Search for commands
; Repeat the previous f、F、t or T Search for commands
"np Get back the last n Second deletion ( There are a certain number of deletions in the buffer , It's usually 9)
n Repeat the previous / or ? Search for commands
N Repeat the previous... In the opposite direction / or ? command
Save text and exit vi
command Preservation and / Or exit the operation
:w Save the file without exiting vi
:w file Save the changes in file In but not out vi
:wq or ZZ or :x Save file and exit vi
:q! Don't save files , sign out vi
:e! Abandon all changes , Edit from the last time you saved the file
vi Options in
Options effect
:set all Print all options
:set nooption close option Options
:set nu Print the line number before each line
:set showmode Show input mode or replace mode
:set noic Ignore case when searching
:set list Show tabs (^I) And end of line symbols
:set ts=8 Set... For text input tab stops
:set window=n Set the text window to display n That's ok
vi The state of
Options effect
:.= Print the line number of the current line
:= Print the number of lines in the file
^g Show filename 、 The current line number 、 Total number of files and percentage of file locations
:l Using letters "l" To display many special characters , Such as tab and newline
Position paragraphs and place markers in text
Options effect
{ 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 , If used mz To mark z
'(a-z) Move the cursor to the specified marker , If used 'z Move to z
stay vi Link lines in
Options effect
J Connect the next line to the end of the current line
nJ Connect the back n That's ok
Cursor placement and screen adjustment
Options effect
H Move the cursor to the top line of the screen
nH Move the cursor to... Under the top line of the screen n That's ok
M Move the cursor to the middle of the screen
L Move the cursor to the bottom line of the screen
nL Move the cursor to... On the bottom line of the screen n That's ok
^e(ctrl+e) Scroll a line on the screen
^y Scroll the screen down one line
^u Scroll the screen in half
^d Scroll the screen down half a page
^b Scroll a page on the screen
^f Scroll the screen down one page
^l Redraw screen
z-return Set the current line to the top line of the screen
nz-return Put the second... Under the current line n Line is set to the top line of the screen
z. Put the current line in the center of the screen
nz. Put the number on the current line n Line to the center of the screen
z- Set the current line to the bottom of the screen
nz- Put the number on the current line n Line to the bottom of the screen
vi Medium shell Escape command
Options effect
:!command perform shell Of command command , Such as :!ls
:!! Perform the previous shell command
:r!command Read command Command input and insert , Such as :r!ls Will execute first ls, And then read in the content
:w!command Use the currently edited file as command Standard input and execution of commands command command , Such as :w!grep all
:cd directory Change the current working directory to directory The directory represented
:sh Will start a sub shell, Use ^d(ctrl+d) return vi
:so file stay shell Program file Read and execute commands in
vi Macro and abbreviation in
( Avoid using control keys and symbols , Don't use characters K、V、g、q、v、*、= And function keys )
Options effect
:map key command_seq Define a key to run command_seq, Such as :map e ea, Any time is fine e Move to the end of a word to append text
:map Show all defined macros on the status line
:umap key Delete the macro for the key
:ab string1 string2 Define an abbreviation , So that when you insert string1 when , use string2 Replace string1. When you want to insert text , type string1 Then press Esc key , The system is plugged in string2
:ab Show all abbreviations
:una string Cancel string Abbreviation
stay vi Indent text in
Options effect
^i(ctrl+i) or tab When inserting text , Insert the width of the movement , The moving width is pre-defined
:set ai Turn on auto indent
:set sw=n Set the move width to n Characters
n> send n All lines move one width to the right , for example 3>> Move the next three lines one width to the right
1. Edit mode : Insert / Replace ( Press INSERT Key switch )
2.[ESC] Command mode
3. The input mode Will enter... In command mode a
newly added (a,A)
a: Add data after the cursor position , The data behind the cursor moves backward with the new data .
A: Add data from the bottom of the cursor column .
Insert (i,I)
i: Insert data from the front of the cursor position , The data behind the cursor moves backward with the new data .
I: Insert data before the first non blank character in the cursor column .
Start (o,O)
o: Add a new column under the cursor column and enter the input mode .
O: Add a new column above the cursor column and enter input mode .
4. Save / sign out
[ESC] :wq // Save and exit
:w! // Save to continue
:q // sign out ( Source file not edited available )
:q! // Forced exit
5. Deletion and modification
[ESC] x // Delete cursor character
dd // Delete cursor column .
r // Change the character of the cursor ,r The character to be corrected after .
R // Enter the replacement state .
s // Delete the character where the cursor is , And enter the input mode .
S // Delete the column where the cursor is located , And enter the input mode .
6. recovery
[ESC] u // You can restore the deleted text .
U // You can restore all changes in the column where the cursor is located .

VI More articles on how to use the editor

  1. 【Linux Basics 】VI How to use the editor

    vi The editor is all Unix And Linux Standard Editor under the system . Yes Unix And Linux Any version of the system ,vi The editor is exactly the same , It is Linux The most basic text editor in . Chapter one vi Three models of Chapter two vi Text editor ...

  2. linux Systematic -vi Editor

    stay linux The system is in use , Have a good command of vi Editor , Can improve linux The work efficiency . that vi What are the ways to use the editor ? vi The editor can be used in most linux Used in distribution . Vi The role of the editor : Create or modify files : maintain li ...

  3. solve vi The editor can't use two ways to solve the problem of arrow keys and backspace keys

    Method 1. Use vi On command , Can't edit file properly , There are always a lot of letters when using the arrow keys ? stay Ubuntu in , Get into vi Edit mode of command , It is found that the cursor cannot be moved by pressing the direction key , It will output ABCD, And the backspace key can't delete characters properly . This is because ...

  4. Kali Linux Of vi Editor /vim Editor usage

    Reprint statement : This is a reprinted article Original address :https://www.52host.cn/blog/kali-linux-vi-editor/ Kali Linux Systematic vi Editor /vim The use of the editor and Cent ...

  5. vi Editor usage ( The most detailed )

    vi The editor is all Unix And Linux Standard Editor under the system , It's as powerful as any new text editor , Here is just a brief introduction to its usage and a few instructions . Due to Unix And Linux Any version of the system ,vi The editor is perfect ...

  6. Vi and Vim How to use the editor in detail

    Study linux It's a must-have skill , Skillfully use vi/vim Editor that's the most important . But it's a headache to watch a lot of operations , Here are some common commands . Working mode vi The editing interface has three different working modes , They are command mode . The input mode . ...

  7. 10 Years ago, the article _ vi Editor find and replace methods

    vi Editor find and replace methods 1. lookup :/ Look down on the current line :? Look up the current line and continue to look in the same direction n In the opposite direction N2. Replacement format : range s/source/target/g explain :range Indicates the scope to search  & ...

  8. Ubuntu System vi Editor up and down key change ABCD Solutions for ( turn )

    First, uninstall the old version of vi Editor : $sudo apt-get remove vim-common Then install the new version vi that will do : $sudo apt-get install vim Ubuntu There are several versions of it v ...

  9. linux in vi Use of editor

    vi The editor is all Unix And Linux Standard Editor under the system , It's as powerful as any new text Editor , Here is just a brief introduction to its usage and a few instructions . Due to Unix And Linux The function of the system is What version ,vi The editor is finished ...

Random recommendation

  1. About use rem Company ,calc() Adaptive layout

    About css Units in We all know that css Units in , It generally includes px,%,em Equal unit , in addition css3 Add a new unit rem. among px,% And other units are often used in the traditional layout , We are also familiar with , however px The unit is ...

  2. Android5.0 New characteristics —— New animation (animation)

    New animation stay Material Design In design , For users and app Interactive feedback on their actions and behaviors provides visual coherence .Material The themes are controls and Activity The transition provides some default animations , stay android ...

  3. Use sqoop take mysql Data import to hadoop

    hadoop I won't talk about it here . Sqoop Installation is also simple . complete sqoop After installation , You can test if you can connect to mysql( Be careful :mysql Of jar The bag should be placed in SQOOP_HOME/lib Next ): ...

  4. VC Various methods of getting form handle in

    AfxGetMainWnd AfxGetMainWnd Get your own form handle HWND hWnd = AfxGetMainWnd()->m_hWnd; GetTopWindow The functionality : This function checks with ...

  5. Some regularities are in js Usage method

    Input box directly regular judgment <input type="password" name="pwd" placeholder=" The password can only be in numbers \ english \@\.& ...

  6. Windows Phone Development (21): Make a simple drawing board

    original text :Windows Phone Development (21): Make a simple drawing board In fact, what we are going to talk about today is a control --InkPresenter, This control is not very powerful , There's no way to talk to WPF Medium InkCanvas comparison , It is estimated to be true ...

  7. XBMC Source code analysis 6: Video player (dvdplayer)- The file header ( With ffmpeg For example )

    XBMC Analysis series : XBMC Source code analysis 1: Overall structure and compilation method XBMC Source code analysis 2:Addons( The skin Skin) XBMC Source code analysis 3: The core part of the (core)- review XBMC Source code analysis 4: ...

  8. Unity UGUI

    Super detailed basic course portal :( Ongoing update ) Unity UGUI And Canvas&EventSystem:http://blog.csdn.net/qq992817263/article/detai ...

  9. scipy.stats

    scipy.stats Scipy Of stats The module contains a variety of random variables with probability distribution , Random variables are divided into continuous and discrete . All continuous random variables are rv_continuous Object of a derived class of , And all discrete random variables are ...

  10. Asterisk 11 chan_sip.c: Failed to authenticate device Out of sight IP The problem of

    Asterisk 11 chan_sip.c: Failed to authenticate device Out of sight IP The problem of   Not verified Original address  http://www.coochey.net/? ...