VI and VIM

Source: Internet
Author: User
Tags linux text editor

Vi:linux text editor, all Linux distribution will be taken by default.
Vim:vi's enhanced version, it can automatically call the program's syntax judgment based on the file's suffix or the information at the beginning of the file to determine the contents of the file, and then display the program code and general information in color.
VI is divided into three modes: instruction mode, edit mode, instruction column mode


instruction mode: The default entry mode after opening a document with the VI + document name
the commands you can use are as follows:
h: The cursor moves one character to the left
J: Move the cursor down one character
k: The cursor moves up one character
L:(lowercase l) The cursor moves one character to the right
30j: Move the cursor down 30 lines
ctrl+f: The screen moves down one page, equivalent to the PAGE DOWN button
ctrl+b: The screen moves up one page, equivalent to the PAGE Up button
Home button: The cursor moves to the front of this line of characters prompt
End: The cursor moves to the last word in this line prompt
N<enter>:n is a number, the cursor moves down n rows.
G: Move to the last line of this file (the first column of the last row)
NG: Moves to the nth row of the file
GG: Move to the first line of this file (first column of first row)
/word: Look under the cursor for a string called Word
? Word: Look for a string named word above the cursor
N: This refers to the English letter N, which means to continue the next in/word and Word searchstring Match
N: Just the opposite of small N, search up
x: In one row, X is the backward deletion of a character, which is equivalent to the DEL key
x: In one row, X is the forward deletion of a character, equivalent to the BACKSPACE key
Nx:n is a number that continuously deletes n characters backwards
DD: Delete the line where the cursor is located
NDD: Deletes successive n rows down the line where the cursor is located
d1g: Delete all data from the row to the first row of the cursor
DG: Delete all data from the row to the last row of the cursor
d$: Delete all characters at the end of the line at the cursor location
D0: Delete all characters at the beginning of the cursor
yy: Copy cursor line
nyy: Copy the row of contiguous n rows down the cursor line
y1g: Copy all data from the row to the first row of the cursor
YG: Copy all data from the line of the cursor to the last line of the file
P: Pastes copied data to the bottom of the line where the cursor is located
P: Pastes the copied data to the top of the line where the cursor is located
J: Merge the cursor rows and the next line into one line
u: Undo Previous Action
ctrl+r: Redo the previous action

: n1,n2s/word1/word2/g: N1 and N2 are numbers. Look for the string word1 between N1 and N2, and replace the string with a string word2!
: 1, $s/word1/word2/g: to find the Word1 string from the first line to the last line and replace the string with word2!
: 1, $s/word1/word2/gc: the word1 string needs to be found from the first line to the last line, and the prompt is displayed to the user before replacing it (confirm) if it needs to be replaced!

Available commands in edit mode:

I: Insert from the cursor location
I: starting with the first non-null character of the line where the cursor is located
A: Insert
a: Inserts a
o: Insert a new line on the next line of the cursor's row
O: Insert a new line at the top of the line where the cursor is located

r: consecutive substitution of the cursor character, Until you press the ESC key
esc: Exit edit mode, go back to command line mode


Commands available in instruction-column mode:
: w: Writes edited data to the hard disk file
: w!: Force write to file, in the end can write with your own permissions on the file about
: Q: Leave VI
: q!: Forced leave, such as modified file, will not take effect
: Wq: Save exit
: wq!: Force Save exit
ZZ: If the file is not changed, it is not stored away, if the file changes, the storage left
: w FileName: Save the edited data as another file
: R filename: Reads the contents of the filename file to the back of the line where the cursor is located
: n1,n2 w FileName: Stores data between n1,n2 rows in the filename file
:! Command: Temporarily leave VI to command column mode to display results of command commands
: Set Nu: Display line number
: Set Nonu: Cancel line number

Vim Editor's File Recovery:
When we use VIM to edit, Vim creates a. filename.swap file in the directory of the edited file,
Your action on the filename file will be recorded in the. filename.swap file if your system is
Cause the file you edited is not yet stored and can be recovered from the. filename.swap file.


Additional features of vim:
(1)
Enter the alias command at the command line if alias Vi=vim means that when you execute the VI command, the Vim,alias is the alias.
when you use VIM to open a file (vim + filename), the bottom right corner of the document will have the cursor current column of the indicator, there will be the current screen as a percentage of the entire file,
there will be color display, there will be a general textual description of the document properties, such as: "/etc/services" [readonly] 11176L, 670293C
(2)
block selection: After opening a file with Vim
pressing the V: Character selection will place the cursor over the white selection.
Press V: Line selection To select the line that the cursor passes through.
Press CTRL + V: Rectangular selection,
Press Y: Copy the anti-white place.
Press D: Remove the anti-white area.
Press P: Paste the content you just copied at the cursor.
(3) Multi-file editing: Use Vim to open multiple files simultaneously, such as: Vim 1.c 2.c
in the first file, press: Files (the colon means to enter the instruction column mode), you can view the files that are currently open with vim.
Press: N to switch to the next file.
Press: N to switch to the previous file.
(4) Multi-window function:
: SP [filename]: Opens a new window, if it has a filename, indicates that a new file is opened in a new window, otherwise two windows are the same file content.
Ctrl + W +J or DOWN arrow: The key method is to press CTRL, then press the W to release all the keys, and then press the J or down ARROW, the cursor can be moved to the lower window.
Ctrl + W + K or up ARROW: ditto, but move the cursor to the top window.
Ctrl + W + Q: Ends the new window that opens.
(5) Pick word complete function:
Ctrl +x CTRL +N: Use the content text of the file you are currently editing as a keyword to complement it.
Ctrl +x CTRL +O: Add the extension as a syntax supplement, with VIM built-in keywords.
(6) Vim environment setting and logging: There will be two files in the user's home directory. VIMRC (default does not exist, you need to create it manually) and. Viminfo (record the various actions you used to use VIM)

in the instruction column mode of the file opened with Vim, enter:
: Set All: View all of Vim's SetPoint values
: Set Nu: Display line number
: Set Nonu: Cancel line number
: Set Hlsearch: High Brightness search
: Set Nohlsearch: Cancel High Brightness search
: Set autoindent: Auto Indent (the so-called indentation is when you press ENTER to start a new line, the cursor no longer starts, but aligns with the first non-empty character on the previous line.) )
: Set noautoindent: un-indent
: Set Backup: Whether to automatically store backup files, usually nobackup. If you set up backup, then when you change any of the files, the source file is stored as a file named filename~.
Set Ruler: Display the lower right corner of the status bar
: Set Showmode: Displays the--insert--and the like in the lower left corner of the status bar
: Set: Displays the setting parameters that are different from the system default values.
: Syntax on: Whether different colors are displayed according to program-related syntax.
: Syntax off: Cancels the associated syntax color display.
: Set Bg=dark: Show different color tones
: Set Bg=light: Preset is light.

VI and VIM

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.