How to use the Vim editor in detail

Source: Internet
Author: User

VI Editor: Full-screen text editor
Vi:visual Interface, visual interface;

Vim:vi improved, equivalent to VI upgrade version of the tool;

The new features of Vim with respect to VI: syntax coloring display, more commands, and historical records;

Vi/vim: Modal text editor
There are several modes:
Edit mode (Command mode), default mode of operation;
Insert mode (input mode), free editing of text information, and "insert" tag in lower left corner;
Ex mode (last line mode), for certain properties and functions of the editor itself, can also be used to edit the text;
Other auxiliary modes:
Replacement mode: The input information directly replaces the content of the cursor location;
Visualization mode:

Vi/vim Related Use actions:
One. Open the Vi/vim operator interface:
1.vi/vim order;
2.vim file:
1) file exists, directly using the VIM editor to open it, the default is to place the cursor at the beginning of the file, that is: the first character of the first line;
2) file does not exist, directly opened as a new file, if you execute the Save command, the files will be written to disk, if not saved, the file will disappear;
3.vim +# File:
When the specified file is opened, the cursor is positioned directly at the beginning of line # of the file, and if the given number is larger than the number of rows in the file, the cursor stays at the beginning of the last line;
4.vim + file:
When you open the specified file, position the cursor directly at the beginning of the last line of the file;
5.vim +/pattern/:
When you open the specified file, position the cursor at the beginning of the line that was first matched to the pattern;

Two. Close the Vi/vim interface:
Operations in the EX mode:
: Q
Directly close the Vim editor, do not save the modified content;
: q!
Close the Vim editor directly, forcing the exit without saving the modified content;
: Wq
Save the modified content and close the Vim editor;
: wq!
Force save for files that do not have permission to change, and force the Vim editor to close after forcing save;

Actions in edit mode:
ZZ: Save and close the Vim editor;
ZQ: Do not save the modified content, directly close the Vim editor;

Three. In Vim's edit mode, how the cursor moves:
Cursor movement actions in edit mode:
1. Character-by-word movement:
1) left ARROW or RIGHT arrow;
2) H: The cursor moves to the left; L: The cursor moves to the right;
2. Move literally:
W: Move the cursor to the beginning of the next word;
E: Move the cursor to the end of the current word or the next word;
B: Move the cursor to the first word;
3. In-line cursor jump:
0: Absolute beginning of the line;
^: relative to the beginning;
$: Absolute line end;
4. Inter-row Jump:
1) Up ARROW or down arrow;
2) J: Move down one line; K: Move up one line;
3) #G: Move the cursor directly to the beginning of line #;
4) G: Move the cursor directly to the last line of the document;
5) GG: Move the cursor directly to the first line of the document;
Cursor movement in EX mode:
: #: Move the cursor directly to the beginning of the specified line number "#";
: 1: Move the cursor directly to the beginning of the first line of the document;
: $: Moves the cursor directly to the beginning of the last line of the document;

Four. Turning pages:
PageDown or PageUp can be achieved by turning pages;
^+f: Turn down one page and turn one page forward; equivalent to PgDn;
^+b: Turn up one page and turn back one page; equivalent to PgUp;
^+d: Turn down half a page;
^+u: Turn up half a page;

Five. Delete characters:
Actions in edit mode:
1. Character-by-word deletion:
X: Delete the character where the cursor is located;
X: Delete one character to the left of the cursor position;
2. Delete all content that passes during the cursor movement:
D: Delete everything that passes during the cursor movement:

DH, DL, DJ, DK, Darrow, DW, DE, DB, DG, DGG, D0, d$, d^
DD: Delete the line where the current cursor is located;

Operations in the EX mode:
:D: Delete the line where the cursor is located, equivalent to:. D; Refers to the current line;
: #d: Delete Line #;
: m,nd: Deletes all rows starting at line m and ending in the middle of the nth line;
: m,+nd: Deletes the contents of the line starting from line M, and its subsequent n rows;
:., +nd: Deletes the contents of the row starting at the current cursor and its next n rows;
:., $d: Deletes everything from the line at the beginning of the current cursor to the end of the document;

Note: In the operation of vim, all deletions are also meant to be cut;

Six. Copy commands
Actions in edit mode:
Y: Copy all the content that passes through the cursor during the move:

YH, YL, YJ, YK, YArrow, yw, ye, YB, YG, Ygg, y0, y$, y^
YY: Copy the line where the current cursor is located;
Replication in Ex mode:
: Y: The line where the cursor is copied, equivalent to:. D; where "." Refers to the current line;
: #y: Copy line #;
: M,ny: Copies all rows beginning with line m and ending in the middle of the nth line;
: M,+ny: Copy the contents of the line starting from line M, and the following n rows;
:., +ny: Copy the contents of the row starting at the current cursor and the n rows following it;
:., $y: Copy all content from the line at the beginning of the current cursor to the end of the document;

Seven. Paste the command:
Actions in edit mode:
P (Lower): If it is a copy or cut operation inside the line, paste it on the right side of the cursor, and if it is a multi-line copy or cut operation, complete the paste at the bottom of the cursor;
P (Upper): If it is a copy or cut operation inside the line, finish pasting on the left side of the cursor, and if it is a multi-line copy or cut operation, finish pasting on the top of the cursor;
Operation of the EX mode:
:p

Eight. Mode switch:
The edit mode (command mode) is called the VI Editor's basic working mode; a broker that switches between all other modes

1. Edit Mode-->ex mode:
":": means to enter the EX mode and complete the execution of the command;
"/" or "?": Turn on the match lookup function in ex mode;
/default is the top-down search order;
? default is the bottom-up lookup order;

N and N commands jump between each matching keyword;
N: Sequential Lookup
N: Reverse Lookup

2.ex Mode---Edit mode:
Double-click the ESC key;
Enter key
Backspace key

3. Edit mode--Input mode (insert mode):
I, I, A, A, O, O, S, S, C, CC
I: Start typing at the left of the cursor position;
I: Start typing at the beginning of the line;
A: Start typing to the right of the cursor position;
A: Start typing at the end of the line;
o: Insert a new line below the current cursor line;
O: Insert a new line above the current cursor line;
S: Delete the character at the cursor position and start typing on the left side of the cursor;
S: Delete the line of the cursor, start typing at the beginning;
C: Delete the cursor position until the end of the line, and start typing at the end of the row;
CC: equivalent to s command;

4. Input mode--edit mode:
ESC key

5. Edit mode---replace mode:
R: Enter replacement mode

6. Replace mode--edit mode:
ESC key

7. Edit mode--visual mode:
V: The rectangle is selected according to the character;
V: The rectangular selection according to the behavior unit;

Attention:
1) After the selection is complete, you can use the D command to delete, or you can use the y command to copy;
2) As long as the selection is complete, immediately return to the editing mode;

All other modes can only be toggled with the edit mode, and if you try to switch between the two non-editing modes, you must go through edit mode;

Nine. Replace the command:
R: The character that replaces the cursor position without entering the input mode;

10. Undo the command:
U: Undo the operation in the current line;
U: Undo the previous edit operation;

^+r: Undo the last undone operation;

11. Repeat the previous edit operation:
.

12. Multi-Document editing:
1.vim file1 file2 file3 ...
: Next to view or edit the latter document
:p rev to view or edit the previous document
: First to view or edit one document
: Last view or edit Final Document

2.vim-o | -O file1 file2 file3 ...
-O: Open multiple windows horizontally
^+w, UP ARROW | DOWN ARROW: Switch between multiple windows;
-O: Open multiple windows vertically
^+w, LEFT arrow | Right ARROW: Switch between multiple windows;

13. Common commands in the EX mode:
1. Display and cancel line numbering:
: Set Number (abbreviated as: Set NU)
: Set Nonumber (abbreviated as: Set Nonu)

2. Turn the sensitivity of character case on or off:
: Set IgnoreCase (abbreviated as: Set IC)
: Set Noignorecase (abbreviated as: Set Noic)

3. Turn the Auto Indent feature on or off:
: Set Autoindent (abbreviated as: Set AI)
: Set Noautoindent (abbreviated as: Set Noai)

4. Turn syntax coloring on or off:
: Syntax on
: Syntax off

5. Turn on or off the find highlighting:
: Set Hlsearch
: Set Nohlsearch (abbreviated as: Nohl)

6. Save As:
: W/path/to/somewhere
: M,nw/path/to/somewhere

7. Read the contents directly from other files and add them to the current file:
: R/path/from/somefile

8. Interacting with the shell, invoking the shell command
:! COMMAND



How to use the Vim editor in detail

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.