Linux file editor vi

Source: Internet
Author: User
Tags add numbers
1. Text Editor;

There are many text editors, such as gedit, kwrite, OpenOffice ......, in text mode, the editors include VI, VIM (enhanced version of VI), and nano ...... VI and Vim are the most commonly used editors in Linux. It is necessary to introduce the simplest usage of VI (Vim) so that entry-level Linux users can learn to use it in the shortest time.

The nano tool is similar to the edit operation in the DOS operating system. It is easy to use and we will not introduce it. If you are interested, try it;


2. VI editor;

Why do we need to learn simple application Vi?

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. When 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;


3. Use the VI editor;


3.1 How to call VI;

 

[root@localhost ~]# vi filename

 

 

~
~
~
~
~
~
~
~

 


3.2 three command modes of VI;

 

Command mode, used to input commands;
Insert mode, used to insert text;
Visual mode: used to highlight the video and select the body;

 


3.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;
: X should be saved and exited. Function and: WQ! Same

 


3.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.


3.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;

 


3.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;

 


3.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 cancel 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;


3.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 visual mode;

The visual mode provides us with an extremely friendly selection of text ranges for highlighted display; at the very bottom of the screen, there are;

 

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


3.9 copying and pasting;

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

 

For example:

For example, if we want to copy the third line of a document and post it to the end of the fifth line, what should we do?

There are two methods;

Method 1:

First Delete the third line, move the cursor to the third line, then use the DD action, and then press SHIFT + P. In this way, the third line of the deleted post is located in the original place.

Then we move the cursor to the fifth line with the K key, and then press the P key to post the content of the third line to the end of the fifth line;

Method 2;

Enter the visual mode, Press ESC, and then press v. Move the mouse pointer, select the content of the third line, and press y to copy; move the pointer to the fifth line, and then press p;

Therefore, the copy and paste operations are the combined use of command mode, insert mode, and visual mode. We need to learn how to switch between various modes, and usually use the ESC key; more importantly, move the cursor in command mode;


3.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;


3.11 search and replace functions;


3.11.1 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: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

 


3.11.2 replacement;

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 words with the row where the cursor is located, which should be:

 

:s /the/THE/g

 

We replace all the "the" in the entire document with the following:

 

:%s /the/THE

 

We just replace the, in rows 1st to 10th with the, which should be;

 

:1,10 s /the/THE/g

 

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.