Vim's general usage skills

Source: Internet
Author: User
Vim's general usage skills
Author: Geminis published on:

In each user's home directory, there is a VI Configuration File ". vimrc "or ". exrc ", you can edit it so that these settings are valid every time you start VI.
For example, add the following setting line:
Set nu display row number
Set Nonu does not display row numbers
Case Insensitive for set IC search
Case sensitivity considerations for set noic search

Note:

In the text, [] indicates an option or a short text, but in addition to [] In pattern matching, see pattern matching.

Source code:

VI Vim usage
Vim Editor

First, let's briefly talk about the VI Editor-Unix General Editor,
Read as Vee-eye (short for visual editor ).
(Vim here is the improved VI, short for VI improved. Vim is written by Bram moolenaar)
VI Working Mode
It can be divided into command mode, insert mode, and ex mode;
Press ESC to bring VI to the command mode;
The insert mode is the text input mode.

Some simple operations of VI:
Normal operation:
Insert text
Command I, A, I, A, O, O
I enter I in command mode, which is common to a common text editor;
A or enter a. The difference is that a inserts additional text after the character, and I inserts it before the character;
Insert at the beginning of line I;
Insert at the end of line.
O create a blank line under the cursor
O create a blank line on the cursor
Modify and delete text
Command C, x, D, D
CW deletes the current position to the end of the word in this position, and inserts text into it. CW deletes English words, which are unavailable in Chinese.
C $ or C: Delete the current location to the end of the row, and insert text.
X: only delete the current character. If you want to delete one Chinese character, use 2x (of course, you can also use 4x 8x) because one Chinese Character occupies two characters.
CC deletes a row, and can insert text.
R: Delete the current character, and insert text.
DW only deletes the current position to the end of the word.
[N] dd: Delete [N] a row. Of course, you can use 2dd 3DD to delete two or three rows.
D. Delete it to the end of the row.

Paste, copy, and merge
Command P, Y, J, and "(quotation marks)
P after you perform the above delete operation, the deleted text will remain in the cache. When you input P, the deleted text will be pasted behind the current cursor.
Y has a combination of (YW, 4yy, y $, etc.) // copy the word, Copy four rows, copy to the end of the row, and then paste it with P
J. Merge the next row to the current row. Note that J is capitalized.
"[X] YY name your buffer. Add a colon and a name (A to Z) before the Copy command, for example," ryy "d2dd" ZD) to copy a row to the r buffer, delete two rows to the D buffer, and delete the cursor to the end of the period to the Z buffer.
"[X] P: paste the X buffer to the current position.
The default value is 1 ~ 9 The buffer zone is the first nine delete operations.
Visualized operation
Command V, V, CTRL + V

When gvim is used, you can use the mouse to select the text you want to copy and delete, and then press d (delete), P (paste), and Y (copy) to operate your text,
Of course, you can also share data with other programs through the clipboard.
V: For Vim in xterm, enter the command V to enter the visible State. In this case, you can right-click and copy and paste it.

Repeat and undo
Command. (DOT), u, u
. Repeat the previous operation. If it is not the same type, it just repeats the command you entered last time.
CTRL + R redo
U undo the last operation
U undo any modification of the current row.
Search Text
Command /,? , N, n
/Search for the text to enter the command mode and enter/[The text you want to search for]; for example, search for edit and enter/edit;
? And the text to be searched forward is input? [The text you want to search for];
N when there are many search results, enter N in command mode to find the next search result, and enter n
N indicates the next reverse search result.

Open, save, and exit
Enter the ex mode first, that is, enter the following command in command mode: then enter the command.
Command ZZ, Q, W, E, R
: Enter ZZ (uppercase) in command mode to save + exit;
: W. Save the text and enter command mode. Enter W to save the text;
: Q! Enter Q to force exit! ;
: E! Undo All current edits. Enter e if the original status is returned! ;
: W [filename] is saved as W [filename].
: E [filename] Open another file (save the file before opening the new file.
: R [filename] after reading the filename content to the current position of the current file
Move cursor
Command hjkl, (,), {,}, E, W, B, GG, G

Generally, hjkl can move the cursor directly with the arrow keys on the keyboard. The page up/down key is the front and back pages;
Or, in command mode, press
H (left) J (bottom) K (top) L (right)
Or add a number to the base and move several rows. For example, 4j indicates moving four rows down.
For example, 200 GB means moving 200 rows. CTRL + G is to display the information of the current row (the number of rows at the position of the text );
G to the end of the file
Gg To File Header

E. Input e to move the cursor with one word,
(Beginning of the current sentence
) The next sentence starts. For example, 2) move two sentences forward.
{Start of segment
} Next section
Before B and W, words are moved in units.

Mark command M, '(single quotes),' (reverse quotes are ~ The key there)
M [x] Mark X (~ Z) position. For example, MB indicates that the current cursor position is B.
'X move the cursor to the beginning of the line marked by X
'X move the cursor to the original position marked by X
''Move to the beginning of the last marked row
''Move to the previous cursor position;
Zl zh Zs ze window left roll right roll left start right end // configurable: Set sidescroll = 32 set the scroll length (I don't seem to be able to use this command)
Others
CTRL + L screen Flushing
Return to the beginning of the next row
-, + Move the cursor up and down rows, and the beginning of the upper and lower rows (it seems that the + number must be the + number of the keypad );
CTRL + G display current row Information
H screen start point
M screen center
L screen tail

Combined Use Case command mode
Example: Existing text

Aaabbb
Cccdddeeefff (cursor in the first line)

Enter/AAB press enter D/CCC press enter, then the current location AAB (including Aab) will be deleted, all data before CCC will be copied to the buffer (similar to cut ). Of course, D can also be used as Y for copying purposes, and then pasted with P.

Vim ex status when in: Ex mode, you can use a tab to automatically complete the command (Use: Set nu to display the number of rows) and can use | you can combine multiple commands at the same prompt

Text Search, replace, move, delete, and copy
Command: G/,: S/,: M,: D,: Co (or: T)
: G/[text] global search text. move the cursor to the last search text and display the row where all data is added.
: G! /[Text] rows without [text]
: S: [line N], [to line m] S/[to replace]/[to replace] n-M, if n is not present, m, replace the current row.
: %/S/[to be replaced]/[replaced]/g for global replacement. % Indicates each row. In addition, "." indicates the current row, "$" indicates the last row, and "+-" indicates the upper and lower N rows of the row. For example, "-2" indicates the second row above the current row.
: M: [data to be moved] M [place to move] example: 20, 30m100 20-30 rows move to before 100 rows (the last row is 100 rows)
Example: G/^ $/D: delete all empty rows. Add the delete action.

Search Mode
. (Point number) is a single character. The Chinese character uses two points ..
* Match zero or multiple first characters (not any characters). For example,/abcdl * searches for abcdl or starts with ABCD, and finds all /.*
^ Indicates starting from the beginning of the line
$ At the end of the expression, the regular expression is used at the end of the row.
/Escape/. Indicates searching./* Indicates searching * // indicates searching/
[] [0 ~ 9] indicates matching 0 ~ 9 any number in the same way [~ Z] and [~ Z], and [ab3a] indicates any character of A, B, 3, and.
/<,/> Start or end a word with a space or punctuation. For example, inux/> matches Linux.
/R press ENTER
/T Tab
Execute the sh command
:! [Shell Command] example :! Ls

Add data to the end of the File>
Example: 1, 10 W> newfile

Note that Vim can combine some commands for application.
: 1, 3D | S/They/she/delete one to three rows, and replace the current row they with she
:.,/While/d Delete the current row to match all data in the while row (including the current row and the row containing the while)
:/AAA/,/BBBB/D. Delete the row from AAA to BBBB (including the two rows)
:/AAA/,/BBB/M. AAA move to BBB row to current row

Split screen
: [N] SP [partition] [filename] opens a window with N rows in height and opens the filename file.
CTRL + W [x] to prepare for window operations, where X is the function key to be selected after pressing CTRL + W (see the next line) for example, press Ctrl + W and then t to go to the top window.
CTRL + W [S/W/T/B/C/Q/R/I/=] S: Split the screen again. W: Next window; T: Top window; B: bottom window, c Close Window Q exit window R loop window I open and enter new window = all windows are in the same height
: Hide hides the current window. When this file buffer is not closed (that is to say, the file is still the previous file after it is opened with: E [filename] again)
: Q: Close the window and release the buffer.
: Qa [!] Close all windows and force exit Vim;
: On [ly] Only displays the current window
: New [filename] create a file and open a new window.
: Res [ize] [N]: res [ize] [+-N] Change the window size or increase or decrease the number of rows

Programming with vim
: Make [arguments] Run make and move it to the first error,
: Cf [Ile] [!] [Errorfile] reads the error file and moves it to the first error ,! Force Vim to move to another buffer zone,
: Cl [ist] [!] Listing errors with file names. Yes! Listing all errors,
: CC [!] [N] If n exists, error N is displayed; otherwise, the current error is displayed again,
: CN [ext] Move to the next error.
: CP was moved to the previous error.

Another useful programming function is to use ctags (Unix Command to mark functions in C language. This command is used in combination with the tags of VI)
Use the ctags command First :! Ctags file. C: Create the tags file of the C program, and then use the tag [function name] in VI. Then VI calls out the original C file and jumps to the function definition.
Example ::! Ctags *. c
: Tag AAA, where BBB. C contains the AAA function, so it will jump to the AAA definition.
: Tn jump to the next match flag
: Indicates the last identifier of TP.

Set Vim
: Set all shows all options.
: Set nu [mber] shows the number of rows.
: Set binary readable binary file
: Syntax on syntax highlighting
: Set taglength = 4 set the Tag Length
You cannot remember the command that can be completed by using the tab key (you can also display the command that you can select)

Vim configuration file. vimrc
There is a vimrc_example.vim file under the vim installation directory. You can copy it to your working directory as your original Vim configuration file (the file is changed to. vimrc)

VI Citation
Finally, VI certification is perfect.
Argument: VI is six in the number of Rome. A natural number smaller than six can divide 6 by 1, 2, 3, 6 = 1x2x3; 6 can be represented as 6 = 1 + 2 + 3. Obviously, VI is a perfect number, so VI is perfect.

For Vim
In the roman numerals, VIM may be expressed as (1000-(5 + 1) = 994, and is just equal to 2x496 + 2, while 496 can be either, or, 62,124,248 division, and 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248 = 496
Therefore, 496 is a more perfect number. We can see that Vim is more perfect than vi.

 

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.