Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use vi Editor in CentOS

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how to use the vi editor in CentOS. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

1. The basic concept of vi

Basically, vi can be divided into three states: command mode (command mode), insert mode (Insert mode) and bottom line mode (last line mode). The functions of each mode are as follows:

1) Command line mode command mode)

Control the movement of the screen cursor, the deletion of characters, words or lines, move and copy a section and enter the Insert mode, or to the last line mode.

2) insert mode (Insert mode)

Text input can only be done under Insert mode, and press the "ESC" key to return to command line mode.

3) bottom line mode (last line mode)

To save or exit vi, you can also set the editing environment, such as finding strings, listing line numbers, etc. Wait.

However, when we use vi, we usually simplify it into two modes, that is, the last line mode mode is also included in the command line mode command mode).

2. Basic operation of vi

A) enter vi

After the system prompts for the symbol vi and the file name, go to the vi full screen editing screen:

$vi myfile

It is important to note, however, that after you enter vi, you are in "command line mode (command mode)", and you have to switch to "insert mode (Insert mode)" before you can enter text. People who use vi for the first time will want to use the upper and lower keys to move the cursor first, and as a result, the computer beeps all the time, making themselves angry half to death, so after entering the vi, don't move, switch to "insert mode (Insert mode)".

B) switch to insert mode (Insert mode) to edit the file

Press the letter "I" in "command line mode (command mode)" to enter "insert mode (Insert mode)" and you can start typing text.

C) switching of Insert

You are currently in Insert mode mode, so you can only type text all the time if you find that you have typed the wrong word! To use the cursor key to move back and delete the word, press the "ESC" key to go to "command line mode (command mode)" before deleting the text.

D) exit vi and save the file

Under Command Line Mode (command mode), click the: colon key to enter Last line mode, for example:

W filename (enter "w filename" to save the article with the specified file name filename)

: wq (enter "wq", save and exit vi)

: q! (enter qquit, do not save the disk and force to exit vi)

3. Command line mode (command mode) function key

1)。 Insert mode

Press "I" to switch to insert mode "insert mode". Press "I" to enter the file from the current position of the cursor after entering insert mode

After pressing "a" to enter insert mode, the text is entered from the next position where the current cursor is located.

When you press "o" to enter insert mode, insert a new line and enter text at the beginning of the line.

2)。 Switch from insert mode to command line mode

Press the ESC key.

3)。 Move the cursor

Vi can directly use the cursor on the keyboard to move up and down, but the regular vi uses lowercase letters "h", "j", "k" and "l" to control the cursor to move left, down, up and right respectively.

Press "ctrl" + "b": move the screen one page back.

Press "ctrl" + "f": the screen moves one page forward.

Press "ctrl" + "u": move the screen to "back" half a page.

Press "ctrl" + "d": move the screen half a page to the front.

Press the number "0": move to the beginning of the article.

Press "G": move to the end of the article.

Press "$": move to the end of the line where the cursor is located.

Press "^": move to the beginning of the line where the cursor is located

Press "w": the cursor jumps to the beginning of the next word

Press "e": the cursor jumps to the suffix of the next word

Press "b": the cursor goes back to the beginning of the previous word

Press "# l": move the cursor to the # th position on the line, for example: 5lMagne56l

4)。 Delete text

"x": each time you press, delete the "after" character of the cursor location.

"# x": for example, "6x" means to delete "after" 6 characters where the cursor is located.

"X": uppercase X. each time you press it, one character in front of the cursor is deleted.

"# X": for example, "20X" means to delete the "first" 20 characters where the cursor is located.

"dd": delete the line of the cursor.

"# dd": delete # lines from the line where the cursor is located

5)。 Copy

"yw": copies the character where the cursor is located to the suffix into the buffer.

"# yw": copy # words to the buffer

"yy": copies the line of the cursor to the buffer.

"# yy": for example, "6yy" means to copy "count down" six lines of text from the line where the cursor is located.

"p": paste the characters in the buffer to the position of the cursor. Note: all copy commands related to "y" must work with "p" to complete the copy and paste function.

6)。 Replace

"r": replace the character where the cursor is located.

"R": replaces the character where the cursor goes until the "ESC" key is pressed.

7)。 Reply to the last operation

"u": if you execute a command by mistake, you can press "u" immediately to go back to the previous operation. Press "u" multiple times to perform multiple replies.

8)。 Change

"cw": change the word where the cursor is located to the suffix

"c3w": for example, "c3w" means to change 3 words

9)。 Skip to the specified line

"ctrl" + "g" lists the line number of the cursor.

"# G": for example, "15G" means to move the cursor to the beginning of line 15 of the article.

4. Brief introduction of commands under Last line mode

Before using "last line mode", remember to press the "ESC" key to make sure you are under "command mode", and then press the ":" colon to enter "last line mode".

A) list line number

"set nu": after entering "set nu", the line number is listed before each line in the file.

B) skip to a line in the file

The "#" sign represents a number. Enter a number after the colon and press enter to jump to the line. If you enter the number 15 and enter again, you will jump to line 15 of the article.

C) find characters

"/ keyword": first press the "/" key, and then enter the character you are looking for. If the keyword you are looking for for the first time is not what you want, you can press "n" until you find the keyword you are looking for.

"? Keyword ": press the"? "key, and then enter the character you are looking for. If the keyword you are looking for for the first time is not what you want, you can press" n "until you find the keyword you are looking for.

D) Save the file

"w": type the letter "w" in the colon to save the file.

E) leave vi

"Q": press "Q" to quit. If you can't leave vi, you can force you to leave vi after "Q".

"qw": it is generally recommended to use it with "w" when you leave, so that you can save the file when you exit.

5. Vi command list

1. The following table lists the functions of some keys in command mode:

H

Move the cursor one character to the left

L

Move the cursor one character to the right

K

Move the cursor up one line

J

Move the cursor down one line

^

Move the cursor to the beginning of the line

0

The number "0", move the cursor to the beginning of the article

G

Move the cursor to the end of the article

$

Move the cursor to the end of the line

Ctrl+f

Flip the screen forward

Ctrl+b

Flip the screen backward

Ctrl+d

Turn half the screen forward

Ctrl+u

Turn back half the screen

I

Insert a character before the cursor position

A

The last character at the position of the cursor begins to increase.

O

Insert a new line and type at the beginning of the line

ESC

Retreat from input state to command state

X

Delete the characters after the cursor

# x

# characters after the cursor is deleted

X

(capital X), delete the characters in front of the cursor

# X

Delete # characters in front of the cursor

Dd

Delete the line where the cursor is located

# dd

Delete # lines from the number of lines in which the cursor is located

Yw

Copy a word at the location of the cursor

# yw

Copy # words of the location of the cursor

Yy

Copy the line where the cursor is located

# yy

Copy # lines from the number of lines where the cursor is located

P

Paste

U

Cancel the operation

Cw

One word that changes the position of the cursor

# cw

Change the position of the cursor in # words

2. The following table lists some instructions in the line command mode.

W filename

Save the file being edited as filename

Wq filename

Save the file you are editing as filename and exit vi

Q!

Discard all changes and exit vi

Set nu

Show line number

/ or?

Find, enter what you are looking for after /

N

And / or? Use together, if the search is not the keyword you are looking for, press n or backward (in conjunction with / with) or forward (with? Keep searching until you find it.

For using vi for the first time, here are a few caveats:

1. After opening the file with vi, you are in "command line mode (command mode)", and you have to switch to "insert mode (Insert mode)" before you can enter text. Switching method: press the letter "I" in "command line mode (command mode)" to enter "insert mode (Insert mode)", and you can start typing text.

2, after editing, you need to switch from insert mode to command line mode to save the file, the switching method: press the "ESC" key.

3. Save and exit the file: type: wq in command mode! (don't forget the one in front of wq:)

This is how to use the vi editor in the CentOS shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report