VI command usage

Source: Internet
Author: User
Tags control characters linux shell commands

Start the visual editor from Shell

VI filename indicates the shell to start the VI editor and pass the filename parameter to it. If the file exists, the VI editor interprets it as the file to be opened. If the file does not exist, the VI compiler creates a new file.

VI file1 file2 file3 shell passes three parameters to VI, which is interpreted as the file to be opened by VI. You can run the W command to save the file and run the N command to access the next file.

Vi + # filename open the file and move the cursor to the specified row. For example, run the command vi + 100 records to edit the file records from line 3.

Vi +/the filename open the file and move the cursor to the row containing the target string. For example, the command vi +/Jason friends starts editing the file friends from 1st lines containing the string Jason.

View filename: open the file and edit it. However, the file cannot be modified unless W is used! Command

Cursor movement command

Hjkl moves the cursor to the left, bottom, top, and right of a character.

0 (zero) move the cursor to the beginning of the current row

^ Move the cursor to the beginning of the row like 0

$ Move the cursor to the end of the current row

# G move the cursor to the row specified by the number before G. For example, move the cursor to the 42nd rows of the file at 42 GB.

G move the cursor to the last line of the file

W move the cursor forward to the first letter of the next word

E move the cursor forward to the last letter of the next word

B. move the cursor backward to the first letter of a word.

-Move the cursor to the beginning of the last row.

+ Move the cursor to the beginning of the next row

12 | move the cursor to the 12th column of the current row

L move the cursor to the bottom line of the screen

M positions the cursor in the middle of the screen

H. move the cursor to the top line of the screen.

''Two single quotes move the cursor to its previous position

Cursor location command (context)

FB moves the cursor forward to the next letter B on the current row (or any other specified character)

FB moves the cursor backward to the previous letter B (or specified character) on the current row)

T # move the cursor to the right of the 1st instance of the character # On the current line. For example, the TM command moves the cursor to the right of 1st Mb on the current line.

T # move the cursor to the left of the current row and move it to the first character of the 1st instance of character #

/Word move the cursor forward to the next instance of WORD

? Word moves the cursor backward to the previous WORD instance

N move the cursor to the Front Command/word or? Next instance of the specified mode in Word

Show Adjustment Command

CTRL + D display the text in the lower half of the file

CTRL + u display the upper half text in the file

CTRL + F display the next screen text in the file

CTRL + B display the previous text in the file

Set display options

: Set number uses the row number as a part of the screen, but the row number is not part of the file. It is abbreviated as: Set nu

: Set nonumber: Clear the row number on the screen. You can also use the abbreviation set Nonu.

: Set showmode: displays append mode information in the lower right corner of the screen.

: Set list displays the dollar sign at the end of each row, and uses Ctrl + I to represent the tab

: When set showmatch is input) or], move the cursor to the matched (or [

: Set window = value defines the number of lines of text lines displayed on the screen

: Set autoindent: auto indent. You can also use the abbreviation set AI.

: Set tabstop = value: set the number of spaces that display tabs. You can also use the abbreviation Ts = value.

: Set wrapmargin = value: set the right side of the display. When you enter the configured page edge, the editor automatically returns a line feed.

: Set ignorecase indicates that the editor searches for strings and ignores the uppercase and lowercase letters in the target.

: Set: displays all the settings.

: Set all: display all options that can be set

Text deletion command

Dd deletes all text lines of the current cursor

# Dd Delete # Line Text

DW deletes a word from the text

# DW Delete # words from text

X deletes a character from the cursor.

# X remove # characters from text

D. Delete the part after the cursor on the current row.

: #, # D For example,: 12, 37d will delete 12th ~ All text between 37 rows, including 12th and 37 rows

Undo command

U undo. Restore the last text modification operation, even if the cursor has been moved. In Linux, use the undo command again to restore the previous text modification operation. In bsd vi, the first Undo operation will be abolished for 2nd undo operations, and the text modified before the first Undo operation will be restored.

: Redo in Linux, cancel the Undo operation to restore text modification. In standard UNIX systems, 2nd U commands cancel 1st U commands, and the result is a "redo"

U if the cursor has not been removed from the current row after modification, you can cancel all text modifications made to the current row.

Add text to text

A (lower case) inserts text from the right of the cursor

A (uppercase) adds text from the end of the current row

I (lower case) insert text from the left of the cursor

I (uppercase) insert text from the beginning of the current row

O (lower case) Open (or insert) a new line under the cursor

O (uppercase) opens a new line above the cursor

: # R filename for example: 8r report. Old reads the report. Old File and places its content after the first row of the current file

No matter what commands are used to enter the append/insert mode, you can press ESC to exit the append mode and return to the VI command mode.

CTRL + V allows the input of control characters. Press Ctrl + V and press enter to insert Ctrl + M to the file.

Modify text in a file

CW only modifies the word at the cursor (delete the word and enter append mode to add text at the position of the word to be deleted)

S (lower case) replace a single character

S (uppercase) replaces the entire line of text

CC replaces the entire line of text (same as S)

R replaces the character at the current cursor with the next character entered, and automatically returns to the command mode

R place the editor in overwrite mode and replace the characters at the cursor one by one with the entered characters.

C (uppercase) modify the text between the cursor and the end of the line

CT # modify the text on the row between the cursor and the first 1st target characters. For example, cty deletes all text from the cursor to the first 1st characters y on the current line, and enters the append mode to add text at the position of the deleted text

Cf # modify the text (including the target character) between the cursor and the first 1st target characters in a row ). For example, cfy deletes all text (including Y) between the cursor and the first 1st characters of Y on the current line, and enters the append mode to add text at the position of the deleted text

CT # modify the text between the cursor and the 1st Target Characters in the row. For example, cty will delete all text on the current line from the cursor to the back of 1st characters y, and enter the append mode to add text at the position of the deleted text

Cf # modify the text (including the target character) between the cursor and the 1st Target Characters in the row ). For example, cfy deletes all text (including Y) from the cursor to the back of the current line, and enters the append mode to add text at the position of the deleted text

The word used to extract and paste rows.

YY copies or connects the current row to the internal buffer zone. 20yy copies the current line and the 19 lines (20 lines in total) following it To the memory. The target row is still in the file. You can use the p command to paste the text in the memory into the file.

Dd deletes the current row and places it in the same memory buffer used by the YY command. The target line is deleted from the file, but you can use the p command to paste it elsewhere in the file.

YW pulls out or copies the word where the current cursor is located to the inner buffer zone. The 6yw command will copy the current word and the five (6 in total) words following it To the memory.

DW deletes the current word and places it in the same memory buffer used by the YW command. You can use the p command to paste words to other places in the file.

Yt # receives the text from the cursor to the forward character (excluding this character. For example, the ytb command takes the text from the cursor to the next instance of character B (excluding character B) and copies it To the memory.

YF # receives the text from the cursor to the forward character (including this character. For example, the YF command is used to extract or copy text from the cursor to the next instance of character: (including characters :) to the memory.

Yt # Forward (excluding target characters ). For example, the YTN command is used to extract or copy text (not including CHARACTER n) from the cursor to CHARACTER n to the memory of the 1st instances.

YF # Forward (including target characters ). For example, the YFJ command will take the text (including the CHARACTER n) from the cursor to the back 1st instances of CHARACTER n and copy it To the memory

P paste the text lines in the memory to the bottom of the row where the cursor is located in the file, or paste the words in the memory to the right of the cursor in the file

P (uppercase) paste the extracted or deleted lines of text to the row where the cursor is located in the file. Or paste the extracted or deleted words to the left of the cursor in the file.

File movement command

J. Merge the downstream text with the current row into one row.

: #, # Move the specified row to the target position. : 12th, 35 move 58 command will be ~ All the text between 35 rows is moved to the end of 58th rows. Abbreviated as Mo

: 1st-Co 82 ~ Copy all text between the 26 rows to the end of the 82nd rows (you can select a row number)

Use the visual editor for global editing

: S/target/replacement/searches for the 1st instances of the target string on the current row and deletes them. Then, replace them with the string replacement. Only 1st target instances on the current row are modified.

: G/target/S // replacement/search for the 1st instances of the target string on all rows and delete them. Then replace them with the string replacement. Modify the target 1st instances on all rows

: #,# S/target/replacement/replace on the specified row. For example, 7, 37 S/March/walk/will search for 7th ~ All text lines between 37 rows, and replace the 1st target strings march in each line with the string walk. Modify 1st target strings on all specified rows

: #,# S/target/replacement/g perform global replacement on the specified row. For example, 1, $ S/Fun/joyful/G will search for and delete all instances of the target string fun between row 1st and the last line of the file, and then replace it with the string joyful. All instances of the target string on the specified row are modified.

: G/target/S/Replacement: Find and delete the 1st instances of the target string on all rows, and then replace them with the string replacement. The 1st targets on all rows are modified.

: #,# Target/S/replacement/replace on the specified line. For example, the 7th-March/S/walk command ~ Search for 1st instances of the target string on each line of text between 37 and delete them, and replace them with the string walk. All the 1st target strings on the specified row are modified.

: #,# Target/S/replacement/g perform global replacement on the specified row. For example,: 1, $ fun/S/joyful/G will search for all instances of the target string fun from row 1 to the last line of the file and delete them, and then replace them with the string joyful. All target strings on the specified row are modified.

Editing tool: ing, abbreviations, and tags

M # mark the current row with letters. For example, the MA command indicates that the current line is marked with. Even if a row is moved, it is still marked as a. You can use a to locate the row.

'# Locate the mark row. For example, the command 'a moves the cursor to the line marked. The command 'a, $ D will delete all lines from the marked row to the end of the file

: Map # command string when input # In command mode, use it as a command string. For example, map # O #! /Bin/KSh generates a new command mode command. When you enter #, it is interpreted as: O opens a new line and adds text #! /Bin/KSh to the file. To include carriage return and other control characters in the command, you can use the CTRL + V command before them.

: AB abbreviation char-string sets the abbreviation of append mode. For example, enter AB MV Milky Way galaxy in command mode to create an abbreviation. If you enter the string MW in append mode and Press ESC, MW will be replaced by Milky Way galaxy

Issue commands to shell in VI Editor

:! Ls starts a shell and enables the shell to run the LS program. After running the specified program, you must press the Enter key to return to the editor.

:! Ksh starts a shell, which allows you to run multiple commands. Exit shell and return to the compiler.

: Or! Speel % checks the spelling of the current file (%), and sends the spell output to the current file, starting from row 1st (after line 0 ).

: 31R! Command % run a Unix Command (such as Cal or date) and read its output to the current file. Place the output from line 1

CTRL + Z is used to suspend the CSH and KSh commands of the current editing session process. It allows you to issue commands to the parent shell.

FG reactivate the CSH and KSh commands of the pending editing process

Read, write, and exit the editor

: WQ saves the changes made to the file during the editing session, and exits the editor and returns to shell.

: Q. If you have not modified or added a file, you can use Q to exit the editing of a file.

: Q! Exit file editing and return to shell mode, but do not save the modifications made to the file during the editing session

: W filename V: Save the buffer copy (modified version) of the file to a new file.

W newtest example: 1st W newtext command to create a file named newtext, and ~ Copy six lines of text to the newtext file.

: 1st W> oldfile: replace ~ Append a copy of the six lines of text to the end of the existing file oldfile.

! Oldfile uses 1st ~ of the current file ~ Six lines of text overwrite the oldfile

--------------------------------------------------------------------------------

--------------------------------------------------------------------------------

Delete collection vi

: % S/R // G press ENTER ^ m to delete the DOS mode (this mode should be used in VI-B mode: % S/^ m // among them, ^ m is played by CTL + V and CTL + M)

: % S = * $ = Delete the blank space at the end of the row

: % S/^ (. *) N1/1 $/delete duplicate rows

: % S/^. {-} PDF/newusers/only deletes the first PDF

: % S/<! -- _. {-} --> // Delete the multi-line comment (comment? Why do we say "again ?)

: G/S * ^ $/d delete all empty rows: Is this easy to use? Has anyone used any other methods? (In this blog, press: Delete the line without character application: G/^ $/d to delete the line application with some spaces: g/^/S * $/d Indicates several rows starting with a space (/s)

: G! /^ DD/d Delete the row without the string 'dd'

: V/^ DD/d same as above (translated as V = G !, Is not matched !)

: G/str1/,/str2/d delete all the rows from the first entry containing str1 to the first entry containing str2.

: V/./,/./-1 Join compressed empty rows

: G/^ $/,/./-J compressed empty rows

Ndw or ndw deletes n-1 characters starting at and following the cursor.

Delete D0 to the beginning of the row.

D $ Delete to the end of the row.

NDD deletes the current row and the next n-1 row.

Delete a character from X or X.

CTRL + u Delete the text entered in the input mode.

^ R restore u

J. Merge the next row to the end of the current row

V select a row

^ V press ^ V to select a rectangle.

Aw select word

IW internal words (no space)

As Select sentence

Is Select sentence (no space)

AP selection Section

IP selection section (no space)

D. Delete it to the end of the row.

X, Y Delete and copy contain highlight Area

DL deletes the current character (same as the X command)

D0 is deleted to the starting position of a row.

D ^ Delete the first character (excluding spaces or tab characters) to a row)

DW is deleted to the end of a word.

D3w is deleted to the end of the third word.

DB is deleted to the start position of a word.

DW is deleted to the end of a word with a space as the separator.

DB is deleted to the start position of a word with space as the separator.

D7b is deleted to the start position of the first seven words with spaces as separators.

D) Delete to the end of a statement

D4) to the end of the fourth statement.

D (delete to the start position of a statement)

D) delete it to the end of a paragraph.

D {Delete to the beginning of a paragraph

D7 {Delete the first 7th paragraph positions before the start position of the current paragraph

Dd deletes the current row

D/Text: Delete the position specified by "text" from the text,

Forward until the content of the next occurrence (but not including the word)

DFC deletes the position where the character "C" appears from the text and continues until the next occurrence of the character (including the character ).

DTC deletes the content from the current row until the next character "C" appears

D. Delete it to the end of a row.

D $ Delete to the end of a row

5dd Delete the contents of the five rows starting from the current row

Delete DL content until the last line on the screen

DH Delete until the first line of content on the screen

Delete content from the DG until the end of the work Cache

D1g Delete content that begins in the work Cache

 

References

 

 

Common Linux shell commands (2)

 

Linux Shell script BASICS (1)

 

Linux Shell script BASICS (2)

 

Linux VI command usage tips to make everything easier

 

Practical Tips: Set of shortcut keys for VI commands in Linux

 

Linux VI commands

 

Linux Shell Programming

 

Linux Shell script comprehensive learning

 

Classic shell programming instance

 

Oracle shell for Unix/Linux

 

Common shell backup scripts in Linux

 

The best Linux Shell 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.