VI User Manual

Source: Internet
Author: User
Tags control characters first string
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.
[Color = # dc143c] [B]: #, # W newtest example: 1st W newtext command to create a file named newtext, and set ~ 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 Access the VI command
VI filename: open or create a file and place the cursor at the beginning of the first line
Vi + N filename: open the file and place the cursor at the beginning of line N
Vi + filename: open the file and place the cursor at the beginning of the last line
Vi +/pattern filename: open the file and place the cursor at the first string matching pattern
VI-r filename: a system crash occurred when VI was being edited last time, restoring filename
VI filename... filename: open multiple files and edit them in sequence

Move cursor command
H: move the cursor one character to the left
L: move the cursor one character to the right
Space: move the cursor one character to the right
Backspace: move the cursor one character to the left
K or Ctrl + P: move the cursor up a row
J or Ctrl + N: move the cursor down a row
Enter: move the cursor down a row
W or W: move one word to the beginning of the word to the right of the cursor.
B or B: move one word left to the beginning of the word
E or E: move the cursor right to the end of a word J.
): Move the cursor to the end of the sentence.
(: Move the cursor to the beginning of the sentence.
}: Move the cursor to the beginning of the paragraph
{: Move the cursor to the end of the paragraph
NG: move the cursor to the beginning of line N
N +: move the cursor down n rows
N-: Move n rows above the cursor
N $: move the cursor to the end of line N
H: move the cursor to the top line of the screen
M: move the cursor to the middle line of the screen
L: move the cursor to the last line of the screen
0: (Note the number is zero) move the cursor to the beginning of the current row
$: Move the cursor to the end of the current row

Screen tumble command
CTRL + u: half screen at the beginning of the file
CTRL + D: Flip the half screen to the end of the file
CTRL + F: Flip the screen to the end of the file
CTRL + B; open a screen at the beginning of the file
NZ: Rolls row n to the top of the screen. If n is not specified, the current row is rolled to the top of the screen.

Insert text commands
I: Before the cursor
I: at the beginning of the current row
A: After the cursor
A: At the end of the current row
O: open a new row under the current row
O: open a new row above the current row.
R: Replace the current character
R: Replace the current and subsequent characters until you Press ESC.
S: Starting from the current cursor position, replace the specified number of characters with the input text
S: delete a specified number of rows and replace them with the input text
NCW or NCW: modify a specified number of characters
NCC: modifies a specified number of rows.

DELETE command
Ndw or ndw: n-1 characters starting at and following the cursor
Do: Delete to the beginning of a row
D $: Delete to the end of the row
NDD: Delete the current row and the next n-1 row
X or X: delete a character. x deletes the character after the cursor, and X deletes the character before the cursor.
CTRL + u: delete text entered in input mode

Search and replace commands:
/Pattern: Search for pattern from the beginning of the cursor to the end of the file
? Pattern: Search for pattern from the beginning of the cursor
N: Repeat the previous search command in the same direction.
N: Repeat the previous search command in the reverse direction.
: S/P1/P2/G: replace all p1 in the current row with P2.
: N1, N2s/P1/P2/G: replace all P1 from line N1 to line N2 with P2.
: G/P1/S // P2/G: replace all P1 files with P2.

Option settings
ALL: lists all options.
Term: Set the terminal type
Ignorance: Case Insensitive in search
List: displays the stop table (CTRL + I) and end mark ($)
Number: displays the row number.
Report: displays the number of changes made by line-oriented commands.
Terse: displays brief warning information
Warn: No write information is displayed if the current file is not saved when it is transferred to another file.
Nomagic: allows you to use special characters without "" In search mode.
Nowrapscan: Prohibit VI from searching at both ends of the file and starting from the other end.
Mesg: Allows VI to display the information that other users write to their terminal using write.

Last-line command
: N1, N2 Co N3: copy the content from line N1 to line N2 to line N3.
: N1, N2 M N3: Move the content from line N1 to line N2 to line N3
: N1, N2 D: Delete content from line N1 to line N2
: W: Save the current file
: E filename: open the file filename and edit it.
: X: Save the current file and exit.
: Q: Exit VI.
: Q! : Do not save the file and exit vi
:! Command: execute shell command
: N1, N2 W! Command: The content from line N1 to line N2 in the file is used as the command input and executed.
If N1, N2 is specified, the entire file content is used as the command input.
: R! Command: place the output result of the command to the current line. Xsb published on: 2005.11.09: Classification: (OS): Read :( 751 times): permanent link: Reference (0) Re: VI User Manual (zt) [reply]

Http://www.5ilinux.com/blog/archives/000058.html
The most powerful Editor-vi
VI is a screen editor provided by all UNIX systems. It provides a Windows device that allows you to edit files. Of course, people who know a little about UNIX systems may feel that VI is super difficult to use, but VI is the most basic editor, so I hope readers can learn it well, in the future, the Unix world will surely be smooth and easy to use, because other types of text processors are not UNIX-standard. Maybe someone else's Linux machine does not install Joe or Pico. If you don't know Vi, you may not have to worry about it.
Basic concepts of vi
Basically, VI can be divided into command mode, insert mode, and last line mode ), the functions of each mode are as follows:
1. Comand mode: controls the movement of the screen cursor, the deletion of characters or the cursor, the replication of a specific segment, the entry into the insert mode, or the last line mode.
2. insert mode: only in insert mode can text data be input. Press ESC to return to Comand mode.
3. Last line mode: Save the file or leave the editor. You can also set the editing environment, such as searching strings and listing row numbers.
However, you can simplify VI into two modes, that is, the last line mode is also counted into command mode, and VI is divided into command and insert mode.
Basic operations of vi
• Go to VI
After the system prompts you to enter the VI and file name, you can enter the VI full screen editing screen:
$ VI testfile
Note that after entering Vi, you are in "command mode". You must switch to "insert mode" to enter the text. Users who use VI for the first time will want to move the cursor first with the upper and lower left keys. As a result, the computer keeps calling and getting angry with themselves. So after entering VI, do not change it into insert.
• Switch to insert mode to edit the file
Press 'I', 'A', or 'O' in command mode to enter the insert mode. At this time, you can start to enter the text.
I: insert. Insert the entered text from the current cursor.
A: Add. Input text starts from the next word where the cursor is currently located.
O: Insert a new row and enter text from the beginning of the line.
• Insert switchover → command mode, and Press ESC
If you are currently in insert mode, you can only keep typing. If you find a typo, and want to use the mouse button to move back, delete the word, you need to press ESC to switch back to command mode, and then delete the text.
• Exit VI and save files
In command mode, you can press the colon ":" to enter the last line mode. For example:
: W filename (enter "w filename" to save the article to the specified filename)
: WQ (enter "WQ". Because the file name testfile has been specified at the time of entry, it will write testfile and leave VI)
: Q! (Enter "Q !", Force exit and discard edited files)

Command mode function key list
When introducing the command mode command, add the "commonly used" function key to the command to indicate commonly used VI commands. You must learn and remember them.
(1) switch I, A, and O to insert mode. [Super common]
(2) move the cursor
VI can be moved up, down, left, right, and left directly using the optical key on the keyboard, but the regular VI is a lowercase English letter.
H, J, K, and l, respectively, control the left, down, up, and right shifting of the cursor.
Press Ctrl + B: Move the screen back one page. [Common]
Press Ctrl + F: Move the screen one page forward. [Common]
Press Ctrl + u: Move the screen back half a page.
Press Ctrl + D: Move the screen half page forward.
Press 0 (number zero): to move the beginning of an article. [Common]
Press g to move to the end of the article. [Common]
Press W: the cursor to jump to the beginning of the next word. [Common]
Press E: move the cursor to the end of the next word.
Press B: The cursor returns to the beginning of the previous word.
By $: Move to the end of the row where the cursor is located. [Common]
Press ^: to move to the first non-blank character of the row.
Press 0: Move to the beginning of the row. [Common]
Press #: Move to the # position of the row, for example, 51 and 121. [Common]
(3) Delete text
X: each time the cursor is deleted, the next character is deleted. [Extraordinary use]
# X: for example, the 6X table deletes the next six characters of the cursor position. [Common]
X: X of the big character. Each time the cursor is deleted, the first character is deleted.
# X: for example, in the 20x table, the first 20 characters of the cursor position are deleted.
DD: Delete the row where the cursor is located. [Extraordinary use]
# DD: for example, the 6dd table deletes six lines of text from the row where the cursor is located. [Common]
(4) Copy
YW: copy the character from the cursor position to the end of the word to the buffer zone.
(Unlike # X and # X)
P: paste the characters in the buffer to the cursor position (the command 'yw' and 'P must be used together ).
YY: copy the row where the cursor is located. [Extraordinary use]
P: copy a single row to where you want to paste it. (The command 'yy' and 'p' must be used together)
# YY: for example, 6yy indicates copying six lines of text from the row where the cursor is located. [Common]
P: Copy multiple rows to where you want to paste them. (The command '# yy' and' P' must be used together)
"Ayy: Put the replication row into buffer A. VI provides the buffer function to store common data in the buffer.
"AP: paste the data stored in buffer.
"B3yy: store three rows of data into buffer B.
"B3p: paste data with buffer B
(5) replace
R: Replace the character at the cursor position: [common]
R: Replace the character until you Press ESC.
(6) restore the previous command (UNDO)
U: If you misoperate a command, you can immediately press u to return to the previous operation. [Extraordinary use]
..: You can execute the last command again.
(7) Change
CW: change the word at the position of the cursor to the end of the word $.
C # W: for example, c3w indicates to change three characters.
(8) Jump to specified row
CTRL + G: displays the row number of the cursor.
# G: for example, 15g indicates moving the cursor to the first row of the article. [Common]
Introduction to commands in last line mode
Before using last line mode, remember to Press ESC to confirm that you are in command mode, and then press the colon ":", "/" or "?" One of the three keys enters the last line mode.
1. List row numbers
Set nu: After "set nu" is input, the row number is listed before each row of the article.
2. Jump to a line in the article
#: The well number represents a number. Enter a number before the ":" symbol in last line mode, and press enter to jump to this line, for example: 15 [enter] will jump to the 15th line of the article. [Common]
3. Search for strings
/Keyword: First press/and then enter the word you want to search for. If the keyword you are looking for the first time is not as close as possible, you can press n until you find the keyword you want.
? Keyword: First press ?, Enter the word you want to search. If the keyword you want for the first time is not what you want, you can press n to search for the keyword you want.
4. Replace string
1, $ S/string/replae/G: Input "1, $ S/string/replace/g" in last line mode to replace the full-text string with the replace string, among them, 1, $ s refers to the search range as the meaning of the article from the beginning to the end, G indicates that all replace does not need to be confirmed.
% S/string/replace/C: Replace the full-text string with the replace string, which is different from the preceding command: % s and 1, $ S is the same function, and C indicates that you must confirm whether to replace it before replacement.
1, 20 s/string/replace/G: Replace the string between 1 and 20 lines with the relpace string.
5. save files
W: Press W before the last line mode prompt ":" To save the file. [Extraordinary use]
#,# W filename: If you want to extract a part of the article and save it as another file, use this command # to indicate the row number, for example, 30, 50 w nice. The 30th ~ 50 rows are saved as nice files.
6. Leave
Q: Press Q to exit. Sometimes, if you cannot exit Vi, you can use "! : Force exit VI, such as "Q !"
QW: it is generally recommended that you use it with w when you exit. files can be stored when you exit. [Common]

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.