Ubuntu Vi/vim Editor must know

Source: Internet
Author: User

Reprint Source: http://blog.csdn.net/xiajun07061225/article/details/7039413

First, why should we learn the VIM editor?

There are a lot of text editors under the command line interface of Linux. For example, there are Emacs, Pico, Nano, Joe and VIM, etc. that are often heard. Vim can be seen as a premium version of VI. Why do we have to learn vim? There are several reasons for this:

(1) All Unix like systems will have a VI text editor built in, and other text editors will not necessarily exist.

(2) Many of the software's editing interface will actively call VI.

(3) Vim has the ability to edit the program, can be active in font color to identify the correctness of the grammar, convenient program design.

(4) Simple program, fast editing speed.

Second, the following first introduce the basic use of VI and its related commands.

Three modes of the VIM Editor: normal mode, edit mode, and command line mode.

You can delete, copy, and paste in normal mode, but you cannot edit the contents of a file. Switch from general mode to edit mode to press I, I, O, O, A, a, R, R keys. Press the ESC key to return to normal mode. In the general mode, enter:,/,? Any one of the three can move the cursor to the bottom row. In this mode, you can provide operations to find data, while reading, saving, mass substitution characters, leaving VII, displaying line numbers, etc. are done in this mode. It is important to note that there is no switch between the edit mode and the command-line mode.

The most commonly used VI commands are listed below:

How to move the cursor:

[Ctrl]+[f]: The screen moves down one page, equivalent to the [PageDown] button.

[Ctrl]+[b]: The screen moves up one page, equivalent to the [PageUp] button.

0 or function key [Home]: Moves to the front character of this line.

$ or Function key [end]: moves to the last face character of this line.

G: Move to the last line of this file.

GG: Move to the first line of this file, equivalent to 1G.

N[enter]:n is a number, the cursor moves down n rows.

Find and Replace:

/word: Look down for a string called Word.

Word: Look up a string called word.

: n1,n2s/word1/word2/g: Find the Word1 string between line N1 and N2, and replace it with Word2.

: 1, $s/word1/word2/g: look for the word1 string from the first line to the last line, and replace it with Word2.

: 1, $s/word1/word2/gc: look for the word1 string from the first line to the last line, and replace it with WORD2. and display the prompt character before the replacement to confirm that the user needs to be replaced.

Delete, copy, and paste:

X,x: In a line of words, X is the backward deletion of one character (equivalent to the [Del] key), and X is a character (equivalent to [Backspace]) that is deleted forward.

DD: Deletes the entire line where the cursor is located.

NDD: Deletes the downward n-row of the cursor.

YY: The line where the cursor is copied.

Nyy: The next n line where the cursor is copied.

P,p:p pastes the copied content on the next line of the cursor, and P is pasted on the previous line of the cursor.

U: Restores the previous operation.

[Ctrl]+r: Redo the previous operation.

.: A decimal point, repeating the previous action.

General mode switch to edit mode:

I,i: Enter insert mode, I is inserted from the current cursor location. I begin the insertion at the first non-whitespace character at the current row.

A,a: Enter insert mode. A is the insertion starting at the next character where the cursor is currently located. A is inserted starting at the last character of the row.

O,o: Enter insert mode. O is inserted on the next line. O is inserted on the previous line.

R,r: Enter replacement mode. R replaces only the same character as the cursor. R will always replace the character of the cursor until the ESC key is pressed.

General mode switch to command line:

: W: Writes the edited data to the hard disk.

: Q: Leave VI behind add! To force leave.

: Wq: Leave after saving. : wq! to leave after forced save.

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:



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.

Third, the function of vim

"1" Block selection:

This feature allows us to copy the contents of a rectangular area, which is very convenient.

V: Character selection, will be the cursor through the local anti-white selection;

V: Line selection;

Ctrl + V: Block selection;

Y: Copy the anti-white place;

D: Remove the anti-white place.

Examples are as follows:

It is important to note that the paste is also pasted in the range of a block, rather than being handled in the unit of behavior.

"2" Multi-file editing

This feature makes it much easier for us to copy and paste content between two or more files.

Use the command vim name1 name2 name3 ... (each file name is separated by a space) to open multiple files at the same time.

: N: Edit the next file;

: N: Edit the previous file;

: Files: Lists all the files that are currently open by VIM.

"3" multi-window function

You can open multiple files in one window.

Enter the command: Sp{filename} to implement this function. If you want to start another file in a new window, add the file name. If the file name is omitted, the same file is opened.

Use an example to illustrate.

Vim Manpath.config open a file. In general mode, enter: Sp. The discovery opens the same file. If you re-enter: Sp/etc/hosts. It becomes:

Other key functions:

CTRL+W+J: First press CTRL, then press W to release all the keys, and then press the J (or down ARROW key), the cursor can be moved to the lower window;

Ctrl+w+k: ditto, but the light table moves to the upper window;

Ctrl+w+q: Leave.

"4" Vim environment settings

It is important to note that VIM will record I our previous behavior to facilitate our operation. It is saved in the file: ~/.viminfo.

The environment settings parameter commands commonly used by VIM are as follows:

: Set Nu Sets line number

: Set Nonu Cancel line number

: Set Hlsearch setting High brightness lookup

: Set Nohlsearch Cancel High brightness lookup

: Set backup automatically backs up files

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

: Set Showmode display 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 to show different colors according to the relevant program syntax

: Set bg=dark/light whether to display different color tones

But we don't need to reset each parameter value each time we use VIM. We can use configuration files to directly specify the VIM operating environment that we are accustomed to. The overall Vim setting value is generally placed in the/ETC/VIMRC. We generally do not modify this file. We can create this file manually by modifying ~/.VIMRC, if it does not exist. Then write the values that we want to set. For example, we can write:

[CPP]View Plaincopyprint?
    1. Vim ~/.VIMRC
    2. Set Hlsearch (note: Set can also be preceded by a colon, as a result)
    3. Set backspace=2
    4. Set ruler
    5. Set Showmode
    6. Set Nu
    7. Syntax on

Once you have created and saved this file, the default environment for that file is set when you next re-edit a file with Vim.

Ubuntu Vi/vim Editor must know

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.