0050 Linux VIM Command

Source: Internet
Author: User

1. Mode switch

Vim mode $ VI filename goes into normal mode, which is the command mode used to perform most common editing commands and cannot be entered

Knock I into insert mode, this is normal editing mode, press ESC to return to normal mode

Enter in normal mode: (colon) into the bottom row mode, also executes the command mode, the most used is to find

Whether in insert or bottom-line mode, press ESC to return to normal mode, insert mode and bottom line mode are not able to switch directly to each other, only through normal mode

2. Save exit


Save, exit, environment variables
: w: Writes edited data to the hard drive archive (common)
: w!: If the file property is read-only, the archive is forced to be written. However, in the end can be written, or with you on the file permissions related Ah!

: Q: Leave VI (Common)
: q! : If you have modified the file, do not want to store, use! Do not store files for forced departures.

: Wq: Leave after storage

: x: Left after storage

: wq! : Forced to leave after storage

ZZ: This is the upper case Z Oh! If the file does not change, then do not store away, if the file has been changed, then save and leave!


: w [filename]: Save As
: R [FileName]: In the edited data, read into the data of another file. The file "filename" is also added to the following line of the cursor
:! : Command temporarily leaves VI to command column mode to perform command display results! For example ":! Cat./test "To view the contents of the test file in the current folder in VI

3. Environment variables

: Set Nu: Sets line number (common)

: Set Nonu: Cancel line number (common)

: Set Hlsearch: Setting High Brightness Lookup

: Set Nohlsearch: Cancel High Brightness lookup

: Set backup: Automatically back up files

: Set ruler: Turn on the lower right corner status bar description

: Set Showmode: Displays status bar such as insert in the lower-left corner

: Set backspace={0,1,2}: Sets the backspace key function. At 2, any character can be deleted. to 0 or 1 o'clock only the characters you just entered can be deleted.

: Set all: Displays all current ambient parameter values

: Set: Displays parameter values that differ from system default values

: Syntax on/OFF: whether to open according to the relevant program syntax to display different colors

: Set Bg=dark/light: Whether to display different color tones

4. Cursor operation

h or LEFT ARROW key (←): The cursor moves one character to the left

20h or 20 (←): The cursor moves 20 characters to the left,

J or DOWN ARROW key (↓): Move the cursor down one line
20J or 20 (↓): Move the cursor down 20 lines,

K or Up ARROW key (↑): Move the cursor up one line
20k or 20 (↑): The cursor moves up 20 lines

L or right ARROW key (→): The cursor moves one character to the right
20l or 20 (→): The cursor moves 20 characters to the right,

^: Move the beginning of this line, as opposed to the

Ctrl + F: Screen "down" to move one page, equivalent to [PAGE DOWN] key (common)
Ctrl + B: Screen "Up" to move one page, equivalent to [PAGE UP] key (common)
Ctrl + D: Screen "Down" moves half page
Ctrl + u: Screen "Up" moves half page
Ctrl + E: Screen "Down" moves one line
Ctrl + y: Screen "up" to move one line
+: Cursor moves to the next column of non-whitespace
-: Cursor moves to the previous column of non-whitespace
N<space>: that n means "number", pressing the number and then pressing the SPACEBAR, the cursor moves the n characters of the line to the right. For example 20<space>, the cursor moves 20 character distances to the back.
N<enter>: n is a number. Move the cursor down n rows (common)
0: or function key [Home] move the cursor to the front of the next line and go to the insert State
$: or the function key [end] moves to the last face of this line prompt (commonly used), where the $ in the regular is the meaning of the end, so that understanding can remember
H: The cursor moves to the first character of the line at the top of the screen, h you write it as the initials of the header, so it's okay to remember
M: The cursor moves to the first character of the Central line in this screen, m you write it as middle abbreviation
L: The first character of the line that the cursor moves to the bottom of the screen, l you write it down as the last abbreviation.
G: Move to the last line of this file (common)
NGn: is a number. Move to the nth row of this file. For example, 20G will move to line 20th of this file.
GG: Move to the first line of this file, equivalent to 1G (common)

5. Delete, copy, paste, revoke

X, x: In a line of words, X is the backward deletion of a character (equivalent to the [Del] key), X is to delete a character forward (equivalent to [backspace] is the backspace) (commonly used)
NX N: is a number that continuously deletes n characters backwards. For example, I want to continuously delete the 10 characters after the cursor, "10x".
NX N: is a number that continuously deletes the n characters preceding the cursor. For example, I want to delete the 10 characters before the cursor, "10X".

DD: Delete the entire line where the cursor is located (common)
YY: The line where the cursor is copied (common)

Ndd:n is a number. Delete the row where the cursor is down n rows, for example 20DD to delete 20 rows (common)
Nyy:n is a number. Copy cursor row down n rows, for example 20yy copy 20 rows (common)

D1G: Delete all data from the row to the first row of the cursor

Y1G: Copy 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
YG: Copy all data from the row to the last row of the cursor

d$: Deletes the cursor to the last character of the line
y$: Copies all data from the same character as the cursor to the end of the line

D0: That is 0 of the number, remove the cursor, and go to the first character of the line
Y0: Copies all data from the same character as the cursor to the beginning of the line

P, p:p Copy the data, paste the next line of the cursor, P copy the data, paste it to the previous line of the cursor
J: Combine data from one row of the cursor to the same line
C: Repeatedly delete multiple data, you can use the upper and lower keys to decide whether to delete the cursor above, or the following
U: Undo. Common
Ctrl + R: Undo Revocation. Common

6 Select, Find, replace

V: Character selection, the cursor will pass the place of the white selection!
V: Line selection, the line of the cursor will be reversed white selection!
Ctrl + V ": Block selection, you can select data in a rectangular way
Y: Copy the anti-white place.
D: Remove the anti-white place

/string: Look under the cursor for a name called string string
? string: Look for a string above the cursor called string

N: Forward lookup, search for string, can be understood as next

N: Reverse lookup, search out the string, can be understood as not next

: n1,n2s/string1/string2/g: The N1 Here is the number of start rows to find, N2 is the number of rows to find the end. ": 2,7s/ddd/fff/g" in line 2nd, line 7th, replace DDD with FFF
: 1, $s/string1/string2/g: look for the string1 string from the first line to the last line, and replace the string with string2! Common
: 1, $s/string1/string2/gc: look for the string1 string from the first line to the last line, and replace the string with string2! and display the prompt character before the substitution to the user to confirm (confirm) whether need to replace! Common

7. File Recovery


Currently the main editor has a recovery function, VIM is no exception. Vim uses "save" files to retrieve data.
Whenever we edit with Vim, Vim automatically creates a new file named Filename.swap under the directory of the edited file. This is a staging file, and what we do with the file filename will be recorded in this file. If the system crashes unexpectedly, causing the file to not be saved properly, the staging file will work. Here's an example to illustrate (note: I'm using Ubuntu).
Open the terminal, enter the command, copy the manpath.config below the ETC directory to the TMP directory, and change the current working directory to TMP:
Cp/etc/manpath.config/tmp
Cd/tmp
Use Vim to edit manpath.config files: Vim manpath.config.

When we press CTRL + Z in the general mode of VIM, VIM will be thrown into the background for execution. After returning to the command prompt environment, we simulate the abnormal interruption of vim's work.

Kill-9% 1; Force kills the established process.

This causes the scratch disk to fail through the normal process, so the staging file does not disappear, but remains. When you edit that file again (enter the command vim manpath.config), it appears (Ubuntu 11.10):

At this point, there are six buttons to use:

O (pen for read-only): Opens into a read-only file.

E (DIT): Opens the file you want to edit in normal mode, and does not load the contents of the staging file. It is easy to get two users to change each other's files in a problem.

R (ecover): Loads the contents of the staging file.

D (elete): If you are sure that the staging file is useless, you can delete it.

Q (uit): Go back to the command line without any action.

A (bort): Ignores this editing behavior, similar to Q.

Note that this staging file will not be automatically deleted after you end vim and must be deleted manually. Otherwise, the prompt will appear each time you open the corresponding file.

8. Macros

This is an INI-type configuration file, you can see that each line is preceded by a comma, now if you need to remove the comma in front of each line, what should I do? Press X at the beginning of the first line, then press J, then press X ... Repeat this? Yes, I did, but what if the file has 100 lines to change? or 1000 lines?

;=====================================================================================; This was a sample configuration file when upgrading XXX using InstallShield.; Author:        ini_always;date:          8/24/2011; Last modified:9/20/2011; Note:install script does not verify whether the configuration file was in a ' well '; format, a wrong format may leads to Inst Allation Failure.;i F More information are needed, please check the document for details. =====================================================================================

  

Okay, cut the crap and get to the chase. The so-called macro, in Vim refers to a sequence of operations in a particular order, we can record their own sequence of operations, and then repeat the sequence multiple times to simplify some kind of repetitive operation. VIM macro has the process of recording and playing, recording is what you teach Vim to do, play is vim according to you teach automatic operation. Therefore, for the above file processing, the macro recording is first:

1. Position the cursor in the first row;

2. Enter QA in normal mode (you can also enter QB, QC, etc, here A, B, c refers to the register name, VIM will put the recorded macro in this register) (PS: If you do not know what is the VIM register, please put the dog search);

3. Normally, Vim's command line displays the word "Start recording", at this time, position the cursor to the first character (press 0 or |), then press X to delete, press J to jump to the next line;

Enter Q in 4.normal mode to end the macro recording.

Well, after the above steps, we define a macro stored in register a, whose sequence of operations is: 0->x->j, that is, jump to the beginning of the line, delete, jump to the next row.

Now that the first row has deleted the comma at the beginning of the line, and the cursor is already on the second line, now enter @a in normal mode to play the macro we just recorded in register a. As a result, the comma at the beginning of the second line is deleted, and the cursor stops in the third row.

That's not easy, is it? You must think so, to delete 100 lines, I have to enter 100 @a, I might as well manually delete it. Oh, vim has long thought, input [email protected], OK, the remaining 7 lines are all done. (PS: Add a number to the front of the command, which means how many times to execute the command)

Of course, this example is very simple, but it is also typical. Using the good vim of the macro, you can make some of the original boring work to be much simpler.

9 (. ) command

Repeat Last Action

0050 Linux VIM Command

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.