Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of various Operations in vi/vim Command Mode and Editing Mode

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you the "vi/vim command mode and editing mode of various operations example analysis", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let the editor lead you to study and learn the "vi/vim command mode and editing mode of various operations example analysis" this article.

1. About the text editor

There are many text editors, such as gedit, kwrite, OpenOffice in graphic mode, vi, vim (an enhanced version of vi) and nano in text mode. Vi and vim are our most commonly used editors in Linux. It is necessary to introduce the simplest use of vi (vim) so that Linux entry-level users can learn to use it in the shortest possible time.

The nano tool is similar to the edit operation under the DOS operating system, and it is easy to use. We will not introduce it. If you are interested, you might as well give it a try.

2. Vi editor

Why learn to use vi simply

Vi or vim is the most basic text editing tool of Linux. Although vi or vim is not as simple as the mouse click operation of graphical interface editor, vi editor is never comparable to graphical interface editor 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. How to use the vi editor

3.1 how to call vi

[root@localhost ~] # vi filename

~

~

~

~

~

3.2 three command modes for vi

Command (command) mode for entering commands

Insert (insert) mode for inserting text

Visual (visual) mode for visual highlighting and text selection

3.3 Save and exit files

Command mode is the default mode for vi or vim, and if we are in another command mode, switch it over with the 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 save as filename

: wq! Save exit

: wq! Filename note: exit after saving under the file name of filename

: q! Do not save exit

: X should be saved and exited, function and: wq! The same

3.4 cursor movement

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

J move down one line

K move 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 move up

The down arrow moves down

Move left Arrow to left

Move the right arrow to the right

When we edit a file, for the j, k, l, and h keys, we can also precede these action commands with numbers, such as 3J, to indicate that we move down three lines.

3.5 insert mode (insertion of text)

I insert before the cursor

An insert after the cursor

I insert at the beginning of the line where the cursor is located

An insert at the end of the line in which the cursor is located

O insert a row above the line where the cursor is located

O insert a row below the line where the cursor is located

S delete a character after the cursor and enter insert mode

S deletes the line where the cursor is located and enters insert mode

3.6 deletion of text content

X one character

# x remove a few characters, and # represents a number, such as 3x

Dw deletes a word

# dw deletes several words, and # is expressed as a number, such as 3dw for deleting three words

Dd deletes a row

# dd deletes multiple lines. # represents a number. For example, 3dd deletes the cursor line and the next two lines of the cursor.

D $delete the contents of the cursor to the end of the line

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

3.7 resume modification and restore delete operation

U undo modify or delete operation

Press ESC to return to Command mode, and then press u to undelete previous deletions or modifications; if you want to undo multiple previous modifications or deletions, press u several times. This is not much different from the undo operation of Word.

3.8 Visual mode

In the latest Linux release, vi provides visual mode because this feature is unique to vim. If the vi you use doesn't have this feature, you can switch it to vim. Turn on visual mode, press ESC, and then press v to enter visual mode

Visual mode provides us with a very friendly range of selected text to highlight; at the bottom of the screen there are

-visible--

Or

-- VISUAL--

When we enter the visual mode, we can use the cursor movement instruction in the command line mode mentioned earlier to select the text range.

What is the use of selecting a text range?

We can delete some jobs and press d to delete what we selected.

When the content is selected, we press y to copy and d to delete

It is worth mentioning that while deleting, it also means replication. We return to command mode, move the cursor to a location, press the shift+ p key, and paste what we just deleted. First of all, let's mention it here, and we have to talk about it in detail later.

Exit visual mode or use the ESC key

3.9 copy and paste operation

In fact, deleting also means cutting. When we delete the text, we can move the cursor to a certain place, then press shift+p to paste the content in the same place, then move the cursor to a certain place, and then press p or shift+p to paste it again.

P paste behind the cursor

Shift+p pastes before the cursor

Let's give an example:

For example, if we want to copy down the third line of a document and post it after 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 the dd action, and then press the shift+p key. In this way, the third line just deleted will be posted in the same place.

Then we use the k key to move the cursor to the fifth line, and then press the p key, so that the contents of the third line are posted after the fifth line.

The second method

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

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

3.10 about line number

Sometimes when we configure a program to run, there will be an error on line X of the configuration file. At this point, we need to use the operation related to the line number.

Add line numbers to everything

Press the ESC key and enter:

: set number

Location of the cursor

In the lower right corner of the screen, there is something similar to the following

57% 8.27%

In this, 57 represents line 57 and 8 represents the eighth character

3.11 find and replace function

3.11.1 find

First of all, we need to enter the ESC key and enter command mode; shall we enter / or? Enter the search mode.

/ SEARCH Note: forward search, press the n key to move the cursor to the next place that meets the criteria

SEARCH Note: reverse search, press shift+n, and move the cursor to the next eligible

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

First press the ESC key, enter command mode, and then type

/ swap

Or

? swap

3.11.2 replacement

Press ESC to enter 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 # sign indicates the number from the number of lines to the number of lines, replacing SEARCH with REPLACE

Note: in this case, g indicates a global search; we notice that SEARCH is highlighted even if there is no replacement

Examples are as follows:

For example, we have a document to modify.

Let's change the line where the cursor is located and replace all the words the with THE, which should be:

: s / the/THE/g

We replace all the the in the entire document with THE, which should be:

:% s / the/THE

We just replace the the in lines 1 through 10 with THE, which should be

: 1,10 s / the/THE/g

4. About this article

My goal in writing this article is to allow beginners to create, edit, and modify files in vi or vim in the shortest possible 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-page manual; this one does not cover the more advanced usage of vi. If you want to know more, look for man and help

5. Postscript

So far, I have written several articles about directory and file operation, from file and directory creation, deletion, copy to attribute operation, and finally to file modification. These documents are relevant, if these related documents are linked together, it is a whole block of knowledge. Only by mastering this knowledge can we realize the management of the file system.

What will I write in the future? May supplement the search of the file, in fact, there were similar documents before, then I will summarize it and post it for you to have a look at.

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

The above is all the contents of the article "sample Analysis of various Operations in vi/vim Command Mode and Editing Mode". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report