Vim Editor Summary
Directory
A Vi/vim use ... 2
1. Vi/vim Working mode ... 2
1.1 Open File ... 2
2 Normal mode ... 2
2.1 Read the file ... 2
3 edit Mode ... 3
4 End-of-line mode ... 4
5 Find and Replace commands in vim ... 4
6 Vim environment Settings ... 5
7 Advanced Features ... 6
8 Open multiple Files ... 7
9 File Recovery Function ... 7
Ten vim Multi-line comments ... 8
One vi/vim use
- Vi/vim working mode
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/74/11/wKioL1YSoEmx63S2AACWTZtOx20067.jpg "title=" Vim01.png "alt=" Wkiol1ysoemx63s2aacwtztox20067.jpg "/>
Vim's three modes of operation Normal mode edit mode last line mode
1.1 Open File
Command |
Function |
Vim +n filename |
Opens the file and places the cursor at the beginning of the nth line |
Vim + filename |
Open the file and place the cursor at the beginning of the last line |
Vim +/pattem filename |
Open the file and place the cursor in the first row that matches the Patten |
Vim–r filename |
A system crash occurred last time you used the VIM editor to open its swap file |
Vim filename |
Opens the file with the default cursor placed in the first letter of the file |
2 Normal Mode 2.1 read file
After opening the file, Vim enters the mode called normal mode, in which you can use the top, bottom, left, RIGHT ARROW keys or other keys on the keyboard to move the cursor.
To move the cursor parameters:
Operation |
Function |
h or LEFT ARROW key |
The cursor moves one character to the left |
J or DOWN ARROW key |
Move the cursor down one character |
K or UP ARROW key |
Move the cursor up one character |
L or RIGHT ARROW key |
The cursor moves one character to the right |
Crtl+f |
The screen moves down one page |
Ctrl+b |
The screen moves up one page |
Ctrl+d |
The screen moves down half a page |
Ctrl+u |
The screen moves up half a page |
+ |
Cursor moves the next column of non-whitespace |
- |
Cursor moves the previous column of non-whitespace |
N<space> |
The cursor moves the n characters of this line to the right |
0 or function key home |
Move to the front character of this line |
$ or Function key end |
Move to the last character of this line |
H |
The cursor moves to the first subtitle of the line at the top of the screen |
M |
The cursor moves to the first subtitle of the line in the middle of the screen |
L |
The cursor moves to the first subtitle of the line at the bottom of the screen |
G |
The cursor moves to the last line of the document |
NG |
The cursor moves to the nth line of this document |
Gg |
The cursor moves to the first line of the document, which is equivalent to 1G |
N<enter> |
Move the cursor down n rows |
3 edit mode
Go to edit mode
Operation |
Function |
I,i |
Enter edit mode, I means edit from current line, I means start editing from the first non-whitespace character of the current row |
A,a |
Enter edit mode, a means to start editing from the next character in the current cursor position, a to start editing from the last character in the current row |
O,o |
Enter edit mode, O to edit from the next line of the current cursor, o means to edit a new line on the previous line of the current cursor |
R,r |
In edit mode, r means that only the character of the cursor is replaced once, and r indicates that the text of the cursor is always replaced until the ESC key is pressed |
Delete, copy paste (implemented in normal mode)
Operation |
Function |
X,x |
In one line of text, X deletes a character backwards, and X deletes one character forward. |
Nx |
N is a number that continuously deletes n characters backwards |
Dd |
Delete the line where the cursor is located |
Ndd |
N is a number, deleting the row of the cursor on the next n rows |
d1g |
Delete all data from the line 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 of the line after the position of the character where the cursor is located |
D0 |
0 is a number that removes all characters from the line before the character position of the cursor |
Yy |
Copy the line where the cursor is located |
Nyy |
N is a number, copying the line down n rows of the cursor |
y1g |
Copy all data from the row to the first row of the cursor |
YG |
Copy all data from the row to the last row of the cursor |
Y0 |
Copy all data from the same character as the cursor to the beginning of the line |
y$ |
Copy all data at the end of the line with the same character as the cursor |
P,p |
P Pastes the copied data to the next line of the cursor, p pastes the copied data to the previous line |
J |
Combines the line of the cursor with the text on the next line |
C |
Delete the current line and turn it into edit mode |
U |
Undo Last Action |
Ctrl+r |
Redo Last Action |
4 Last-line mode
When you finish editing the file and need to save or exit the file, you need to press ESC key back to normal mode, enter WQ in the last line you can save the exit
last line parameter
Operation |
Function |
: W |
Write edits to disk |
: w! |
Force write |
: Q |
Exit vim |
: q! |
Force exit |
: Wq |
Save exit |
: wq! |
Save forced exit |
Zz |
Exit if file is not modified, save exit if file has been modified |
: W[filename] |
Save the edited file to the filename file |
: R[filename] |
Reads the edited file into the contents of another document |
: N1,n2w[filename] |
Writes the contents of the N1 line to the N2 line to the filename file. |
:!command |
Temporarily exit vim to the command line, execute command display results |
5 Find and Replace commands in vim
in normal mode, enter ":" , "/" , "?" any of these three characters can move the cursor to the bottom row of the screen, which is the last line mode, and can be found in the pattern Replace.
Find Replacement Command
Operation |
Function |
/word |
Look under the cursor for a string named word |
? word |
Look for a string named word above the cursor |
N |
Repeat the previous search action |
N |
Reverse the previous search action (find up) |
: n1,n2s/word1/word2/g |
N1 and N2 are numbers, look for word1 between the lines N1 to N2, and replace the string with Word2 : 1,100s/3308/3307/g represents 1 to 100 rows replaced by 3308 for 3307 |
: N1, $s/word1/word2/g |
Find the Word1 string from the first line to the last line and replace the string with Word2 |
: 1, $s/word1/word2/gc |
Finds the Word1 string from the first line to the last line, replaces the string with Word2, and displays a prompt character for the user to confirm before replacing it |
Noh |
Suppress highlighting after finding content |
:%s#3307#3309#g |
Global substitution |
6 VIM environment settings
Vim In use, setting the environment is also an important link in the last-line mode using the relevant commands can be related to the environment
setting Environment command parameters
Operation |
Function |
: Set Nu |
Set line number |
: Set Nonu |
Cancel line number |
: Set Hlsearch |
Highlight the searched string, Hlsearch is the default value |
: Set Nohlsearch |
The searched string is not highlighted |
: Set Autoindent |
Set Auto Indent |
: Set Noautoindent |
Cancel Auto Indent |
: Set Backup |
Set up automatic backup, default is Nobackup, if set to backup, then when the file is changed, the source file will be saved as a file named filename |
: Set Nobackup |
Cancel automatic backup of documents |
: Set Ruler |
Display the status line in the lower-right corner of the screen |
: Set Noruler |
Do not display a status line in the lower-right corner of the screen |
: Set Showmode |
Display the status line in the lower-left corner |
: Set Noshowmode |
Not realistic the bottom left corner of the status line |
: Set backspace= (012) |
In edit mode, set the BACKSPACE function, backspace to 2 o'clock, you can delete any value, 0 or 1 o'clock, delete only the characters you just entered, and cannot delete the original already existing characters |
: Set All |
Show all current environment parameter setting values |
: Set |
Display setting parameters that differ from the system default values |
: Syntax on |
Set the document to show different colors according to program-related syntax |
: Syntax off |
Canceling a document displays different colors according to program-related syntax |
: Set Bg=light |
Display text as a different color tone, light is the default value |
: Set Bg=dark |
Display text as a different tone |
Vim environment settings and logging: ~/.VIMRC, ~/.viminfo
VIM will take the initiative to record the behavior you have done so that you can work easily next time. The record operation file is: ~/.viminfo, if you have used vim, then your home folder should have this folder, this file is automatically generated, do not have to create their own, and your vim in the operation of the record can be found inside the file.
Vim ~/.viminfo can view the file history opened with vim
Through the configuration file to directly specify our accustomed to the VIM operating environment, the overall VIM device value is generally placed in the/ETC/VIMRC (System global variable) This file, however, it is not recommended to modify it, you can modify the Vim ~/VIMRC this file (default does not exist, need to create manually), Write the values you need, for example:
[[email protected] ~]# Cat ~/.VIMRC user's local variables
"Wangxing" Annotation Information
Set Hlsearch # High Brightness anti-white
Set backspace=2 # can be deleted at any time with backspace key
Set Autoindent # Auto Indent
Set Ruler # to display the status of the last row
Set Showmode # The state of the row in the lower left corner
Set Nu # can display line numbers at the front of each row
Set Bg=dark # shows a different bottom-tone
Syntax on # for syntax checking, color display
[[Email protected] ~]
Chinese code
Chinese code has gb2312, Big5, and UTF8, and so on, if the use of vim when open garbled problems need to consider the following:
1 Linux system default language data,/etc/sysconfig/i18n
2 The language of your Terminal interface (bash), which is related to the variable lang;
3 Your file was originally encoded
Encoding conversion:
Iconv-f gb2312-t UTF-8 gb1.txt >gb2.txt convert GB2312 loadline to Utf-8
7 Advanced Features
local selection and manipulation of text content (normal mode)
Operation |
Function |
V |
Select a part of the character, move the cursor after the first character you select, press "V", and the cursor will be selected |
V |
Select several rows, enter "V" on a selected line, move the cursor up or down, and the cursor will be selected. |
CTRL + V |
Select the block, move the cursor somewhere in the document, enter "Ctrl + V", the "-vimsualblock-" status appears at the bottom of the screen, move the cursor, and the rectangular area where the cursor is covered is selected |
Y |
The selected part is copied |
P |
Paste the copied part |
D |
The selected section is deleted |
8 Opening multiple Files
[Email protected]/]# Vim-o/etc/passwd/etc/group
Open Directory
In the last line mode,: New and: Split command not only open the normal file can also open the directory, a directory opened will be listed in the file information, enter to continue to open the corresponding file or subdirectory, you can also enter "?" To view Help
Vim setting up Environment files
[Email protected]/]# VIM/ETC/VIMRC
Collapse hidden text
When a document is too long and the user only needs to work on one part of it, you can hide the unwanted rows from the folds.
Setup method:
1. Enter in the last line mode: Set NU display line number
2. Enter V in normal mode, enter the visual mode, move the cursor down select you need to collapse the hidden line number, select and press the ZF key to collapse the hidden text.
Such as:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/74/11/wKioL1YSoL7Bo9OFAALfsw79n_0330.jpg "title=" Vim.png "alt=" Wkiol1ysol7bo9ofaalfsw79n_0330.jpg "/>
9 File Recovery function
When you use VIM to edit a document, Vim creates a swap document named Filename.swap in the directory of the edited document, and all actions made during editing are recorded in this temporary archive, and if the system is interrupted for some reason, then Filename.swap will function as a rescue.
Example Recover files
1) Enter Ctrl + Z in the normal mode of vim to mimic abnormal file interrupts
[1]+ Stopped VIM/ETC/VIMRC
2) Find an Exchange file in the next directory
[Email protected] etc]# ll-a. vimrc.swp
-rw-r--r--. 1 root root 12288 Feb 09:46. vimrc.swp
3) will appear when you edit again
E325:attention
Found a swap file by the name "/ETC/.VIMRC.SWP"
Owned By:root Dated:mon Feb 23 09:46:45 2015
File name:/ETC/VIMRC
Modified:no
User Name:root host Name:wx
Process id:6803 (still running)
While opening file "/ETC/VIMRC"
Dated:fri Feb 17 23:17:03 2012
(1) Another program may editing the same file.
If This is the case, being careful not to end
Different instances of the same file when making changes.
Quit, or continue with caution.
(2) An edit session for the this file crashed.
If This is the case, use ": Recover" or "vim-r/ETC/VIMRC"
To recover the changes (see ": Help Recovery").
If you do this already, delete the swap file "/ETC/.VIMRC.SWP"
To avoid the this message.
Swap file "/etc/.vimrc.swp" already exists! Prompt for Exchange file already exists
[O]pen read-only, (E) dit anyway, (R) Ecover, (Q) uit, (A) Bort:
[O]pen read-only Open a document in read-only mode
(E) dit anyway Open the edited file in the normal way, but do not load the contents of the swap file, note that doing so may cause two users to change each other's documents.
(R) Ecover is to load the contents of the swap file, save the document and exit Vim, or manually delete the swap file yourself
(D) elete it Make sure that the swap document is useless, delete the interchange document before opening the document
(Q) Uit exit Vim and return to the command prompt without any action
(A) bort ignores this editing behavior and returns to the command prompt
Follow the above prompts, you can select R, delete the original/etc/.vimrc.swp and then restore the document
Enter R He will prompt press ENTER or type command to continue
Just go straight to the line.
Press Enter or type command to continue continue with enter or type commands
4) Manually delete the temporary archive
[Email protected] etc]# ll-a. vimrc.swp
-rw-r--r--. 1 root root 12288 Feb 09:46. vimrc.swp
[Email protected] etc]# RM-RF. vimrc.swp
[Email protected] etc]# ll-a. vimrc.swp
Ls:cannot access. Vimrc.swp:No such file or directory
[Email protected] etc]#
Ten vim multi-line annotations
CTRL + V enters the column mode, moves the cursor down or up, marks the line that needs comments, then presses the uppercase I, then inserts an annotation, such as #, and then ESC, all comments, or you can run the following command:
Operation |
Function |
: s/^/# |
#用 "#" comment when moving forward |
: 2,50s/^/# |
Add "#" comment #在2 ~50 header |
:., +3s/^/# |
#用 "#" comment the current line and the three rows following the current line |
:%s/^/# |
#用 "#" Comment all rows |
To delete multiple lines of comment:
A Press CTRL + V to enter the column mode
B Select an annotation to cancel
C Press "X" or "D" (This will delete 1 columns)
This article is from the "Little Rookie" blog, please be sure to keep this source http://xiaocainiaox.blog.51cto.com/4484443/1700312
Linux VIM Editor Summary