Linux Learning Notes (10) Text editor vim

Source: Internet
Author: User

Vim is a powerful full-screen text editor, Linux/unix's most commonly used text editor, to Create, edit, and display text files . vim is characterized by no menus, only commands .

Vim mainly has three kinds of working modes, namely command mode, insert mode and edit mode . The relationship between the three can be expressed as:

1 Vim common operation (1) Insert command

Command

Role

A

Insert in the word specifier the cursor

A

Insert at the end of the line where the cursor is

I

Insert in the word match either the cursor

I

Insert at the beginning of the cursor

O

Insert a new line under the cursor

O

Insert a new line on the cursor

(2) Positioning command

Command

Role

: Set Nu

Set line number

: Set Nonu

Cancel line number

Gg

To the first line

G

To the last line

NG

to Nth row

: N

to Nth row

$

Move to end of line

0

Move to the beginning of the line

(3) Delete command

Command

Role

X

Delete the character at the cursor location

Nx

Delete the first n characters at the cursor location

Dd

Delete the cursor in the row

Dg

Delete the line from the cursor to the end of the file

Ndd

Delete n rows after the cursor line (including the current row)

D

Delete the cursor at the end of the line

: n1,n2d

Delete a specified range of rows

(4) Copy and cut commands

Command

Role

Yy

Copy when moving forward

Nyy

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

Dd

Cut when moving forward

Ndd

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

P,p

Paste at the current cursor row or line

(5) Replacement and cancellation commands

Command

Role

R

Replaces the character at which the cursor is located

R

To replace a character from where the cursor is, press ESC to end

U

Cancel the previous action

(6) Search and search replacement commands

Command

Role

/string

Searches for the specified string, ignoring case when searching: Set IC, canceling ignore: Set Noic

N

Searches for the next occurrence of the specified string

:%s/old/new/g

Replace the specified string with full text

: n1,n2s/old/new/g

Replace a specified string within a certain range

Where g means not to ask for confirmation, if it is C, it means asking for confirmation.

(7) Save and Exit commands

Command

Role

: W

Save changes

: w New_filename

Save as specified file

: Wq

Save changes and exit

Zz

Shortcut keys, save changes and exit

: q!

Do not save changes and exit

: wq!

Save changes and exit (the file owner and root can use)

2. Vim Usage Tips

(1) Import the contents of a file into a text file in the following format:

: R file Name

Example: Create the Test text, write the following, copy the contents of the/etc/issue to the test text

VI testi am ws. what's your name? My name is Limingi am glad.

Insert the contents of the issue in the next line of the last line, enter in command mode: R/etc/issue, you can get:

I am ws. what's your name? My name is Limingi am glad-See you!  6.5  (Final) Kernel \ r on a \m

(2) in the case of not exiting Vim, execute the corresponding operation command, in the form of:

:! command

Example: executing the LS command in command mode

I am glad to see you!  6.5  (Final) Kernel \ R on an \m:! ls  Write last change]anaconda-ks.cfg  install. Log   Install. Log.syslog

(3) The result of the import command execution is equivalent to synthesizing the first two , in the form of:

: R! command

Example: Add a date at the end of the text of test, and enter in command mode: R!date, the result is:

I am ws. what's your name? My name is Limingi am glad-See you!  6.5  (Final) Kernel \ r on an \mfri  Jan2:xx:55   -

(4) Define shortcut keys

You can define shortcut keys for commonly used commands in the following format:

Map Shortcut Trigger command

Note: the creation of shortcut keys requires that you press ctrl+v+ to create a shortcut letter

Example 1: Create a shortcut to add an annotation # at the beginning of the line, just enter in command mode: Map ^p i#<esc>, where ^p is ctrl+v+p at the same time, i# is the beginning of the insertion of the comment character #,<esc> Indicates exiting edit mode to command mode.

Then execute ctrl+p on the first line to get the following results:

#I am ws. what's your name? My name is Limingi am glad-See you!  6.5  (Final) Kernel \ r on an \mfri  Jan2:xx:55   -

Example 2: create a shortcut key to delete the beginning of the line comment #, enter in command mode: Map ^b 0x.

Example 3: Create a shortcut key for a mailbox under the current cursor and enter the following command in command mode:

: Map ^h [email protected]

(5) A continuous comment line, which is similar to the search for replace command 1. (6). If you add the comment # to multiple lines in a row, enter the following command in command mode:

: n1,n2s/^/#/g

where ^ indicates the beginning of the line

In contrast, canceling continuous line comments allows you to enter the following command in command mode:

: n1,n2s/^#//g

If a "//" is used to denote an annotation, a continuous comment line can enter the following command in command mode:

: n1,n2s/^/\/\//g

where "\" represents an escape character.

Example: Add//comment to the first two lines in the test text:

// I am ws. // What ' s your name? My name is Limingi am glad-See you!  6.5  (Final) Kernel \ r on an \mfri  Jan2:xx:55   -

(6) Replacement

In a text file, you can replace a string with another string, such as replacing mail with [email protected] with the following command format:

: AB Mail [email protected]

Then write to mail in the text, press the space or enter key to automatically replace mail with [email protected].

The shortcut keys or replacements that are usually set will disappear when the system restarts, and if you want to save the definition or replacement of shortcut keys for a long time, you can write the configuration file in each user's home directory (such as root in/root, user/home/user). VIMRC (Root as/ROOT/.VIMRC) , and then write the required command in edit mode in the configuration file, with no colon (:) at the beginning of the line.

Linux Learning Notes (10) Text editor vim

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.