How to use file editor vi

Source: Internet
Author: User
Article Title: Introduction to the usage of file editor vi. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Vi is short for visual editor and one of the editors provided by UNIX. It provides an editing environment for a user's window. In this window, the user can edit the desired file. Vi and celvis (cvi) are widely used Chinese and English editing software on workstations. For beginners, it is often because of their special usage, rather than entering the door. for users who are already using vi, it is also common because they are not familiar with or do not know enough about vi, however, you cannot make full use of the powerful editing capabilities of vi. The following describes how to use vi.
  
Go to vi
======
Run the vi editing program directly:
% Vi test
At this moment, the vi editing window will appear on the screen, and vi will copy the file to the memory.
Buffer ). Vi will keep the files in the disk unchanged, but first to the buffer File
After editing, the user can decide whether to replace the original file.
  
Leave vi
======
In the input mode, use ESC to enter the command mode, and then select the following command
Leave vi.
: Q! Leave vi and discard the content you just edited in the buffer.
: Wq writes data in the buffer to the disk and leaves vi
: ZZ is the same as wq
: X is the same as wq
: W writes data in the buffer into the disk, but does not leave vi
: Q leaves vi. If the file has been modified, it is required to confirm whether to discard the modified content. This command can be used with w.
  
Vi Operation Mode
==================
Vi provides two operation modes: insert mode and command mode ). When the user enters vi, that is, in command mode, any words entered at this moment are considered as commands. In this mode, you can delete, modify, and perform other actions. To enter data, enter the input mode.
  
Input mode
==========
How to enter the input mode
A (append) adds data after the cursor.
A is added by the end of the row.
I (insert) adds data before the cursor.
I. Add the information at the beginning of the row.
O (open) adds a row under this row for data input.
O adds a row above this row for data input.
  
How to exit input mode
ESC ends the input mode.
  
Command mode
==========
Move cursor
H shifts one character to the left.
J. Move one character up.
K moves one character down.
L move one character to the right.
0 to the beginning of the row
$ Move to the end of the row.
^ Move to the first character of the row.
H: Move to the first column of the window.
M moves to the middle column of the window.
L move to the last column of the window.
G to the last column of the file.
+ Move to the first character of the next column.
-Move to the first character in the previous column.
(Move to the beginning of the sentence. (Note 1)
) To the end of the sentence.
{Move to the beginning of the paragraph. (Note 2)
} Move to the end of the paragraph.
NG is moved to column n of the file.
N + column n after the cursor is located.
N-column n before the cursor position.
The row number, file name, row number of the last row in the file, and row number of the cursor as a percentage of the total row number are displayed.
  
Note 1: sentence (sentence) in vi refers 『!』 , "." Or 『?』 End string.
NOTE 2: paragraph (paragraph) refers to text separated by blank lines in vi.
  
Moving Windows
============
Windows to the next page.
Windows to roll up a page.
Windows to the next half page.
Windows to the upper half page.
Window to the next volume line.
Windows to roll up a row.
  
Introduction to delete, copy, and modify commands (this unit is rarely used)
======================================
The command formats of d (delete), c (change), and y (yank) commands in vi are as follows:
Operator + Scope = command
(Operator) (range)
OPERATOR:
D. Delete the command. The deleted data is copied to the memory buffer.
Y copies data (word groups, columns, sentences, or paragraphs) to the buffer zone.
P placement (put) command, which is used with d and y. The last delete or yank resource can be
Placed in the row and column where the cursor is located.
C modify (change) command, similar to the group and of delete and insert. Delete A Word Group or sentence
And insert new key information.
  
Range:
E is the last character of the string from the cursor position.
W is the first character from the cursor location to the next string.
B is the first character from the cursor position to the previous string.
$ From the cursor location to the last character of the row.
0 is the first character of the row from the cursor position.
From the cursor location to the first character in the next sentence.
(From the cursor location to the first character of the sentence.
{From the cursor location to the last character of the paragraph.
} The first character from the cursor position to the paragraph.
  
Whole row action
Dd deletes the entire row.
D. Delete All characters after the cursor in the unit of action.
Cc modifies the entire line.
Yy yank copies the row of the cursor to the memory buffer.
  
Delete and modify
============
X deletes the character of the cursor.
X deletes the previous field of the cursor.
Dd deletes the row where the cursor is located.
R replaces the character of the (replace) cursor with the character following this command.
For example, ra replaces the character of the cursor with.
R enters the replacement status until ESC.
S. Delete the character of the cursor and enter the input mode until ESC.
S. Delete the row where the cursor is located and enter the input mode until ESC.
  
Migration and Replication
============
You can use the delete and put commands to migrate data.
Yank and put commands can be used to replicate data.
Yank and delete can copy the specified data to the memory buffer, and use the put command
Data in the buffer zone can be copied to the screen.
Example:
Move a row. Execute dd in this row
. Move cursor to destination
. Execute p
Copy a row. Execute yy in this row
. Move cursor to destination
. Execute p
  
Duplicate commands
==========
In command mode, you can add a number n to the front of the command, and the command action repeats n
Times.
Example:
Delete 10 rows. 10dd
Copy 10 rows. 10yy
. Move cursor to destination
. P
10 rows. 10j
  
Undo)
============================
That is, the content before executing the previous command is restored.
  
U.
U restores all changes to the row of the cursor.
  
Search
====
You can search for a field in vi to move the cursor to this field.
  
/String to search for the string after the cursor.
? Returns the string before the cursor.
N to continue searching for the next same string.
N continues to search for the next same string.
  
Data Connection
============
J sentence connection. Connect the next row of the cursor to the end of the row.
  
If a row of data is too long, it can also be divided into two rows, as long as the cursor is moved to the separate point, enter the input mode
(You can use commands such as a and I) and then press Enter.
  
Environment setting
============
: Set nu to set the row number of the data.
: Set nonu cancel row number setting.
: Set ai is automatically scaled in and out.
: Set noai cancels automatic contraction.
  
Automatic indentation)
When editing a file or program, you may sometimes experience a situation where you need to scale in and out. ": set ai" provides
The following example describes the function of dynamic contraction:
. Vi test
. (After entering the editing window)
This is the test for auto indent
Tab start indent labels: set ai (set to auto internal contraction)
Tab data
Tab data
Tab data Compression: set noai (Cancel automatic contraction)
The end of auto indent.
. Note: You can delete the Tab character.
  
Ex command
======
Read and Write Data
: W writes data in the buffer zone to the disk.
: 10th, 20th test writes data from rows to the test file.
: 10th w> test: add the data of rows 20th to rows after the test file.
: R test reads the data of the test file into the end of the editing buffer.
  
Delete, copy, and move
: 10, 20 d delete data from rows 10th to rows 20th.
: 10 days to delete data from the first row.
: % D Delete the entire editing buffer.
: 10, 20co30: Copy data from rows 10th to rows 20th to rows 30th.
: 10, 20mo30: Move the data of row 10th to row 20th to row 30th.
  
String search and replacement
The s (substitute) command can be used to search for a range of rows and columns.
The g (global) command can be used to search for information in the entire editing buffer zone.
The s command is replaced by the first string that meets the condition. If the row contains several
To replace all strings, you must add the g parameter.
: 1, $ s/old/new/g change all the "old" in the file to "new 』.
: 10th s/^ // insert 5 blank spaces at the top of the rows from 20th.
: % S/old/new/g change all "old" in the editing buffer to "new 』.
  
Resumes files interrupted during editing.
======================================
During the editing process, if the system crashes or the connection is interrupted, and the data in the buffer zone is not
When it is written back to the disk, when it is returned to the system again, you can run the following command to reply to the interrupted
.
% Vi-r filename
  
Edit multiple files
================
Vi also provides the ability to edit multiple files at the same time. The method is as follows:
% Vi file1 file2 ..
  
VI skills
  
X deletes the character of the cursor.
Dd deletes the column where the cursor is located.
S deletes the character of the cursor and enters the input mode.
S: Delete the column where the cursor is located and enter the input mode.
R: Modify the character of the cursor, and then modify the character of the cursor.
R enters the replacement status. The new data will overwrite the original data until [ESC] is pressed back to the command mode.
+ $ Jump to the end of the file
[Home] Jump to the beginning of the line
[End] jump to the end of the row
  

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.