One, what is vim
The VI command is the most versatile full-screen plain text editor in Unix operating systems and UNIX-like operating systems. The VI editor in Linux is called Vim, which is the enhanced version of VI (VI improved), fully compatible with the VI editor, and implements a number of enhancements.
The text editor under Linux also has Emacs, notepad++, TextMate, Sublime text and so on, vim editor is the most widely used one, the function is also very powerful, today we will introduce the VIM editor
Vim is the most famous text/code Editor on Linux, and the GVim of the Vi editor of the early years, while the Windows edition of the system is the version. Its biggest feature is the full use of keyboard commands to edit, away from the mouse operation, although making it difficult to get started, but after the introduction of the keyboard flow of a variety of clever combination operation can bring extremely significant efficiency improvement.
So Vim and modern editors (such as Sublime Text) have very big differences, and the introductory learning curve is steep, and many key combinations and commands need to be remembered, and are now considered to be expert, geek-only editors. Although Vim is already an antique-grade software, there are countless newcomers to the difficulty of learning to use, it can be seen in the classic and popular degree. In addition, because Vim is very configurable, a variety of plug-ins, syntax highlighting color scheme, and so many, whether as a code editor or manuscript writing tools are very powerful, then the operation of good vim can make your work easy!! Because many times, Linux as a server does not open the graphical interface, when the need for remote operation, you can only use the SSH "command character" shell interface to manipulate it, if you need to modify the server page code or configuration file, VI or VIM command is your best and most convenient is the most powerful partner! Believe me, learn physics VIM, go around the world is not afraid ...
Second, the use of vim
Vim Editor supports editing mode and command mode, editing mode can complete the editing function of the text, command mode can complete the operation of the command, the correct use of the VIM editor must master the two modes of switching. By default, the Vim editor is opened and automatically enters command mode. Switch from edit mode to command mode use "ESC" key to switch from command mode to edit mode using "a", "a", "O", "O", "I", "I" Keys.
Once you've installed an editor, you'll want to enter something in it and see what the editor looks like. But Vim is not like this, please follow the following command:
After the file path starts VIM, VIM is in normal mode. Let's go to insert mode, press key I. (Chen Hao's note: you will see the words - Insert - in the bottom left corner of vim, indicating that you can input in the way of insertion.) at this time, you can input the text, just like you use "NOTEPAD". If you want to return to normal mode, press ESC.
Now, you know how to switch between Insert and Normal mode. Here are some commands that will allow you to survive in Normal mode:
I → insert mode, press ESC to return to normal mode. X → delete a character where the current cursor is. : W Q → save + exit (: W save,: Q exit) (: W can be followed by the file name) DD → delete the current line, and save the deleted line to the clipboard P → paste the clipboard recommendation: hjkl (it is recommended to use its moving cursor in strong cases, but it is not necessary) → you can also use the light mark key (←↓↑→). Note: J is like the down arrow. : help < command > → displays help for related commands. You can also type: help instead of the command. (input required to exit help: q)
Vim's pattern diagram
Vim's built-in commands
Ctrl+u: Half-screen to the top of the file;
Ctrl+d: Half-screen to the end of the file;
Ctrl+f: Turn over a screen at the end of the file;
Ctrl+b: Turn one screen to the top of the file;
ESC: Switch from edit mode to command mode;
ZZ: Command mode to save the changes made by the current file after exiting VI;
: line number: The cursor jumps to the beginning of the specified line;
: $: The cursor jumps to the beginning of the last line;
X or x: Delete one character, x after deleting the cursor, and x before deleting the cursor;
D: Delete all the characters from the current cursor to the end of the line where the cursor is located;
DD: Delete the line content of the cursor;
NDD: Deletes the current line and its subsequent n-1 rows;
Nyy: Saves the contents of the current row and its next n rows to the register? In, where? is a letter, n is a number;
P: Paste text action to paste the contents of the buffer below the current cursor position;
P: Paste the text action, which pastes the contents of the buffer area above the current cursor position;
/string: Text lookup operation to find the contents of the specified string from the current cursor position to the end of the file, and the found string is highlighted;
? Name: Text lookup operation to find the contents of the specified string from the current cursor position to the head of the file, and the searched string will be highlighted;
A,BS/F/T: The Replace text operation, which is used to change the F string to a T string, between line A and line B. where "s/" means a replacement operation;
A: Add text after the current character;
A: Add text at the end of the line;
I: Inserting text in the current word match either;
I: Inserting text at the beginning of the line;
O: Insert a blank line after the current line;
O: Insert a blank line before the current line;
: Wq: In the command mode, the execution of the disk exit operation;
: w: in command mode, perform disk operation;
: w! : In command mode, execute the forced disk operation;
: Q: In command mode, perform exit VI operation;
: q! : Executes the forced Exit VI operation in command mode;
: E file name: In command mode, open and edit files of the specified name;
: N: In command mode, if you open more than one file at the same time, continue editing the next file;
: F: In command mode, displays the current file name, the line number of the line where the cursor is located, and the display scale;
: Set number: Used in command mode to display line numbers at the leftmost end;
: Set nonumber: In command mode, used to not display line numbers at the leftmost end;
We can also set the line number in the configuration file to take effect permanently
Global:/ETC/VIMRC
Personal: ~/.VIMRC
Some of the extended commands of vim
. → (decimal point) You can repeat the last command
n<command>→ repeat a command n times
Here is an example, find a file you can try the following command:
2dd → delete 2 lines and 3P → paste text 3 times for 100idesu [ESC] → write down "Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu Desu. 3. → repeat the previous command - 100 "Desu". 3 300, you see, how smart VIM is
You want to make your cursor move more efficiently, you must understand the following commands, never skip.
Ng → to line n (note that G in the command is capitalized, and I usually use: n to line n, such as: 137 to line 137) GG → to line 1. (equivalent to 1g, or: 1) g → to the last line. Move by word: W → to the beginning of the next word. E → to the end of the next word. >If you think the word is by default, use lowercase E and W. By default, a word consists of letters, numbers, and underscores (program variables) > if you think the word is separated by a blank character, you need to use uppercase E and W. (program statement)%: move the matching bracket, including (, {, [(you need to move the cursor to the bracket first) * and ×: match the word where the cursor is currently, move the cursor to the next (or previous) matching word (* is the next, and × is the previous)
The 0y$ command means:
0 → first to the beginning of line y → copy $→ copy to the last character of the line
You can enter ye to copy from the current position to the last character of the word.
You can also enter a y2/foo to copy the string between 2 "foo".
There is still a lot of time that you must press Y to copy, and the following command will be copied:
D (delete) V (visual selection) Gu (change upper case) Gu (change lower case), etc
(visual selection is a very interesting command, you can first press V, then move the cursor, you will see the text is selected, and then you may D, also can y, can also be capitalized, etc.)
By mastering these commands, you can use vim very comfortably.
Let me tell you some advanced uses of VIM
Move the cursor over the current line: 0 ^ $ f f t T,;
0 → to the beginning of the line ^ → to the first non blank character $→ to the end of the line g → to the last non blank character position of the line. FA → to the next character a, you can also fs to the next character s. t. → to the first character before the comma. Commas can become other characters. 3fa → find the third occurrence of a in the current row. F and t → same as F and T, but in the opposite direction.
Find and Replace in general mode
/word: Looking for a string after the cursor to Word, when the first word is found, press "n" to continue searching after a
? Word: Look for a string named word before the cursor, and when you find the first word, press "n" to continue searching for the previous
: n1,n2s/word1/word2/g: Find word1 This character between N1 and N2 and replace it with WORD2, you can also change "/" to "#"
: 1, $s/word1/word2/g: from the first line to the last row, find word1 and replace it with WORD2
: 1, $s/word1/word2/gc: plus the role of C is to require user confirmation before replacing
Vim Batch Add comment
1, CTRL + V use the arrow keys up and down the selection range;
2, shift+i into the editing mode, add "#". Then press ESC to exit (also understood as run bulk add);
Delete the added comment lines in bulk.
CTRL + V Use the arrow keys to select to remove the range of annotations, press "X";
1. Matching pairs of parentheses
Match: Set Showmatch, abbreviated as set SM
Cancel: Set NOSM
2. Auto Indent
Enable: Set AI
Disabled: Set Noai
3. Highlight Search
Enabled: Set Hlsearch
Disabled: Set Nohlsearch
4. Syntax highlighting
Enabled: Syntax on
Disabled: Syntax off
5. Ignoring the case of characters
Enable: Set IC
Disabled: Set Noic
6. File format
Enable Windows format: Set Fileformat=dos
Enable UNIX format: set Fileformat=unix
7. Set the text width
: Set textwidth=65 (VIM only)
: Set wrapmargin=15
These are temporary and can be written to the configuration file if they are to take effect permanently.
Global:/ETC/VIMRC
Personal: ~/.VIMRC
Above is some of the most common commands of vim, I suggest you to practice these commands a lot, sometimes learning vim is like a dead back some things, more practice and use can master.
Vim Text Editor