Linux learning notes (10) Text Editor vim, learning notes vim

Source: Internet
Author: User

Linux learning notes (10) Text Editor vim, learning notes vim

Vim is a powerful full-screen text editor, the most common text editor for Linux/Unix. It is used to create, edit, and display text files. Vim features no menus and only commands.

Vim mainly has three working modes: Command mode, insert mode, and edit mode. The relationship between the three can be expressed as follows:

 

1. Common vim operations (1) Insert commands

Command

Function

A

Insert the cursor after the character

A

Insert at the end of the row where the cursor is located

I

Insert the cursor before the character

I

Insert at the beginning of the row where the cursor is located

O

Insert a new line under the cursor

O

Insert a new line on the cursor

 

(2) Positioning command

Command

Function

: Set nu

Set row number

: Set nonu

Cancel row number

Gg

To the first line

G

To the last line

NG

To row n

: N

To row n

$

Move to end of line

0

Move to the beginning of the line

 

(3) Delete command

Command

Function

X

Delete the character at the cursor

Nx

Delete the last n characters at the cursor location

Dd

Delete the row where the cursor is located

DG

Delete the row where the cursor is located to the end of the file

Ndd

Delete n rows (including the current row) after the row where the cursor is located)

D

Delete the content from the cursor location to the end of the line

: N1, n2d

Delete specified range rows

 

(4) copy and cut commands

Command

Function

Yy

Copy current row

Nyy

Copy the following n rows of the current row (including the current row)

Dd

Cut current row

Ndd

Cut the following n rows of the current row (including the current row)

P, P

Paste the cursor to the bottom or row of the current cursor

 

(5) replace and cancel commands

Command

Function

R

Replace the character at the cursor

R

Replace the character from where the cursor is located, and Press ESC to end

U

Cancel the previous operation

 

(6) search and search replacement commands

Command

Function

/String

When searching for a specified string, Case sensitivity is ignored: set ic; When unignoring: set noic

N

Searches for the next occurrence location of a specified string.

: % S/old/new/g

Replace the specified string in full text

: N1, n2s/old/new/g

Replace a specified string within a certain range

G indicates no confirmation, and c indicates confirmation.

(7) Save and exit the command

Command

Function

: W

Save changes

: W new_filename

Save as specified file

: Wq

Save changes and exit

ZZ

Shortcut Key, save the modification and exit

: Q!

Do not save the changes and exit

: Wq!

Save the changes and exit (available to the file owner and root)

2. Tips for using vim

(1) import the content of a file to a text file in the following format:

: R file name

For example, create the test text, write the following content, and copy the content of/etc/issue to the test text.

[root@localhost ~]# vi testI am ws.What's your name?My name is LiMingI am glad to see you.

Insert the issue content in the next line of the last line. In command mode, enter: r/etc/issue, which can be:

I am ws.What's your name?My name is LiMingI am glad to see you!CentOS release 6.5 (Final)Kernel \r on an \m

(2) execute the corresponding operation command without exiting vim, in the format:

:! Command

For example, run the ls command in command mode.

I am glad to see you!CentOS release 6.5 (Final)Kernel \r on an \m :!ls[No write since last change]anaconda-ks.cfg  install.log  install.log.syslog

(3) Importing command execution results is equivalent to combining the first two in the format:

: R! Command

For example, add a date to the end of the test text and enter "r!" In command mode! Date. The result is:

I am ws.What's your name?My name is LiMingI am glad to see you!CentOS release 6.5 (Final)Kernel \r on an \mFri Jan  2 06:00:55 CST 2015

(4) define shortcuts

You can define shortcuts for Common commands in the following format:

Map shortcut key trigger command

Note: to create a shortcut key, press Ctrl + v + to create a letter.

Example 1: Create a shortcut key to add the annotator # to the beginning of the line. You only need to enter map ^ p I # <ESC> in command mode, ^ P is displayed after Ctrl + v + p is pressed at the same time. I # is the comment symbol inserted at the beginning of the line #. <ESC> indicates that the edit mode is switched out to the command mode.

Then execute Ctrl + p on the first line to obtain the following results:

#I am ws.What's your name?My name is LiMingI am glad to see you!CentOS release 6.5 (Final)Kernel \r on an \mFri Jan  2 06:00:55 CST 2015

Example 2: Create a shortcut for deleting the comments # at the beginning of a line. In command mode, enter map ^ B 0x.

Example 3: Create a shortcut for the mailbox under the current cursor. In command mode, enter the following command:

: Map ^ H i123@qq.com

(5) continuous comment line. This command is similar to the search replacement command 1. (6. For example, add the annotator # to multiple consecutive lines. In command mode, enter the following command:

: N1, n2s/^/#/g

^ Indicates the beginning of the line.

To cancel the continuous line comment, enter the following command in command mode:

: N1, n2s/^ # // g

If you use "//" to indicate the annotator, you can enter the following command in command mode:

: N1, n2s/^/\/g

"\" Indicates the escape character.

For example, add a // annotator to the first two lines of the test text:

//I am ws.//What's your name?My name is LiMingI am glad to see you!CentOS release 6.5 (Final)Kernel \r on an \mFri Jan  2 06:00:55 CST 2015

(6) Replacement

In a text file, you can replace one string with another, such as replacing mail with a 123@qq.com, with the command format:

: AB mail 123@qq.com

Then write the mail in the text, and press the space or Enter key, mail is automatically replaced with 123@qq.com.

The shortcut keys or replicas usually disappear when the system restarts. If you want to save the definition or replace of the shortcut keys for a long time, you can set them in the home directory of each user (such as root at/root, the user writes the configuration file under/home/user. vimrc (root, for example,/root /. vimrc), and then write the required command in edit mode in the configuration file, and the line does not need to add a colon (:).

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.