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 are the vi commands?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces what the vi command has, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

A complete Collection of vi commands

Windows network managers have long used Edit, notepad, or Wordpad to edit text and handwritten files. For UNIX operating systems, most UNIX systems have their own types of editors, but the actual tools used are different, depending on the UNIX system used. Currently, some editors have even become strong competitors to Microsoft Word, such as Sun Microsystems. However, vi visual editors are installed on all UNIX operating system machines. Once you have mastered the skills of using vi, you are free to edit files in all UNIX operating systems, including Linux.

What is Vi?

At first glance, vi has nothing to praise but full-screen viewing. However, she is a very powerful editor and can satisfy any operation you need. Vi allows you to edit and browse the entire file during operation. Many previous editors, such as ed, were line editors, that is, only one line of text could be edited and browsed at a time. Therefore, compared to the past UNIX editor, vi is more convenient to browse the entire file.

Many UNIX operating systems centralize several different command-line functions, such as file processing tools such as nroff and troff. Like Microsoft's Word, these command-line functions can edit files in different formats and styles. I personally used these tools to write my graduation thesis, which is very convenient.

You don't need a special installation path to install vi on your machine, so you don't have to worry too much about it. Vi automatically appears when the system is installed. Because the system administrator needs to use vi to handle a lot of work, including creating system text, and so on. Redhat's Linux provides the option to install specific editors on the system, but it is rare for UNIX operating systems not to install vi

Use and operation of Vi

Before introducing how to use vi, it is important to understand that vi has two basic modes. The two basic modes are input mode and instruction mode. If the input mode is to enter the command, the text will be displayed on the screen, while the instruction mode will execute the command instead of displaying the text. The easiest way to understand the instruction pattern is to imagine what happens when you cut, copy, and paste data.

By default, vi starts in command mode, so text is not displayed when you enter a command unless you enter a command to switch to input mode. Most vi commands don't show anything on the screen, you just need to know how they work. Use the [Esc] key to switch between input mode and instruction mode. If you are already in instruction mode, using [Esc] will tell vi that you have entered the command and are about to execute it. If you do not enter any commands, pressing the [Esc] key in instruction mode will interrupt the operation.

Now that I have introduced the basic operation of vi, you can try it out. First, open a terminal window on your workstation. Simply type vi in the $prompt and press enter to launch the vi editor.

Next, you will see a pop-up blank file with a symbol in the lower left corner of the page. This symbol is not easy to see, but the symbols you see indicate that they are at the bottom of the file. When the file is saved, these symbols are not displayed. To edit a file or name a blank file, you can type vi filename. Vi filename can be an existing file name or a name assigned to a new file.

Universal input mode

Once you have entered the editing document, there are many other ways to get vi into input mode besides using the [Esc] switching method. Many other methods involve using a vi instruction. These instructions immediately put vi into input mode, and the characters entered immediately appear as text in the document. The commonly used instructions are:

I this is an input command, that is, the text is inserted in front of the cursor, and the text behind the cursor moves with the new text. If it is a blank document, the text will be entered in the upper-left corner of the document.

I input mode at the beginning of the line command, that is, enter text at the beginning of the current line. Similar to entering text after pressing the key in Word.

A this is a new command that adds new text from behind the cursor, and the data behind the cursor moves backward with the new text.

A this is an operation that adds new text to the line command, that is, starting at the end of the line where the cursor is located. This is similar to entering text after pressing the key in Word.

O this is the open line command under a command, that is, add a new line below the line of the cursor and enter input mode.

O this is the open command above a command, that is, add a new line above the line of the cursor and enter input mode.

Use any of the above commands after editing, and you will return to instruction mode.

Cursor movement instruction

The next thing you need to learn is the browse command in vi. To move through a document in instruction mode, you can use the following command:

H: move a text to the left

J: move one text down

K: move one text up

L: move a text to the right

These mobile commands may seem out of date in the mouse and GUI environment, but they are widely used in all vi versions. In many cases, the cursor, upper cursor, lower cursor, left cursor, and right cursor will all play a role, but the operator should include the termcap and terminfo attributes in these commands.

The following other move instructions can also be very helpful:

[Ctrl] f moves down one page of the instruction. This causes the cursor to move down one page in the text.

[Ctrl] b move the instruction back one page. This causes the cursor to move back one page in the text.

[Ctrl] u moves down half a page of instructions. This causes the cursor to move down half a page in the text.

At the beginning of the ^ line command. The cursor moves to the beginning of the line.

At the end of the line command. The cursor moves to the end of the line.

W moves to the beginning of the previous text.

B move to the beginning of the next text.

The end instruction of the e text.

G moves to the end of the file.

General function

In a general text editor, all functions, such as delete, change, cut, copy, paste, and search / replace, can be found in vi. Many instructions can put the editing state into input mode and can be terminated using [Esc]. Commonly used editing instructions include:

X: delete character command, that is, delete the character where the cursor is located.

Dw: delete text command, that is, delete the text at the cursor.

Dd: delete the line command, that is, delete the line where the cursor is located.

D: delete to the end of the line, that is, delete all text from the position of the cursor to the end of the line.

Cw: change the text command, that is, the text at all positions of the cursor will change as you enter the text, until you press [Esc].

R: substitute character command, that is, the character where the cursor is located will be replaced by the character entered until [Esc] is pressed.

R: replace the multiline character command, that is, all characters from the cursor position to the right will be replaced by the entered characters until [Esc] is pressed.

Most of these instructions can be combined to improve their functionality.

For example, if you type 2ddJiavi, two lines of text will be deleted.

Cut and paste and other operations

Like many other editors, you can use cut and paste text in a document. The following command is the simplest cut and paste operation that can be used.

Yy copies line commands. The text of all lines of the cursor will be copied to a buffer similar to the clipboard. Notice that you can use multiple copy commands to change this command to cut multiline text to the buffer.

Yl copy character command. The characters of all lines of the cursor will be copied to a buffer similar to the clipboard. You can use multiple copy commands to change this command to cut multiple lines of characters to the buffer.

Place the command after p. Places or pastes the contents of the buffer after the current cursor position. For the copy line command, the contents of the buffer are pasted after the current line. For the copy character line command, the buffer contents are pasted to the right of the cursor.

Place the command before P. Places or pastes the contents of the buffer before the current cursor position. For the copy line command, the contents of the buffer are pasted before the current line. For the copy character line command, the buffer contents are pasted to the left of the cursor.

When using copy and placement instructions, you can also use multiple buffers or clipboards for character instructions. These characters can be amerz and can only be lowercase letters. For example, ayy copies the contents of the current line to the buffer A _ focus AP command to paste the contents of the buffer before the current line.

The most important command in Vi is the undo instruction. The command is simple: just press u. Press the u key to undo the last instruction used. Similar to the undo command is the redo command. Press the full stop key. The most recent instruction will be retyped. For example, if you use cw to change a text, you can find the text and use a full stop. Come to this word.

The other two important commands are the search command. To look forward in the document, type / text,text for what you are looking for. It is important to note that you can find it only if the input matches exactly. Vi does not support mixed or fuzzy queries. For backward lookups, type? text,text is what you are looking for.

Edit instruction

In instruction mode, many groups of commands can be prefixed with:. Unlike the commands mentioned above, once you enter a colon, all instructions appear in the lower-left corner of the screen and can be edited before execution. The following are commonly used and important: instructions. W write file command. If you type: W and enter, the current file will be written to the hard drive. If you are using a blank document, the message "No current file name" will appear. You can write a blank document or the current document to another file using the: W file name.

: w! Or: W file name overrides the file command. If you enter: W! Then enter and the file will be overwritten by the currently newly entered name. This can be used to overwrite files.

: Q exit the command. If held normally, entering: Q will terminate the entire operation.

: Q! exit without keeping the command. Exit without saving the file.

:% s/find_text/replace_text/

Query and alternate command

As mentioned above, you can change the query command to a query and an alternative command. Use the:% s command to tell vi to query all lines in the document. The / find_text command is similar to the above query command, where the first line of the result of the / find_text operation is replaced when it is suffixed with / replace_text/,. To replace all the contents of / find_text, you can add a g (global), such as:% s/find_text/replace_text/g.

You should not only use a single instruction. You can use multiple instructions in combination. For example,: wq! The file will be saved to the hard drive and vi will be exited to return to the instruction state. Vi= contains all of the above mentioned (Very involved) is only a small part of the function of vi. You can execute the instruction string together, copy and paste the text in tagged form, or use the sed instruction. You can also insert shell script output or other instructions into your document. The examples and instructions mentioned above will take you through the vi door. Many vi users remember some commands when they operate. You can learn more vi instructions from other people who use vi, and you may marvel at the power of vi. However, you should remember one thing: use the: an instruction frequently, because vi does not provide automatic background document saving.

Thank you for reading this article carefully. I hope the article "what are the vi commands" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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

Development

Wechat

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

12
Report