VI editor usage

Source: Internet
Author: User
Tags add numbers

VI editor usage

Keywords: VI, VI editor,

1. About the text editor

There are many text editors, such as gedit, kwrite, OpenOffice ......, in text mode, the editors include VI, VIM (enhanced version of VI), and nano ...... VI and Vim are the most commonly used editors in Linux. It is necessary to introduce the simplest usage of VI (Vim) so that entry-level Linux users can learn to use it in the shortest time.

The nano tool is similar to the edit operation in the DOS operating system. It is easy to use and we will not introduce it. If you are interested, try it;

2. VI Editor

Why should I learn how to simply apply Vi?

VI or Vim is the most basic text editing tool in Linux. Although VI or Vim does not have the simple operations like graphical interface editor, VI editor is in system management and server management, it is never comparable to the graphic editor. If the X-Windows desktop environment is not installed or the desktop environment crashes, we still need the editor VI in character mode; VI or Vim editor is the most efficient tool for creating and editing simple documents;

3. How to Use the VI Editor

3.1 How to call VI;

[Root @ localhost ~] # Vi filename

3 command modes of 3.2 vi

Command mode, used to input commands;

Insert mode, used to insert text;

Visual mode: used to highlight the video and select the body;

3.3 save and exit an object

Command mode is the default mode of VI or Vim. If we are in another command mode, we need to switch over through the ESC key.

When we press the ESC key and then enter the number, VI will wait for us to enter the command at the bottom of the screen;

: W save;

: W filename is saved as filename;

: WQ! Save and exit;

: WQ! Filename Note: Save the file name as filename and exit;

: Q! Do not save and exit;

: X should be saved and exited. Function and: WQ! Same

3.4 move the cursor

When we Press ESC to Enter command mode, we can use the following key bits to move the cursor;

J. Move a row down;

K. Move a row up;

H: move one character to the left;

L move one character to the right;

Ctrlb moves one screen up;

Ctrlf move one screen down;

Move up arrow up;

The downward arrow moves down;

Move to the left arrow;

The right arrow moves to the right;

When editing a file, you can add numbers before the J, K, L, and H keys, such as 3j, to move three lines down.

3.5 insert mode (text insertion)

I insert before the cursor;

A is inserted after the cursor;

I insert at the beginning of the row where the cursor is located;

A is inserted at the end of the row where the cursor is located;

O insert a row above the row where the cursor is located;

O insert a row under the row where the cursor is located;

S. delete a character after the cursor and enter the insert mode;

S. Delete the row where the cursor is located and enter the insert mode;

3.6 delete text content

X is a character;

# X delete several characters, # indicates a number, for example, 3x;

DW deletes a word;

# DW deletes several words. # It is represented by numbers. For example, 3dw deletes three words;

Dd deletes a row;

# Dd deletes multiple rows and # represents numbers. For example, 3DD deletes the cursor row and the cursor's next two rows;

D $ Delete the content from the cursor to the end of the line;

J. Clear the space between the row where the cursor is located and the previous line, and link the cursor row with the previous line;

3.7 restore modification and delete operations

U undo the modification or deletion operation;

Press ESC to return to command mode, and then press u to cancel the previous deletion or modification. If you want to cancel multiple previous modification or deletion operations, press the number of times U. This is not much different from the word Undo operation;

3.8 visual mode

In the latest Linux release version, VI provides the visual mode, which is only available in vim. If the VI you use does not have this function, replace it with vim. Enable visual mode, Press ESC, and then press V to enter visual mode;

The visual mode provides us with an extremely friendly selection of text ranges for highlighted display; at the very bottom of the screen, there are;

-- Visualized --

Or

-- Visual --

In the visual mode, we can use the cursor movement command in the command line mode mentioned above to select the text range.

What is the purpose of selecting a text range?

We can delete a job and press the d key to delete the selected content.

After the selected content is selected, Press Y to copy the content; press d to delete the content;

It is worth mentioning that deleting also means copying. We return to the command mode, move the cursor to a certain position, and press the shiftp key to paste the deleted content. Let's start with a sentence here. In the next article, we have to talk about it in detail.

Exit the visual mode, or use the ESC key;

3.9 copying and pasting

In fact, deletion also involves cutting. When we delete the text, we can move the cursor somewhere, and then press the shiftp key to paste the content to the original place, and then move the cursor somewhere, then press P or shiftp and paste it again;

P paste the post after the cursor;

Shiftp paste the post before the cursor

For example:

For example, if we want to copy the third line of a document and post it to the end of the fifth line, what should we do?

There are two methods;

Method 1:

First Delete the third line, move the cursor to the third line, then use the DD action, and then press the shiftp key. In this way, the third line of the deleted post is located in the original place.

Then we move the cursor to the fifth line with the K key, and then press the P key to post the content of the third line to the end of the fifth line;

Method 2:

Enter the visual mode, Press ESC, and then press v. Move the mouse pointer, select the content of the third line, and press y to copy; move the pointer to the fifth line, and then press p;

Therefore, the copy and paste operations are the combined use of command mode, insert mode, and visual mode. We need to learn how to switch between various modes, and usually use the ESC key; more importantly, move the cursor in command mode;

3.10 about the row number

Sometimes, when we configure a program to run, an error occurs in Line X of the configuration file. At this time, we need to use row number-related operations;

Add row numbers for all content;

Press ESC and enter:

: Set number

Cursor Position

In the lower right corner of the screen, there are similar;

57,8 27%

In this example, 57 represents 57th rows, and 8 represents 8th characters;

3.11 search and replace functions

3.11.1 search

First, we need to enter the ESC key to enter the command mode. We enter/or? The search mode is displayed;

/Search Note: Forward search, press the n key to move the cursor to the next qualified place;

? Search note: In reverse search, press the shiftn key to move the cursor to the next qualified

For example, if I want to find the swap word in a file, I should do the following;

First Press ESC to Enter command mode, and then enter;

/Swap

Or

? Swap

3.11.2 replacement

Press ESC to enter the command mode;

: S/search/replace/G note: Replace the search words in the row where the current cursor is located with replace and highlight all the search words;

: % S/search/replace Note: replace all search files with replace;

: #,# S/search/replace/G note: # represents a number, indicates the number of rows to the number, and replaces search with replace;

Note: In this case, G indicates global search. We noticed that the search will be highlighted even if there is no replacement;

Example:

For example, we have a document to modify;

We replace all words with the row where the cursor is located, which should be:

: S/The/g

We replace all the "the" in the entire document with the following:

: % S/

We just replace the, in rows 1st to 10th with the, which should be;

: 1, 10 s/The/g

 

1.1.1 editor vi

In Linux, code is usually written using the VI Editor, just like writing a document in notepad in windows, but the suffix of the code file is. C or. CPP, etc. In the future work, we will use the VI editor for daily programming and file editing. Therefore, the proficiency in VI usage directly determines your work efficiency.

There are a lot of information on how to use VI on the Internet. You can download it back or learn the help manual that comes with Linux. No matter how you study, the following points must be mastered and used skillfully:

I,VIThe differences between the three working modes and switching between them

The VI working mode is:

1. editing mode

After starting (opening a file), VI first enters the editing mode. In this mode, you can move the cursor, delete text, copy or paste text, and other operations;

VI Syntax: VI <File Name>;

2. insert mode

When you enter commands such as I, A, and O in editing mode and enter the insert mode, any character except ESC will be considered as a character inserted into the editing buffer, press ESC and switch to the editing mode;

3. Last line mode

Enter ":" In editing mode and enter the last line mode, in this mode, you can enter a command to save the file, read the file content, execute shell commands, set VI parameters, search for strings in a regular expression, or replace strings;

II,Edit mode command operation

1. move the cursor

To modify the body content, you must first move the cursor to the specified position:

K, J, H, and l functions are equivalent to the upper, lower, left, and right arrow keys.

CTRL + B Move up a page in the file (equivalent to the Pageup key)

CTRL + F move down a page in the file (equivalent to the Pagedown key)

H move the cursor to the top of the screen (highest)

NH moves the cursor to line N of the screen

2 h move the cursor to the second line of the screen

M move the cursor to the middle of the screen (middle)

L move the cursor down the screen (lowest)

NL moves the cursor to the nth line of the screen

3l move the cursor to the last 3rd rows of the screen

W shifts the cursor right in the specified row to the beginning of the next word.

E move the cursor right in the specified row to the end of a word

B. move the cursor left in the specified row to the beginning of the previous word.

0: 0. move the cursor left to the beginning of the line.

$ Move the cursor right to the end of the row

^ Move the cursor to the first non-empty character in the row

2. Replace and delete

Replace the character pointed to by the cursor with other characters, or delete one or more characters from the current cursor position:

RC uses C to replace the current character pointed to by the cursor

NRC uses C to replace the first n characters pointed to by the cursor

5rc replaces the first five characters pointed to by the cursor with C

X deletes the current character pointed to by the cursor

NX deletes the first n characters pointed to by the cursor

3X Delete the first 3 characters pointed to by the cursor

DW Delete the word on the right of the cursor

Ndw deletes n words on the right of the cursor

3 DWS Delete the three words on the right of the cursor

DB deletes words on the left of the cursor

NDB deletes n words on the left of the cursor

Delete 5 words on the left of the cursor in 5db

Dd deletes the row where the cursor is located and removes gaps

NDD deletes n rows of content and removes gaps

3 dd delete 3 rows and remove gaps

3. paste and copy

The content (such as characters, words, or rows) deleted from the body is not really lost, but is cut and copied to a memory buffer. You can paste it to the specified position in the body:

P: lowercase letter P. paste the content of the buffer to the end of the cursor.

P capital letter P, paste the content of the buffer to the front of the cursor

YY copies the current row to the memory buffer

Nyy copies n lines of content to the memory buffer.

Copy 5 lines of content to the memory buffer in 5yy

4. Search for strings

To find the location where a specified word or phrase appears in a file, you can use VI to search directly, instead of manually. The search method is: Enter the character/, followed by the string to be searched, and then press Enter. The editor executes a forward search (toward the end of the file), finds the specified string, stops the cursor at the beginning of the string, and type N to continue the search, locate the next occurrence of the string. Use characters? Replace/to implement reverse search (beginning with the object)

5. Revocation and repetition

When editing a document, you can use the undo command to eliminate the consequences of an incorrect editing command. In addition, if you want to repeat the preceding edit command at the new cursor position, you can run the following command again:

U undo the result of the previous command

. Repeat the last command to modify the body.

6. Select text

Vi can enter a visual mode. In this mode, you can use the cursor to move the command to select text visually and then perform other editing operations, such as deleting and copying the text:

V character selection command

Command selected by line V

 

III,Insert mode command operation

After you locate the cursor correctly in edit mode, you can switch to insert mode with the following command:

I enter the body on the left of the cursor

A. Enter the text on the right of the cursor.

O Add a new line to the next row of the cursor

O Add a new line to the row where the cursor is located

I enter the body at the beginning of the row where the cursor is located

A. Enter the body at the end of the row where the cursor is located.

The preceding describes several simple methods to switch to the insert mode. There are also some commands that allow you to delete a piece of text before entering the insert mode to replace the text. These commands include:

S. Replace the character pointed to by the cursor with the input body.

NS replaces n characters on the right of the cursor with the input body

CW replaces the word on the right of the cursor with the input body

NCW replaces n words on the right of the cursor with the input body

CB replaces the word on the left of the cursor with the input body

NCB replaces n words on the left of the cursor with the input body

CD replaces the row of the cursor with the input body

NCD replaces n rows under the cursor with the input body

C $ replace all characters starting from the cursor to the end of the line with the input body

C0 replaces all characters starting from the beginning of the line with the input body with the cursor

IV,Last line mode command operation

In edit mode, type ":". The cursor jumps to the last line of the screen and a colon is displayed. The user input is displayed in the last line of the screen. Press enter to run the command VI;

1. Exit the command

ZZ can exit the VI editing program in editing mode. This command saves modifications to the body and overwrites the original file;

: Q exit without modification

: Q! Discard all modifications and exit the editing program

: WQ writes the edited content to the original file and exits the editing program (equivalent to the ZZ command)

2. line number and file

In command mode, you can specify the range of line numbers for command operations. The value is used to specify the absolute line number. The character ". "indicates the line number of the row where the cursor is located;" $ "indicates the line number of the last line of the body; a simple expression, such as". + 5 "indicates the second row down from the current row;

Each line in the editing contains its own line number. You can use the following command to move the cursor to the specified line:

: N move the cursor to line N

: W writes the edited content to the original file to save the intermediate editing result.

: W file writes the edited content to the file, keeping the content of the original file unchanged.

: A, BW file writes the content from row A to row B to the file File

: R file reads the content of the file and inserts the content behind the row where the current cursor is located.

: E file: edit the new file to replace the original content.

: F file: Rename the current file to file

: F print the name and status of the current file, such as the number of lines of the file and the number of lines where the cursor is located.

: 345 million file write 345th rows into the file File

: 3rd W file: Write lines 5th to file files

: 1,. W file writes row 1st to the current row into the file File

:., $ W file: Write the current row to the last row to the file File

:... + 5 W file: write 6 lines of content into the file from the current row

: 1, $ W file writes all content to the file, equivalent to the w file command

3. string SEARCH

Returns a string that can be searched to reach the specified row. If you want to perform a forward search, place the string to be searched between two "/". If you want reverse search, place the string in Two "?" Between:

:/Str/forward search, move the cursor to the next row containing the string Str

:? Str? Reverse search: move the cursor to the previous row containing the string Str

:/Str/W file: Forward searches, and the first row containing the string 'str'

Write File

:/Str1/,/str2/W file forward search, and write the line containing the string str1 to the line containing the string str2 into the file File

4. Text replacement

Use the S command to replace strings. The specific usage includes:

: S/str1/str2/use string str2 to replace str1 that appears for the first time in the line

: S/str1/str2/g replace all the str1 strings in the row with str2

:., $ S/str1/str2/g replace string str1 from the current row to the end of the body with string str2

: 1, $ S/str1/str2/g replace str1 with string str2

: G/str1/S // str2/g functions are the same as above

 

From the above replacement command, we can see that G is placed at the end of the command, which means to replace each appearance of the search string; without g, it means to only Replace the first appearance of the search string; G is placed at the beginning of the command to replace all rows that contain search strings in the body.

V,ShellSwitch

When editing the body, you can use the shell switch Command provided in VI command mode to execute Linux commands without exiting VI, which is very convenient or the last line mode. Syntax format:

:! After executing the shell command, return to VI

 

In edit mode, Enter K to run the VI command to find the manual page of the word where the cursor is located. This is equivalent to running the man command.

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.