The VI editor for Linux uses

Source: Internet
Author: User
Tags lowercase save file uppercase character

VI is a screen editor provided by all Unix systems, and it provides a Windows device through which you can edit files. Of course, the UNIX system slightly know people, more or less think VI super difficult to use, but VI is the most basic Editor, learn the VI, in the UNIX world will be unimpeded, comfortable, because several other text processors are not UNIX standard equipped with

Under Unix, VI is one of the standard text editors. Almost any UNIX system has the VI editor as the most basic "software" and is assembled by default in the system. In Pc-unix, the VI editor is installed on the system by default, even if you choose a system with minimal installation.

Referring to VI, you have to mention another powerful editor under Unix-Emacs. A lot of people like to take Emacs and VI to compare, but I think that Emacs and VI are not comparable. VI's lightweight features make it a preferred tool for system management, and Emacs's powerful features make it a preferred tool for system development. If you use Emacs to do system management, its huge volume and the cost of the system resources (relative to VI), will inevitably affect the efficiency of system management; However, if you use VI for software development, some of its development-oriented features are not complete, it will inevitably affect the development of flexibility. Here, our goal is to establish and manage a server, so we choose to use the VI Editor to complete the server configuration work.

Because all the server settings related to the modification work, are through the VI to complete, so prepared through the various methods described in this site to build the server, please be sure to master the use of VI method. Otherwise it will be difficult to proceed smoothly. If you use other editors, such as Emacs, you can install them online during the installation process or via the Yum command.

Usage

Accustomed to Microsoft Windows GUI Mode Editor's friend, just contact VI will definitely have a "rejection reaction", this can only advise a little bit ann ... When you get used to VI, you'll find that it's both precise and orderly, and it's important that it's very specific to the object you're editing.



The status of VI is started after the "VI" command is entered under Cui.

or enter a path to an existing file to open a text file that already exists. (here to log in to the system with the root user, then enter "vi/etc/x11/xorg.conf" for example.) )



The state after the/etc/x11/xorg.conf is opened with VI. The position of the cursor at the first character Glide line on the left.

The difference between VI and other editors is that it separates the operations of editing, processing, and so on, forming 3 different modes of operation, namely "Command mode", "Insert Mode", and "ex mode".

The editor for Notepad, and so on, which is commonly used under Windows, the cursor is already in the input wait state when the file is opened, that is, we can edit the file when we open it. For example, we want to enter new text, or copy a paragraph of text, all the premise of the operation, as long as the editor is open state, the operation is possible.

But under VI, in order to enhance the targeted text to be processed, text input, copy and so on different operations, such as the text input mode is called "Insert Mode", in the Insert mode, we can use the same as the Windows operation method to input text, edit and so on, However, copying and so on non-editing operations, in the Insert mode can not be completed, and need to switch to the corresponding mode with the corresponding command to complete.

In this case, the important VI model is explained first.

★vi Mode (Command mode, insert mode, ex mode)

1) Command mode:

When we just new or open an existing file through VI, the default mode of reading is "command mode", and the command mode is characterized by the absence of any hint in the lower left corner of the editor window.



In this mode, we cannot enter new text into this file without the corresponding command. It is suggested that the new contact with the VI friend just when you open a file with VI do not be in a desperate and random press the keyboard to try to enter some text into the file, because the command mode so-called "command", not our command line at the time of the input process such as "LS-and enter" the form of commands. Specifically, the command in the VI command mode, just a key or some key combination on the computer keyboard, when we hit the keyboard in command mode a button, the corresponding command has been executed.

Here, the command to move the cursor is introduced first. Usually the cursor movement through the keyboard to the right of the arrow keys, but because each move the cursor, the hand to move to the right side of the keyboard, so the use of VI is not effective, it is recommended to use the VI Intrinsic Command (key) to control the movement of the cursor.

   The move command for the cursor is as follows:

Move right--l
Move left--H
Move Down--J
Move Up --K
As shown above, for example, to move the cursor 8 characters to the right distance, as long as the keyboard to knock the L key 8 times, move down the distance of 3 lines, as long as the keyboard to knock the J Key 3 times can be ... That is, in the VI command mode, these keys (commands) are adopted to control the movement of the cursor.
The second is the text deletion command. Most editors in Windows simply press the [Backspace] key while deleting text. However, because in VI, the insertion mode and the command mode are separated, so in command mode you need to use the command (key) to delete the text, rather than press the [Backspace] key directly.

   the commands for text deletion are as follows:

Delete the character at the cursor location--X
Delete the line where the cursor is located--DD (knock D two times)
As shown above, for example, to remove the 3 characters from the right of the cursor position, as long as the X-key on the keyboard to hit 3 times, to delete the line where the cursor, as long as the hit DD (the D key continuously two times), the cursor is the line of all the characters and the row will be deleted.
Again, when you have finished editing a document, you want to save and exit with the command "ZZ" (uppercase, 2 consecutive knocks).

   Save and exit the document:

   Save current changes and exit ---ZZ
In addition, VI has some editorial nature of the order, summed up as follows

   useful commands with editing properties:

Undo the previous action--U
Move the cursor to the beginning of the line--0 (number 0)
Move the cursor to the end of the line--$
Moves the cursor to the top line of the current page--H
Move cursor to the bottom of the current page--L
Flip one page forward (page UP)-Ctrl + F
Flip one page backwards (page DOWN)-Ctrl + b There is also the use of VI in the server configuration, frequently used is the VI search function. In the VI command line state, after typing "/" (slash), the bottom of VI appears "/", and in the input wait state, this time enter the text or text column to find, and then press ENTER to find the corresponding text or text column. When multiple objects exist, knock "n" (lowercase) key to find the next target, knocking "n" to find the previous target.

VI is a screen editor provided by all Unix systems, and it provides a Windows device through which you can edit files. Of course, the UNIX system slightly know people, more or less think VI super difficult to use, but VI is the most basic Editor, learn the VI, in the UNIX world will be unimpeded, comfortable, because several other text processors are not UNIX standard equipped. If you don't have Joe or Pico installed on other Linux machines, you might not be able to use VI.

VI EditorThere are 3 types of modes: Command modeInput Modelast-line mode。 It is important to master these three models:

Command mode: VI starts after the default entry is the command mode, from this mode to use the command can switch to the other two modes, and no matter in any mode just click the [ESC] key can return to the command mode. Enter the letter I or O or a in the command mode to edit the file into the input mode of VI.

Input mode: In this mode we can edit, modify, input and other editing work, in the last line of the editor to display a "--insert--" Mark VI entered the input mode. We need to save the file when we have finished modifying the input, then we need to return to the command mode and save it in the last line mode.

Last-line mode: Enter ":" in the command mode to enter the mode, there are many good commands in the last line mode.

The following describes the VI Common command operations

Enter the input mode command

I enter the body on the left side of the cursor [frequently used]
A enter the body on the right side of the cursor [frequently used]
o lowercase character o to add a new line to the next line in the row where the cursor is located
o Uppercase character O, adds a new line to the previous line on the row where the cursor is located
I enter body at the beginning of the line where the cursor is located
A enter body at the end of the line where the cursor is located
Several simple ways to switch to insert mode are described above. There are also commands that allow you to first delete a paragraph before entering the insert mode
Text, thus implementing the body replacement. These commands include:
s replaces the character pointed to by the cursor with the body of the input [frequently used]
NS replaces the n characters to the right of the cursor with the body of an input
CW replaces the word to the right of the cursor with the body of the input [frequently used]
NCW replace the n characters to the right of the cursor with the body of the input
CB replaces the word on the left of the cursor with the body of the input [frequently used]
NCB replaces the n characters on the left side of the cursor with the body of the input
CD replaces the cursor's row with the body of the input [frequently used]
NCD replace the n lines below the cursor with the body of the input
C $ replaces all characters from the beginning of the cursor to the end of the bank with the body of the input [frequently used]
C0 replaces all characters from the beginning of the line to the cursor with the body of the input [frequently used]

R replaces the character at which the cursor is located, such as C3, which replaces the character at the cursor with the character 3 [frequently used]

R replaces character until "ESC" is pressed

Press the "ESC" key or the combination key "Ctrl" +[to exit the input mode

Last-line mode

W save file [Common]

W file to save the contents of the current file to the file

Wq save file and Exit VI Editor [common]

Wq files writes the contents of the current file to the file and keeps the contents of the original file intact, then exits the VI editor (if the current file contents are changed but no save operation is performed, the VI editor is not exited after the command is executed)

R file reads the contents of the file and inserts the following line of the current cursor
E file closes the current document and opens a new file (the command will not be executed if the current file contents are changed but not saved) [frequently used]
F File Rename the current file to file [frequently used]
F Print the current file name and status, such as the number of lines of the file, the line number where the cursor is located, etc.

Q Exit VI Editor (cannot exit if current file is changed) [frequently used]

Q! Force Exit VI Editor (do not save changes) [common]

# move the cursor to line # [frequently used]

PS: Some tips for using line numbers:

The value is used to specify the absolute line number; Represents the line number of the row where the cursor is located; the character "$" indicates the line number of the last line of the file, for example:

. +5 move the cursor down 5 rows from the current line

$ moves the cursor to the last line of the file

1 Move the cursor to the first line of the file

23w file writes the 23rd line to the files file

3,5w file writes line 3rd to line 5th to file
1,.W file writes line 1th to the current line to the file
., $w file writes the current line to the last line to the file
.,. +5w file writes 6 rows of content to the file from the current line
1, $w file writes all content to the file, equivalent to: W file command

Move the cursor operation

"Home" cursor to the beginning of the line
"End" cursor to end of line
Page UP pages up

Page DOWN, PAGE Down

"Delect" removes the character at the cursor position
"Ctrl" + F move forward one page

"Ctrl" + B moves backward one page

H cursor moves one character to the left [common]
J Cursor moves down one character [common]
K cursor moves up one character [common]
L cursor moves one character to the right [common]
Number 0 Move the cursor to the end of the line [frequently used]
$ move the cursor to the beginning of the line [frequently used]
G move the cursor to the end of the file [frequently used]
GG Move the cursor to the beginning of the file [frequently used]
W move the cursor to the beginning of the next word [frequently used]
E move the cursor to the end of the current or next word [frequently used]
b move the cursor to the beginning of the current or next word [frequently used]
^ Move the cursor to the first non-whitespace character of the line (tab,space) [frequently used]
: # Move the cursor to the first # position of the line, for example: 3,: 10 [common]
The cursor moves to the end of the sentence
(The cursor moves to the beginning of the sentence
Note: The sentence is filled with a space after the half-width period (.) Segmentation
The cursor moves to the end of the paragraph
{cursor moves to the beginning of a paragraph
Note: Paragraphs are split with blank lines
NG positions the cursor at the beginning of the nth row [frequently used]

n+ moves the cursor down n rows

N-Moves the cursor up n rows

n$ moves the cursor down n rows and places the end of the line

H Place the cursor at the top row of the current screen

M Place the cursor in the middle row of the current screen

L Place the cursor on the last line of the screen

~ Change the case of the current character [common]

>> move the current line to the right by 8 characters (the equivalent of the "tab" key) [common]

<< moves the current line 8 characters (equivalent to "Shift" + "tab") to the left [common]

NZ: Rolls line N to the top of the screen and scrolls the current line to the top of the screen when n is not specified.

Undo Action

The U command revokes the last operation and can be used multiple times to restore the original operation [frequently used]
U Undo All operations
. You can revert to operations that use the U command [frequently used]

Copy/delete/paste operations

x Delete the single character at the cursor and place the deleted character in the buffer (actually deleted equivalent to cut) [common]
NX removes three characters. For example, 3x deletes the three characters to the right at the cursor
"Bnx put the deleted characters in buffer b

DW deletes all characters from the current character to the end of the word (including spaces) [frequently used]

DD Delete cursor line [common] [common]
NDD deletes n rows. For example, 3DD deletes three lines of text from the current line [frequently used]

"Bndd put the deleted content in buffer b

PS: If you want to delete the contents of rows m through nth, you can enter M,ND in the last line mode. For example, 3,5d means delete line 3rd to 5th [Common]

YY copies the contents of the current cursor line to the vi buffer [common]
NYY copy n rows. For example, 5yy is to copy 5 rows from the line where the current cursor is located, and place the copied contents in the vi buffer [common]
"Bnyy put the copied content in buffer b. For example, b3yy three rows of data into the buffer B [common]

PS: If you want to copy the contents of line m to nth row, you can enter M,ny in the last line mode for example: 3,5y Copy the third row to the fifth row of contents to the buffer [frequently used]

YW Copy the contents of the current cursor to the end of the word (excluding spaces) to the vi buffer, equivalent to copying a word [frequently used]
NYW copy N Words for example: 2yw is a copy of two words
"Bnyw for example, copy n words to buffer b

y$ copy cursor position to end of line content to buffer area
y^ copy cursor position to the beginning of the content to the buffer area
P lowercase p, paste the contents of the buffer below the current line (with yy), or insert to the next position of the current cursor (with DW, X, y$, y^ mates) [Common]

P Capital P, similar to lowercase p, but pasted in opposite directions

Find and replace

The search and replace function of VI should be completed in the last line mode.

/keyword search from top to bottom, such as/work, to find the contents of the keyword work from the current cursor [frequently used]

The keyword bottom-up lookup, such as/work, represents the search for the keyword work from the current cursor [frequently used]

PS: When a match is found, the cursor is positioned at the beginning of the keyword character. To continue looking for the next occurrence, simply typing in the character n; looking back, typing N. You can enter 2n down to find the next next occurrence

S/old_string/new_string replaces the first occurrence of the old_string in the row of the cursor with new_string

S/old_string/new_string/g replaces all old_string in the row of the cursor with new_string

m,ns/old_string/new_string/g Replace all old_string of line m to nth row with new_string [common]

%s/old_string/new_string/g Replace all old_string with new_string [common]

/pattern_address/s/old_string/new_string/g looks down from the current cursor to the line that matches the pattern_address, and then replaces all old_string with new_string on that line (actually , this command is composed of/pattern_address and/s/old_string/new_string/g)

G/pattern_address/s/old_string/new_string/g finds all rows that match the pattern_address and replaces all old_string of each line with new_string (note: The meaning of the two g before and after is different, the previous G indicates that the entire text search matches the pattern_address line, and the following G indicates that each row matched with pattern_address is replaced with new_string old_string)

If Pattern_address and old_string are the same, they can be abbreviated as g/pattern_address/s//new_string

You can add a C command at the end of all commands to force each substitution to require the user to confirm, for example: s/old/new/c or s/old/new/gc [common]

PS: Some tips for finding

/str/w file forward search and writes the first line containing the string str to the file
/str1/,/str2/w file is searched and writes the line containing the string str1 to the file containing the string str2

Recover files
VI when editing a file, a temporary file is generated, which is the file. Begins and ends with a. SWP. Normal exit the file is automatically deleted, if unexpected exit such as sudden power outage, the file will not be deleted, we can choose the next edit command processing:

O read-only open without changing the contents of the file
E continue editing the file without restoring the contents of the. swp file saved
R restores the contents of the file after the last edit was not saved
Q Exit VI
D Delete the. swp file

Or use the Vi-r file name to restore unsaved content

The VI editor for Linux uses

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.