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

What is the vi command in linux

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

Share

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

This article will explain in detail what the vi command in linux is. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Detailed explanation of linux vi command

The vi editor is the standard editor for all Unix and Linux systems. It is as powerful as any of the latest text editors. Here are some of its usage and instructions.

Since the vi editor is exactly the same for any version of Unix and Linux systems, you can learn more about it anywhere else that introduces vi. Vi is also the most basic text editor in Linux, and after learning it, you will walk freely in the world of Linux.

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 qresume, do not save and force vi to exit)

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 in which 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": the cursor moves to the # th position of the line, such as: 5lPower56l.

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": copy 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)。 Resume 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 at the cursor to the suffix "C3W": for example, "C3W" means to change three 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 cursor left one character l move cursor right one character k cursor up one line j cursor move down one line ^ cursor moves to the first 0 of the line the number "0" The cursor moves to the beginning of the article G the cursor moves to the last $cursor of the article moves to the end of the line Ctrl+f flips the screen forward Ctrl+b flips back half the screen Ctrl+d turns half the screen backward I insert the character an in front of the cursor position the last character of the cursor begins to add o insert a new line Enter ESC from the beginning of the line from the input state to the command state x delete the character behind the cursor # x # character X (uppercase X) after deleting the cursor Delete the character in front of the cursor # X Delete the # character in front of the cursor dd delete the line where the cursor is located # dd delete from the number of lines where the cursor is located one word at the location of the yw copy cursor # yw copy the cursor at the location of the yy copy cursor at the location of the line # yy copy from the number of lines for the cursor p paste u cancel the operation cw change the position of the cursor Cw changes the position of the cursor in # words

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

W filename saves the file being edited for filenamewq filename saves the file being edited as filename, and exits viq! Discard all changes and exit viset nu to display line numbers / 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:)

So much for sharing what the vi command is in linux. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

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