Basic usage of VI in Linux (zt)

Source: Internet
Author: User

Guide to the basic usage of VI in Linux.

1. Introduction to basic commands

---- 1. cursor command

K, J, H, l -- move the cursor up, down, left, and right. Although you can use the four optical keys on the right of the keyboard in Linux, it is very useful to remember these four commands. These four keys are the basic locations where the right hand is placed on the keyboard.

Ng -- jump command. N indicates the number of rows. This command immediately redirects the cursor to the specified row.

CTRL + G -- number of rows and columns at the cursor position.

W, B -- let the cursor skip a word forward or backward.

---- 2. Edit the command

I, A, R -- insert character commands (I = insert, A = append, r = replace) before, after, and at the cursor ).

CW, DW -- change (replace)/command for deleting words where the cursor is located (C = change, D = delete ).

X, d $, and DD -- delete a character, delete all characters from the cursor's position to the end of the line, and delete the entire line.

---- 3. Search for commands

----/String ,? String -- the command used to search for the corresponding string from the cursor to the back or forward position.

---- 4. copy Copy command

---- YY, p -- command used to copy a row to the clipboard or retrieve the content in the clipboard.

Ii. FAQs and application skills

---- 1. Read the content in/etc/passwd in a new file and retrieve the username section.

---- VI File

----: R/etc/passwd read/etc/passwd at the cursor position in the opened file

----: % S/:. * // G delete all parts after the user name in/etc/passwd from the colon until the end of the line.

---- You can also read the file content after the specified row number. For example, you can use the command ": 3R/etc/passwd" to read all the content of/etc/passwd from line 1 of the new file.

---- You can also use the following method to delete all blank lines in the file and comment lines starting.

---- # Cat squid. conf. Default | grep-V '^ $' | grep-V '^ #'

---- 2. After opening a file and editing it, the logged-on user does not have the write permission for the file and cannot store the disk. You need to save the modifications to the temporary file.

---- VI File

----: W/tmp/1 save all the modifications you have made. You can also save some of the modifications to a temporary file. For example, you can only save 20th ~ Store the contents between 59 rows into a file/tmp/1. You can type the following command.

---- VI File

----: 20, 59 w/tmp/1

---- 3. Use VI to edit a file, but you need to delete the contents of a large segment.

---- Use the edit command "vifile" to open the file, move the cursor to the row to be deleted, press Ctrl + G to display the row number, and then press Ctrl + G to the end, displays the row number at the end of the file.

----: Assume that the number of rows obtained twice in 23,104 5d is 23 and 1045, all contents in this period will be deleted, you can also mark the start and end lines to be deleted with the MA and MB commands, and then use the ": 'a, 'bd" command to delete them.

---- 4. Add strings at the beginning or end of the row or several lines of the entire file.

---- VI File

----: 3, $ S/^/some string/Insert "some string" at the beginning of the line from the first row to the last row of the file ".

---- % S/$/some string/g Add "some string" at the end of each row of the entire file ".

---- % S/string1/string2/g Replace "string1" with "string2" in the entire file ".

----: 3rd S/string1/string2/Replace "string1" in rows 7th to of the file with "string2 ".

---- Note: S is substitute, % indicates all rows, and G indicates global.

---- 5. Edit two files at the same time, copy the text in one file and paste it into another file.

---- VI file1 file2

---- YY copies the row at the cursor of file 1

----: N switch to file 2 (n = NEXT)

---- P paste the copied row at the cursor of file 2

----: N switch back to file 1

---- 6. Replace the path in the file.

---- Use the command ": % s #/usr/bin # G" to replace all the paths/usr/bin in the file with/bin. You can also run the command ": % S/usr/bin // bin/g", where "" is an escape character, it indicates that the subsequent "/" character is a meaningful character, not a separator.

 

Bytes ---------------------------------------------------------------------------------------------------------

I. Introduction

VI is the most common full-screen editor in the Unix world. in Linux, VIM is the enhanced VI version. Vim is fully compatible with vi. VI is the abbreviation of "Visual Interface. It can perform output, delete, search, replace, block operations and many other text operations, and users can customize it according to their own needs. This is other editsProgramNone.

VI is not a typographical program. Unlike word or Wps, it can orchestrate fonts, formats, paragraphs, and other attributes. It is just a text editing program.

VI has no menu, only commands, and many commands. You can enter the VI editing environment by typing VI on the command line. VI has three statuses: Command mode, text input mode, and last line mode. Enter VI and press enter to enter the command mode of VI.

In command mode, any character entered by the user is interpreted and executed by VI as a command. If the user wants to treat the entered character as text content, switch the VI operating mode from command mode to text input mode. In command mode, press the "a" ("A") or "I" ("I") or "O" ("O") key, enter the inserted text input mode. Press the "A" key to insert to the end of the current cursor; press the "I" key to insert at the current cursor; press the "A" key to insert at the end of the row where the current cursor is located; press the "I" key to insert at the beginning of the row where the current cursor is located. In text input mode, Press ESC to return to command mode.

The last row mode is also called the ex escape mode. VI has a dedicated "escape" command to access many line-oriented ex commands. In command mode, you can press the ":" key to enter the last line mode. At this time, VI will display a ": as the last line prompt, waiting for the user to enter the command. Most file management commands are executed in this mode (for example, writing the content in the editing buffer to a file is moderate ). After the last-line command is executed, VI automatically returns to the command mode.

For example:

: 1, $ S/A/G

Replace uppercase A with lowercase A from the first line to the end of the file.

If you want to switch to the command mode when you enter the command in the last line mode, press the button or use the return key to delete all input commands, and then press the return key, the VI can be returned to the command mode.

To switch from command mode to edit mode, TYPE command a or I. To return from text mode, Press ESC. In command mode, enter ":" to switch to the last line mode, and then enter the command.

Many Commands in VI use numeric values such as row number and row number. If the edited file is large, it is inconvenient to calculate it by yourself. Therefore, VI provides the function of adding row numbers to texts. These row numbers are displayed on the left of the screen, and the content of the corresponding row is displayed after the row number. It should be noted that the row numbers added here are only displayed to users, and they are not part of the file content.

When you use VI to create a new file, you can leave no file name in the VI command. After editing the file, you must specify the file name to save the data.

When entering Vi, you can not only specify a file name to be edited, but also have many additional operations. If you want to move the cursor to a specific row in the file after entering VI, you can add the option + N after Vi, where N is the specified number of rows. For example, after you enter the command vi + 3 example.txt, the optical mark will be placed on line 3rd in example.txt.

Ii. Common VI commands

1. Insert command

VI provides two insert commands: I and I.

(1) I command

The inserted text starts before the cursor position, and you can use the key to delete incorrect input during the insertion process. At this time, the VI is in the Insert state, and the "-- insert --" (insert) is displayed at the bottom of the screen.

Example 1: A file being edited is as follows:

Welcome to VI world! Come on!

~

~

The cursor is located at the first "!" To insert the following information to the front:

This is an example!

Run the I command and enter the corresponding text. The screen is displayed as follows:

Welcome to VI world this is an example !! Come on!

~

~

In this example, we can see that the cursor is originally in the first "!" But it is inserted before the cursor position, so this "!" It is squeezed into the new inserted text.

(2) I command

This command is to move the cursor to the beginning of the current line, and then insert text before it.

2. move the cursor on the screen

In the full-screen text editor, moving the cursor is undoubtedly the most frequently used operation. Users can quickly and accurately edit the cursor at the desired position only by skillfully using these commands.

The cursor movement in VI can be in command mode or text input mode, but the operation methods are different.

(1) In text input mode, you can use the four arrow keys on the keyboard to move the cursor.

(2) In command mode, there are many ways to move the cursor. Not only can we use four direction keys to move the cursor, but we can also use the H, J, K, and l keys to move the cursor instead, this can avoid conflicts caused by different keyboard definitions on different machines, and you can complete all operations without leaving the letter and keyboard location after you are proficient, thus improving work efficiency.

3. Move on the screen

VI provides three commands for moving the cursor across the screen without scrolling the file itself. They are h, M, and l commands.

(1) h command

This command moves the cursor to the first line of the screen (that is, the upper left corner), that is, the first line of the current screen, rather than the first line of the entire file. You can use this command to quickly move the cursor to the top of the screen. If n is added before the H command, move the cursor to the beginning of the N line. It is worth mentioning that the command DH will delete all content from the current row of the cursor to the first line of the displayed screen.

(2) m command

This command moves the cursor to the beginning of the line in the middle of the display file. That is, if the current screen is full, it will be moved to the middle line of the screen; if it is not full, it will be moved to the middle line of the lines of the text. This command can be used to quickly move the cursor from any position on the screen to the beginning of the line in the middle of the screen display file. For example, when the above screen is displayed (no matter where the cursor is on the screen), in command mode, after the command m is input, the optical mark will be moved to the letter A in the Add line. It is also worth mentioning that using the command DM will delete all content from the current row of the cursor to the middle row of the displayed file on the screen.

(3) l command

When the content displayed in the file exceeds one screen, this command moves the cursor to the beginning of the line at the bottom of the screen. When the content displayed in the file is insufficient, this command moves the cursor to the beginning of the last line of the file. This command can be used to quickly and accurately move the cursor to the bottom of the screen or the last line of the file. If n is added before the l command, move the cursor to the beginning of the N line starting from the bottom of the screen. It is also worth mentioning that the command DL will delete all content from the current row of the cursor to the bottom line of the screen.

4. Edit commands

Let's take a look at the simplest and most common editing process: No one makes mistakes. So sooner or later you will encounter the modification of the text you entered. In fact, most of the time spent on word processing is modification rather than entering new things. Therefore, it is important to know how to easily modify it.

(1) x -- delete a character

The simplest way to delete text is to use X. The result of this command is that the character of the cursor disappears and the text behind it moves left. If the character you delete is the last character of a line, the cursor will move to the left so that the cursor will not stay below the nonexistent character. If there is no text, the speaker is called.

(2) d -- delete an object

The right side of this command also has certain text objects. A text object is a piece of text. On the right side is the characters that control the movement of the cursor. For example, if W indicates the previous word, DW deletes the next word. 5 W indicates the forward five words, then the D5W will delete them.

(3) dd -- delete a row

One of the most common D-series commands. As before, 5dd will delete 5 rows

(4) d -- entire Deletion

The uppercase D is used to delete the cursor from the cursor to the end of the row. Same effect as d $.

(5) U -- Recovery

Regret it? It does not only undo the deletion, but also undo all your edits.

(6). -- duplicate

Repeat the edits.

5. Exit vi

You can use one of the following methods to exit VI and return to shell after editing the file.

(1) In command mode, the uppercase letter Z is used for two times. If the current edited file has been modified, VI will save the file and exit, and return to shell; if the current edited file has not been modified, VI exits directly and returns to shell.

(2) In the last line mode, run the following command: w vi to save the current edited file, but does not exit. Instead, it continues waiting for the user to enter the command. When you use the W command, you can create a new file name for the editing file.

Example: W newfile

At this time, VI will save the content of the current file to the specified newfile, while the original file remains unchanged. If newfile is an existing file, the VI prompts in the status line of the display window:

File exists (use! To override)

If you really want to replace the original content in newfile with the current content of the file, you can use the command

: W! Newfile

Otherwise, you can select another file name to save the current file.

(3) In the last line mode, enter the command: Q. Exit VI and return to shell. If the editing file is not saved when you exit VI with this command, the last line of the VI display window displays the following information:

No write since last change (use! To overrides)

The user is prompted that the file is not saved after modification, and then VI does not exit, continue waiting for the USER command. If you want to force exit VI without saving the modified file, run the following command: Q! VI abandons the modification and directly returns it to shell.

(4) In the last line mode, enter the command: wq vi will save the file first, and then exit VI and return to shell.

(5) In the last line mode, the input command has the same function as the ZZ command in the command mode.

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.