Linux instruction Detailed vi interactive text editor

Source: Internet
Author: User

VI is the abbreviation for ' Visual interface ' and is an interactive text editor with a pure character interface as standard under Linux. You can perform many text operations, such as output, delete, find, replace, block operations, and so on, and you can customize them to suit your needs. All of its operations need to be done through the keyboard instructions, if you can skillfully apply, it will be very convenient

--------------------------------------------------------------------
Operating mode:
VI has three modes of operation, Command Mode,insert mode,last line mode
Command Mode: Command mode. (default mode)
When entering the VI editor, this mode is the default
In this mode you can control the movement of the screen cursor, the copy and paste deletion of characters or lines
Commands entered in this mode are not visible
Insert Mode: Insert mode.
Text can only be entered in this mode
Generally add delete modified text content in this mode
Last line mode: Last line mode. (Special command mode)
Operations in this mode require a carriage return to execute
In this mode you can save the file Exit VI
Environment variables can be set in this mode
You can find and replace in this mode
Automatically returns to command mode when this mode is completed

VI can be simplified into two modes, Command and insert mode. Last line can be considered a special command mode.

--------------------------------------------------------------------
Enter VI:
1:# VI filename: Opens (or creates a new) file, places the cursor on the first line
2:# vi +n filename: Opens the file and places the cursor on nth line
3:# vi + filename: Opens the file and places the cursor on the last line
4:# vi +/word1 filename: Opens the file and places the cursor in the first word1
5:# vi-r filename: Restore filename (System crash if last edit with VI)
6:# vi filename1 filename2 filename3: Open multiple files, edit them in turn


Ps:
1: If filename is present, open filename. New filename If it does not exist (created on save)
2: Show a wave symbol before each line indicates that the behavior is blank line
3: If you do not save the file changes, the original file will not change the contents
4: Multiple users can edit a file with VI at the same time, the last saved version will be saved

--------------------------------------------------------------------
Three modes of switching:
Command mode to insert mode:' i ' a ' ' C ' o ' r ' s 'such as
Insert mode to command mode:' ESC '

Command mode to last line mode:' : '/'? '
Last-line mode to command mode:' ESC '

insert mode to last line mode to switch to command mode first

--------------------------------------------------------------------
Help:
F1
: Help

View version:
# VI--version

--------------------------------------------------------------------
Screen page:
CTRL + F: Screen to ' front ' move one page (Common) Ctrl + B: Screen to ' after ' Move one page (Common) Ctrl + D: Screen to ' Front ' move half page Ctrl + U: Screen to ' rear ' move half page Ctrl + E: Screen roll forward line Ctrl + Y: Screen item to roll a line

--------------------------------------------------------------------
Cursor Movement:
Ctrl+g: Shows the number of rows and columns at the cursor location 0:  cursor moves to the beginning of the current line (number 0) ^: Move  to the first non-whitespace character of the line $:  cursor moves to the end of the current row (common) n$: The cursor moves to the end of the nth line (:   to the beginning of the sentence):   to the end of the sentence {: to the   beginning of the paragraph}: to the end of the   paragraph g: the cursor moves to   the last line of the article (frequently used) NG: The  cursor moves to the beginning of the first n GG:  move the start of the article H or left ARROW key: The cursor moves one character to the left L or Right ARROW key: The cursor moves right one character K or up ARROW key: The cursor moves up one character J or down ARROW key: The cursor moves down one character ctrl+p: Move the cursor up a line (with K) CTRL + N: Move the cursor down a line (with J) Enter:    Move the cursor down a line n< ENTER>: The cursor moves down n line space: The  cursor moves right one character n<space>: The   cursor moves n characters to the right Backspace: The  cursor moves left one character n<backspace The  cursor moves left n characters B: The cursor moves left  to the beginning of the previous word B: The  cursor moves left to the beginning of the last space-delimited word w: the  cursor moves right to the beginning of the next word w: the  cursor moves right to the beginning of the next space-delimited word e: The  cursor moves right to the end of the next word e: The  cursor moves right to the end of the next space-delimited word h: The cursor moves to the  top row of the current screen NH: Move the cursor to the nth row of the screen L:  when the cursor moves to the last line of the current screen        with more than one screen, When the cursor moves to the bottom of the screen when there is less than one screen, the cursor moves to the last line of the file nl: The cursor moves to the penultimate line of the screen m: the cursor moves to the middle row of the screen when the cursor moves to        the middle row        of the screen The cursor moves to the middle line of the text when the content is less than one screen DH: Removes all content from the line of the cursor to the first line of the screen DM: Delete all content from the line on the cursor to the middle row of the screen (file) DL: Delete everything from the line of the cursor to the last line of the screen +:  The cursor moves to the first non-whitespace character of the next line n+: The cursor moves down n lines-: The cursor  is moved to the first non-whitespace character on the previous line N: the cursor moves up n line Z.: The current line as the middle row of the screen V:  Select text content

--------------------------------------------------------------------
Insert text
A: Insert character after current cursor A: Inserts the character at the end of the current line I: Inserts a character before the current cursor I: Inserts a character before the current line (the first non-whitespace character match either) O: Inserts a blank line after the current row, starting at the beginning of the next line where the cursor is located o: Insert a blank line before the current Enter characters from the beginning of the line at the start of the cursor: replace the character with the current cursor (end after substitution) R: Replace the current character with the following character until you press ESC to end the substitution J: appends the next line of the cursor to the line end of the bank: R filename   Adds the contents of the filename to the next line of the current cursor: R!command adds the output of the command    shell commands to the current line XP: The character at the swap cursor position and the character to the right of it ~:  Change the case of the character that the cursor is in

--------------------------------------------------------------------
Delete:
DD:  Delete the row of the cursor ndd: delete n rows from the line where the cursor is located D: Delete the contents of the cursor   following the line cursor x: Delete   The character of the cursor x:   Delete the character in front of the cursor NX:  remove the n characters from the cursor position NX:  n characters before the cursor is deleted DW:  Delete the word that the cursor is in (actually starting at the cursor position to the end of the word) de:  Remove the cursor to all characters (without spaces) in the end of the word d0:  Delete all content before the cursor line cursor d^:  deletes all characters from the current character to the beginning of the line (same as D0) d$: Deletes all the  contents of the cursor to the end of the row d1g: Delete all data from the row to the first line of the cursor DG:  Delete all data from the line to the last line of the cursor DFA: Delete the contents from the cursor to the first a character (including a): Nd: Delete the nth row: n1,n2d: Delete the content between line N1 and line N2 s: Delete the character at the cursor location, then append any character s: delete the current line, and then append any character ns: delete n rows, Then append any character cw: Delete the character of the cursor to the end of the word CE: delete the character (as CW) at the tail of the word C $: Delete the cursor to the current line ending with the character c. Delete the cursor to the end of the current line of characters  (as with C $) c^: Remove the character before the cursor to the beginning of the line (excluding the character on the cursor) cc: Delete the cursor row (unlike DD, CC enters the Insert State) CB: Delete the character that precedes the cursor of the word where the cursor is located

--------------------------------------------------------------------
Undo and Redo
U: Undo Last Action (can be performed multiple times) U: Undoes all operations on the current line.: Repeat last Action Ctrl + R: Undo undone action

--------------------------------------------------------------------
Find:
/word: Find word backwards from the beginning of the cursor (or start from the beginning if you encounter the end of the file)? Word: From the beginning of the cursor to find word (if you encounter a header, then start from the tail) N: Performs the last-defined lookup n in the same direction: performs the last-defined lookup in the opposite direction/: Find the last defined lookup: Set IC: Lookup is case-insensitive: Set Noic: Case-sensitive when finding

--------------------------------------------------------------------
Replace:
: s/word1/word2/c replace     word1 with Word2 and let the user confirm before replacing: s/word1/word2/g     change All word1 in the current line to word2:n1,n2s/word1/word2/g Convert   all word1 from line N1 to line N2 to Word2:1, $s/word1/word2/g     replace     all word1 in the first row to the last row word2:g/word1/s//word2/g Replace all the word1 in the file with word2:%s/word1/word2/g and replace    all word1 in the file with word2:n1,n2s/^/word1/g N1    line to N2            Delete all lines at the end of the line (Super Common):%S#/DODO/1#/DODO/2     Replace the isolator

no G, replace only the first occurrence of the search string
G at the end of the command, replace each occurrence of the row that contains the search string
G at the beginning of the command, replace all the lines in the text

--------------------------------------------------------------------
Save exit:
: w:  Save the current file: Q:  exit directly (without modification to the file): Wq: Save and exit: x: Save and exit:  q!: Do not save the direct exit (do not want to save the file modification): w!: Force Save the file (for read-only files) Shift+zz: Save and exit (in fact, ZZ): w filename        saves the modified file to filename (error if filename exists): n1,n2w filename Saves the contents of line   N1 to line n2 in the filename file

--------------------------------------------------------------------
Hide Display Line numbers:
: Set nu    Displays line number: Set Nonu  suppress line number ctrl+g:  lists the line number of the line where the cursor is located

--------------------------------------------------------------------
Copy and paste:
YY: Copy cursor Line (common) Nyy: Copy starts from the line of the cursor down n rows y1g: Copy all rows from the cursor line to the first line YG: Copy all rows from the line to the last line yw: Copy word from the cursor position to the end of the word y$: Copy the content from the beginning of the cursor to the end of the line y^: Copy the content from the beginning of the cursor to the beginning of the start y0: Copy the contents from the beginning of the cursor to the beginning of the row (same as y^) p: Paste the next line in the current line (lowercase) p: Paste on the previous line (uppercase)

Note: All copies of ' Y ' must be combined with ' P ' to complete the copy and paste

: N1,N2 CO N3   copies the contents of N1~N2 to line N3. : N1,N2 Co $   copies the contents of the N1~N2 line to the last row. : N1,n2 mo $   moves the contents of the N1~N2 line to the last row.

Buffer use (be aware that the buffer name does not conflict with the command name)
A buffer function in VI that allows data to be present in buffer
CYY: Copy the current line into buffer C CP:  paste content placed in buffer a


--------------------------------------------------------------------
Modify file format
: Set Fileformat=unix
: Set Ff=unix
Modify the file to UNIX format (such as win below the text file under Linux will appear ^m, resulting in garbled)


--------------------------------------------------------------------
Other directives:
:! Command: Execute the shell Directive command!! : Re-execute the last (:! Command subcommand) <<: Moves the current line to the left by one tab>>: Moves the current line to the right by one tabn>>: Moves the n line from the current line to the right one tab:n1,n2>: Move the first N1 line to the N2 row to the right one TAB:N1,N2>>>: Move line N1 to N2 to the right by three tab


--------------------------------------------------------------------

environment variable Settings
1, through the last line mode to set
For example: Set autoindent
The setting through the last line mode is temporary, and after exiting VI there is no

2. To make these settings permanent, you can save the option settings to the following files, which are valid for each VI:
/etc/virc or ~/.virc
~/.EXRC or the. EXRC in the current working directory.

The common options are as follows, as long as you add them to a file
Set tabstop=4        #设置tab跳过的空格数为4 (default 8) Set number           #设置显示正文行号set ignorecase       #在搜索命令中忽略大小写 (abbreviation: IC) set Noignorecase     #在搜索命令中区别大小写set scroll=20        #设定指令模式下使用 ' ctrl+d ' command tumbling the number of rows set Showmode         #设定在屏幕右角显示vi编辑器模式 ( Abbreviation: SMD) set ruler            #设定在屏幕底部显示光标所在行, column position set terse            #设定只显示简短的警告信息set nomagic          #设定在搜索模式中, allows the use of special characters without ' \ ' in front ( Symbols no longer have special meanings) syntax on            #设定语法加亮显示set autoindent       #设定每一行都和前一行有相同的缩进量 (abbreviation: AI) set smartindent      # Set each row to have the same indent as the previous line, but encountering ' {' and '} ' and ' # ' without indentation (abbreviation: SI) set cindent          #采用标准的C语言的缩进形式 (C,c++,java) (abbreviation: CI) set shiftwidth= 2     #设定缩进空格数为2 (abbreviation: SW) Set report           #告知用户最后一个命令作用行的行号set wrapmargin=80    #设置行长度为80 (abbreviation: WM)


Linux instruction Detailed vi interactive text editor

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.