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

What are the common skills of VI command

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

Share

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

This article mainly explains "what are the common skills of VI command". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what are the common skills of VI command.

The VI command can be said to be the most commonly used command for editing files in the Unix/Linux world, but because of its large command set, many people are not used to using it. In fact, you only need to master the basic command, and then use it flexibly, you will find its advantages, and will gradually like to use this method.

The purpose of this article is to introduce some of the most commonly used commands and advanced application skills of VI.

I. introduction of basic commands

-1. Cursor command

K, j, h, lmuri-up, down, left and right cursor movement commands. Although you can use the four cursor keys on the right side of the keyboard in Linux, it is useful to remember these four commands. These four keys are the basic position where the right hand is placed on the keyboard.

NG-- jump command. N is the number of lines, and the command immediately causes the cursor to jump to the specified line.

The number of rows and columns where the Ctrl+G-- cursor is located is reported.

W, BMY-causes the cursor to skip one word forward or backward.

-2. Editing command

I, a, r r=replace-insert character commands (i=insert, a=append, r=replace) before, after, and where the cursor is located.

Cw, dw-- commands to change (replace) / delete the word where the cursor is located (c=change, d=delete).

X, d$, dd-- deletes one character, deletes all characters from the cursor to the end of the line, and deletes the command for the entire line.

-3. Find command

A command that looks back or forward for the corresponding string from where the cursor is located-/ string,? string--.

-4. Copy copy command

-yy, pmure-commands that copy a line to the clipboard or take out the contents of the clipboard.

Second, common problems and application skills

Read the contents of / etc/passwd in a new file and take out the user name section.

-vi file

-: r / etc/passwd reads / etc/passwd at the cursor in the open file file

Remove all the parts of the user name in / etc/passwd from the beginning of the colon to the end of the line.

-you can also read the contents of the file after the specified line number, for example, use the command ": 3r / etc/passwd" to read all the contents of / etc/passwd from line 3 of the new file.

-We can also delete all blank lines and comment lines starting with # in the file using the following method.

-# cat squid.conf.default | grep-v ^ $| grep-v ^ #

After opening a file for editing, it is only known that the logged-in user does not have write permission to the file, cannot save the disk, and needs to save the changes to a temporary file.

-vi file

-you can also save some of these changes to a temporary file, for example, just save the contents between lines 20-59 as a file / tmp/1, we can type the following command.

-vi file

-: 2050 59w / tmp/1

Edit a file with VI, but delete large chunks of content.

-first use the editing command "vi file" to open the file, then move the cursor to the line you need to delete, press Ctrl+G to display the line number, and then press Ctrl+G at the end to display the line number at the end of the file.

-assuming that the line numbers obtained twice are 23 and 1045, then delete all the contents during this period, or mark it with the ma and mb commands in the start and end lines to be deleted, and then delete it using the ": a line bd" command.

-4. Add some strings at the beginning or end of each line or lines throughout the file.

-vi file

Insert "some string" at the beginning of the first line to the last line of the file.

-:% s/$/some string/g adds "some string" at the end of each line in the entire file.

-:% s/string1/string2/g replaces "string1" with "string2" throughout the file.

-replace only the words "string1" from lines 3 to 7 in the file into "string2".

-Note: where s is substitute,% for all lines and g for global.

Edit 2 files at the same time, copy the text from one file and paste it into another file.

-vi file1 file2

-yy copies the line at the cursor of file 1

-: n switch to file 2 (n=next)

-p paste the copied line at the cursor in file 2

--: n switch back to file 1

-6. Replace the path in the file.

-use the command ":% s#/usr/bin#/bin#g" to replace all paths / usr/bin in the file with / bin. You can also use the command ":% s//usr/bin//bin/g", where "" is an escape character, indicating that the subsequent "/" character is a meaningful character, not a delimiter

Thank you for your reading, these are the contents of "what are the common skills of VI commands?" after the study of this article, I believe you have a deeper understanding of what the common skills of VI commands are, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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