[Go] vi/vim command mode and edit mode various operations

Source: Internet
Author: User
Tags add numbers

Summary: The vi editor is the most commonly used document creation and editing tool. Beginners should learn to use vi easily, and learn to do simple modifications, deletes, inserts, searches and replacements in vi. If you are a newbie, you may wish to take a look at this article. Perhaps this document will allow you to learn the simple operation of vi in the shortest possible time;



1. About the text editor;

There are many text editors, gedit, kwrite, OpenOffice... in text mode, vi, vim (vi enhanced version) and nano... vi and vim are editors in text mode The most commonly used editor in Linux. It is necessary to introduce the easiest usage of vi(vim) to let Linux entry-level users learn to use it in the shortest amount of time.

The nano tool is similar to the edit operation under the DOS operating system. It is easy to use. We will not introduce it. If you are interested, you may wish to try it.

2, vi editor;

Why learn to use simple vi

Vi or vim is the most basic text editing tool for Linux. Although vi or vim does not have the simple operation of the mouse like the graphical interface editor, the vi editor is never the editor of the graphical interface in system management and server management. . When you do not install the X-windows desktop environment 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, the use of the vi editor;

3.1 How to call vi;


[[email protected] ~]# vi filename



3.2 vi three command modes;


Command mode for entering commands;
Insert mode for inserting text;
Visual mode for highlighting of the visualization and selecting the text;

3.3 File saving and exiting;

Command mode is the default mode of vi or vim. If we are in other command mode, we need to switch through ESC key.

When we press the ESC key and then enter the : sign, 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 with filename as the file name and exit;
:q! Do not save and exit;
:x should be saved and exited, the function is the same as: wq!

3.4 cursor movement;

When we press ESC to enter Command mode, we can use some of the following keys to move the cursor;


j moves down one line;
k moves up one line;

h Move one character to the left;
l Move one character to the right;

Ctrl+b moves up one screen;
Ctrl+f moves down one screen;

Up arrow
Down arrow to move down;
Left arrow to the left;
Right arrow to the right;

When we edit a file, we can also add numbers to the j, k, l, and h keys in front of these action commands, such as 3j, which means that we move down 3 lines.

3.5 Insert mode (insertion of text);


i is inserted before the cursor;
a Insert after the cursor;

I is inserted at the beginning of the line where the cursor is located;
A is inserted at the end of the line where the cursor is located;

o Insert a line above the line where the cursor is located;
O Insert a line below the line where the cursor is located;

s delete one character after the cursor and enter the insert mode;
S delete the line where the cursor is located, and then enter the insert mode;

3.6 deletion of text content;


x one character;
#x Delete a few characters, #表示数, such as 3x;
Dw delete a word;
#dw Delete a few words, # is represented by a number, such as 3dw means to delete three words;
Dd delete a line;
#dd Delete multiple lines, # represents a number, such as 3dd means to delete the cursor line and the next two lines of the cursor;
d$ deletes the cursor to the end of the line;

J clears the space between the line where the cursor is located and the previous line, and connects the cursor line to the previous line;

3.7 restore modification and restore deletion operation;


u Undo the modification or deletion operation;

Press the ESC key to return to the Command mode, then press the u key to undelete the previous deletion or modification; if you want to undo multiple previous modifications or deletes, press u more times. This is not much different from Word's undo operation;

3.8 visual mode;

In the latest Linux distributions, vi provides a visual mode, as this feature is only available in vim. If you use vi without this feature, it will be replaced by vim. Open the visual mode, press the ESC key, then press v to enter the visual mode;

The visual mode gives us an extremely friendly selection of text ranges to highlight; at the bottom of the screen is displayed;


-- Visual --




Entering 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 use of the text range?

We can delete the job for a certain part, and press d to delete the content we selected.
After selecting the content, we press y to indicate copying; press d to delete;

It is worth mentioning that while deleting, it also means copying. We return to command mode, then move the cursor to a location, then press shift+p to paste the content we just deleted. Let us first mention here, in the following, we have to talk in detail.

Exit visual mode, or use the ESC key;

3.9 copy and paste operations;

In fact, the deletion also has the meaning of cutting. When we delete the text, we can move the cursor to somewhere, then press shift+p to paste the content in place, then move the cursor to somewhere, then press p or Shift+p can be attached;


p past the cursor;
Shift+p paste before the cursor

To give an example:

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

There are two ways;

the first method:

First delete the third line, move the cursor to the third line, then use dd, then press shift+p. This will put the third line just deleted in the original place.

Then we use the k key to move the cursor to the fifth line, and then press the p key again, so that the content of the third line is posted to the back of the fifth line;

The second method;

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

Therefore, the copy and paste operation is a combination of command mode, insert mode and visual mode; we must learn to switch between modes, use ESC key; more importantly, learn to move the cursor in command mode;

3.10 on the line number;

Sometimes when we configure a program to run, there will be an error in the configuration file X line. At this time we need to use the line number related operations;

Add a line number for all content;

Press the ESC key and enter:


:set number

Where the cursor is

In the lower right corner of the screen, there are similarities as follows;


         57 27%

Among them, 57 indicates the 57th line, and 8 indicates the 8th character;

3.11 Find and replace functions;

3.11.1 lookup;

First, we have to enter the ESC key and enter the command mode; we enter / or ? to enter the search mode;


/SEARCH Note: For forward search, press n to move the cursor to the next eligible place;
?SEARCH Note: Reverse search, press shift+n to move the cursor to the next eligible condition

For example: I want to find the swap word in a file, I should do the following;

First press the ESC key to enter the command mode and then enter;



3.11.2 replacement;

Press the ESC key to enter the command mode;


:s /SEARCH/REPLACE/g Note: Replace the SEARCH word in the line where the current cursor is located with REPLACE and highlight all SEARCH;
:%s /SEARCH/REPLACE Note: Replace all SEARCH in the document with REPLACE;
:#,# s /SEARCH/REPLACE/g Note: The ## indicates a number indicating how many lines from how many lines, replacing SEARCH with REPLACE;

Note: In this, g means global lookup; we noticed that there is no replacement, it will also highlight SEARCH;

for example:

For example, we have a document to be modified;

We put the line where the cursor is, and replace all the words the with THE, which should be:


:s /the/THE/g

We replace all of the whole document with THE, which should be:


:%s /the/THE

We just replace the the 1st to the 10th line with THE, which should be;


:1,10 s /the/THE/g

4. About this article;

The purpose of writing this article is to let novices create, edit, and modify files with vi or vim in the shortest amount of time, so this document is not a large and comprehensive vi manual. If you have all the functions of vi, you have to write at least a thousand pages of manuals; this does not involve more advanced vi usage. If you want to know more, look for man and help;

5, postscript;

So far, I have written several articles about the operation of directories and files, from the creation, deletion, copying of files and directories to property operations, and finally to document modification. These documents are all related. If you link these related documents, it is a whole knowledge block. Only by mastering this knowledge can we manage the file system.

What will I write in the future? ? ? ? May add a lookup of the file, in fact, there are similar documents before, I will summarize it, and post it to everyone to see it.

The next step is to prepare the network basic document construction, the network foundation is more important, this is the next focus of my plan. . . . . . . .

6, reference documentation;

Man vi and vi --help

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.