Linux file editor VI (Vim)

Source: Internet
Author: User
Tags add numbers

Reference: http://huangbao.blog.51cto.com/725279/152665

1. About the text editor

There are a lot of text compilers in Linux and UNIX, but VI should be quite common. To tell the truth, I don't like it very much. I even found some colleagues use this tracking code. Is this the legendary "Daniel "?? It's really a bit like they work at that low efficiency ...... Is there anything wrong with "XX? Everyone's level is similar. I also like to use "Source insight" (just tracing code)

It may take a while if you like it or not, but sometimes you still need it. I want to write a small script, remember some common methods, and recently want to have a training, so let's start with the most basic things.

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;
2. How to Use the VI Editor

(1) how to call vi

Run the following command to go to the following page:

[Root @ localhost ~] # Vi filename
~
~
~
(2) Three command modes of VI are available:
Command mode, used to input commands;
Insert mode, used to insert text;
Visual mode: used to highlight the video and select the body;
(3) Save and exit the file
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; (exit the Help window)

Save and exit. Function and: WQ! Same

(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;
CTRL + B Move the screen up;
CTRL + F 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.
(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;
(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;

(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 undo 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;

(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 the visual mode. Visual mode provides us with an extremely friendly range of selected text to highlight; shown at the bottom of the screen;
-- 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 SHIFT + P 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;

(9) copying and pasting operations

In fact, deletion also involves cutting. When we delete the text, we can move the cursor somewhere and press SHIFT + P to paste the content to the original place, then move the cursor to a certain place, and then press P or SHIFT + P to paste it again;
P paste the post after the cursor;
Shift + P paste the post before the cursor
(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;
(11) search and replace functions
= 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: For reverse search, press SHIFT + n 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
= Replace
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 the words in the row where the cursor is located with the, 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
3. Upload a complete picture: Why is the preview incomplete? If you need to save it to your computer, do you want to upgrade the csdn image file to a lower level ......

Related Article

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.