[Getting started with Linux] vi/vim editor is required

Source: Internet
Author: User
Tags expression engine

1. Why should we learn the vim editor?

There are many text editors in the command line interface of Linux. For example, we often hear about Emacs, pico, nano, joe, and vim. Vim can be viewed as the advanced version of vi. Why do we have to learn vim? There are several reasons:

(1) All Unix like systems have built-in vi text editors. Other text editors do not necessarily exist.

(2) Many software editing interfaces call vi.

(3) vim is capable of editing programs. It can identify the correctness of the syntax by font color to facilitate programming.

(4) The program is simple and the editing speed is fast.

Related reading:

Powerful Vim Editor

Build a Vim Development Environment on CentOS 6.2

Vim 7.4a released, a new and faster Regular Expression Engine

Install the highlighted Vim editing tool in CentOS 5.4

Vim tips: C language settings

Set the Vim row number in Ubuntu

II. The following describes the basic usage of vi and related commands.

The vim editor has three modes: general mode, edit mode, and command line mode.

You can delete, copy, and paste files in normal mode, but cannot edit the file content. You can press the I, I, o, O, a, A, r, and R keys from the normal mode to the editing mode. Press Esc to return to normal mode. In normal mode, enter :,/,? Any of the three can move the cursor to the bottom row. In this mode, you can find data, and read, save, replace a large number of characters, exit vii, and display the row number. Note that the edit mode and command line mode cannot be switched between each other.

The following lists the most commonly used vi commands:

How to move the cursor:

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

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

0 or function key [Home]: Move to the top character of this line.

$ Or function key [End]: Move to the last character of this line.

G: Move to the last row of the file.

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

N [Enter]: N is a number, and the cursor moves down N rows.

Search and replace:

/Word: search for a string named word.

? Word: search for a string named word.

: N1, n2s/word1/word2/g: searches for the word1 string between line n1 and line n2 and replaces it with word2.

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

: 1, $ s/word1/word2/gc: searches for the word1 string from the first row to the last row, replace it with word2. a prompt character is displayed before replacement to confirm whether to replace it.

Delete, copy, and paste:

X, X: in a row, x deletes one character (equivalent to the [Del] Key) backward, and X deletes one character (equivalent to [Backspace]) forward.

Dd: Delete the entire row where the cursor is located.

Ndd: delete n rows down where the cursor is located.

Yy: copy the row where the cursor is located.

Nyy: copy the n rows down the cursor.

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

U: Restore the previous operation.

[Ctrl] + r: redo the previous operation.

.: Decimal point. Repeat the previous operation.

Switch from normal mode to edit mode:

I, I: Enter the insert mode, I is to insert from the current cursor. I is to start inserting the first non-space character in the current row.

A, A: Enter the insert mode. A is to insert the cursor from the next character. A is inserted starting from the last character of the row.

O, O: Enter the insert mode. O is inserted in the next row. O is inserted in the previous row.

R, R: Enter the replacement mode. R only replaces the character where the cursor is located once. R will replace the character of the cursor until the Esc key is pressed.

Switch to the command line in Normal Mode:

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

: Q: Leave vi. and add it later! To force exit.

: Wq: Save and exit. : Wq! To force save and then exit.

 

Currently, all major editors have the restoration function, and vim is no exception. Vim saves data by saving the file.

Every time we use vim for editing, vim will automatically create a file named filename. swap under the directory of the edited file. This is a temporary file. All operations we perform on the file filename will be recorded in this file. If the system crashes unexpectedly and the file is not properly saved, the temporary file will play a role. The following is an example (Note: I use Ubuntu ).

Open the terminal, enter the command, copy manpath. config under 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 the manpath. config file: vim manpath. config.

Press Ctrl + z in the normal mode of vim, and vim will be thrown to the background for execution. After returning to the command prompt environment, we simulate the failure of vim.

Kill-9% 1; force the process to be killed.

As a result, the temporary disk cannot end through the normal process, so the temporary file will not disappear, but will continue to be retained. When you edit the file again (enter the command vim manpath. config), it will appear (ubuntu 11.10 ):

At this time, there are six buttons available:

O (pen for Read-Only): Open a Read-Only file.

E (dit): Open the file to be edited in normal mode, and the content of the temporary file is not loaded. This can easily cause two users to change each other's files.

R (ecover): load the content of the temporary file.

D (elete): If you are sure this temporary file is useless, you can delete it.

Q (uit): Return to the command line without any operation.

A (bort): Ignore this editing behavior, which is similar to Q.

Note that the temporary file will not be automatically deleted after you finish vim. You must manually delete it. Otherwise, this prompt will appear every time you open the corresponding file.

  • 1
  • 2
  • Next Page

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.