Basic usage of VI Editor

Source: Internet
Author: User
Tags save file first row
Take a look at the picture first


VI Editor as a programmer artifact, it is necessary to learn. what is Vi/vim.

Almost all the configuration files in the Linux world exist in plain text, and the VI editor is available on all Linux distribution systems, so it is convenient to easily modify the various configurations of the system with simple text editing software. VI is a powerful text editor, and Vim is the advanced version of VI, not only can display text content in different colors, but also can do such as Shell script, C language program editing and other functions, can be used as a program editor. why learn to Vi/vim.

First of all Linux distribution system will default built-in VI editor, not necessarily with other text editor, very general, and secondly, many of the software's editing interface will be called by default VI; Third, VI has the ability of program editing; Finally, vi program is simple, the editing speed is quite fast. The three modes of VI and the conversion relationship between the various modes

General Mode common Operation

"H (or left ARROW key)" Cursor moves left one character
"J (or DOWN ARROW key)" Cursor moves down one character "
K (or up ARROW key)" Cursor up one character "L (or right ARROW key)" Cursor moves
right one character

"[Ctrl] + F" The screen moves down one page (equivalent to page down)
"[Ctrl] + B" screen moves up one page (equivalent to page UP key)

"[0] or [Home]" cursor moves to the front of the current line
"[$] or [End]" The cursor moves to the end of the current line the

"G" cursor moves to the last line of the file (the first character)
"NG" N is the number (same as the same), moving to the current file nth line
"GG" moves to the first line of the file, equivalent to "1G" "
n[enter" Move the cursor down n line

"/word" in the file to find the word string (look down)
"? Word" finds a string in a file with Word (look up)
"[n]" means a duplicate find action, that is, find the next
"[N]" reverse lookup next
": n1,n2s/word1/word2/g" N1, N2 is a number, find N2 string between line N1 to Word1 line and replace it with Word2
": 1, $s/word1/ Word2/g "From the first row (nth line) to the last line to find word1 registration, and replace it with Word2
": 1, $s/word1/word2/gc "function as above, except that each time the user will be confirmed

" X,x " X is the backward deletion of a character, equivalent to [delete],x to delete a character, equivalent to [Backspace]
"DD" delete the cursor where the entire line
"NDD" delete the cursor where the row of N-line

"yy" copy of the line where the cursor is located The
"Nyy" copy cursor is located on the down N line
"P,p" p to paste the data that has been copied on the next line of the cursor; p to paste the copied data on the cursor line

"U" undoes the previous action
"[Ctrl] + R" multiple undo
"." This is the decimal key, repeat the previous action
General mode switch to edit mode operation 1. Enter insert mode (6 commands)
"I" from the current cursor, insert "I" from the current cursor
"A" from the current cursor at the beginning of the next character inserted
"A" from the last character of the line where the cursor is inserted
"o" English lowercase letter O, Inserts a new row at the next line of the current cursor row and starts inserting
the "o" capital letter o, inserting a new row at the top of the line where the cursor is currently located and starting to insert
2. Enter replacement mode (2 commands)
"R" replaces only the character of the cursor once
"R" will always replace the character of the cursor until the [ESC] key is pressed
"[ESC]" Exit edit mode back to normal mode
General mode switch to command line mode
": W" Save File
": w!" If file is read-only, force save file
": Q" Leave VI
": q!" do not save Force leave VI
": Wq" save after Leave
": wq!" Force save after Leave ":
! Command "Temporarily leaves VI to command line to execute a command after the display result
": Set nu "Display line number
": Set Nonu "suppress line number
": w newfile "Save As
File Recovery Model
"[O]pen read-only" opens the file as read
-only "[E]dit anyway" opens the file normally, does not load the staging file contents
"[r]ecover" Loading the staging file contents
"[D]elete it] Open the file in normal mode and delete the staging file
"[q]uit" press Q to leave VI, no other action
"[A]bort" is similar to the Quit function
Block Selection (for general mode)
"V,v" V: Place the cursor through the local anti-white selection, V: the cursor through the line to select the
"[Ctrl] + V" Block selection, you can use a rectangular way to select the text
"Y" to copy the anti-white place to the Clipboard
"D" to remove the anti-white content
Multi-file editing
"Vim file1 file2" Open two files simultaneously
": N" Edit next file "
: N" Edit previous file "
: Files" Lists all files currently open with Vim
Multi-window function
": SP [filename]" opens a new window to display the new file, and if you enter only: SP, the two Windows display the same file
"[Ctrl] + W + j" Cursor moves to the lower window
"[Ctrl] + W + k" cursor moves to the top window
"[Ctr L] + W + q "Leave the current window
Vim configuration file
Vim configuration file is/ETC/VIMRC, but it is generally not recommended to directly modify the file, but in the user root directory to create a new hidden file:
vim ~/.vimrc
Then edit this file, commonly used configuration is as follows:
" The content after the double quotation mark is the comment
set nu "Show line number
set Hlsearch" Find the string anti-white display
set backspace=2 "can be removed at any time with the backspace bar
set Autoindent" Auto indent
set ruler "display state set Showmode" in the bottom left-hand
display mode
set Bg=dark "show different undertone, also light
syntax on" Grammar test, Color display    
dos vs. Linux break characters (file conversion)
Dos2unix [-kn] file [newfile]
unix2dos [-kn] file [NewFile]-
K: Preserve the original mtime time format of the file-
N: Keep the original old file, Output the converted content to a new file
language Encoding Conversion
 iconv--list lists the Iconv supported language encoding ICONV-F originally encoded-t new encoding filename [-O to the file]-f:from, followed by the original encoding format-t:to, followed by the new encoding format-o file: Optional parameters, Create a new file example: Convert/tmp/a.txt from BIG5 encoded format to UTF8 encoding format: iconv-f big5-t utf8/tmp/a.txt-o new.txt 

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.