Common VI usage skills

Source: Internet
Author: User
Common tips for using VI-general Linux technology-Linux programming and kernel information. For more information, see the following. I. Introduction

The Vi command is the most commonly used file editing command in the Unix/Linux world. Many people do not like VI because of its many command sets, but we only need to master the basic commands and use them flexibly. I believe you will like it as I do. This article aims to introduce some of VI's most common commands and some advanced applications in a more systematic and systematic manner.

II. Introduction to basic commands

1) cursor command
K, j, h, l -- move the cursor from top to bottom. Although you can use the four cursor keys on the right of the keyboard in Linux, it is very useful to remember these four commands, that is, the position of the right hand on the keyboard.
NG -- n indicates the number of rows. This command immediately redirects the cursor to the specified row.
Ctrl + G -- number of rows and columns at the cursor position
W, B -- let the cursor skip a word forward or backward

2) EDIT commands
I, a, r -- insert the character command above the cursor (I = insert, a = append, r = replace)
Cw, dw -- change (replace)/command for deleting the word where the cursor is located (c = change, d = delete)
X, d $, dd -- delete a character, all characters from the cursor position to the end of the line, and the whole line command

3) Search for commands
/String ,? String -- search for the corresponding string from the cursor to the forward

4) Copy command
Yy, p -- copy a row to the clipboard/command to retrieve the content in the clipboard

Iii. FAQs and application skills

1) Read/etc/passwd in a new file and retrieve the username section.
Vi file
: R/etc/passwd: Read/etc/passwd at the cursor position in the opened file
: % S/:. * // g delete all parts after the user name in/etc/passwd from the colon until the end of the line
: 3r/etc/passwd: Read the file content after the specified row number

Another method is to delete all empty lines in the file and comment lines starting #.
# Cat squid. conf. default | grep-v '^ $' | grep-v '^ #'

2) After opening a file and editing it, the logged-on user does not have the write permission for the file and cannot store the disk.
Vi file
: W/tmp/1 Since the disk cannot be stored, do not want to give up all the modifications, first temporarily save to/tmp/1
: 20th w/tmp/1 or simply store the content between and 59 rows into a file/tmp/1

3) Use VI to edit a file, but you need to delete the content of a large segment.
Vi file
Ctrl + G move the cursor to the row to be deleted, press ctrl + G to display the row number, and then press Ctrl + G at the end.
: 23,104 5d if the two row numbers are 23 and 1045, delete all the contents of these two columns.

You can also use ma In the start and end lines, mark the mb command, and delete it with 'a, 'bd.

4) add strings to the beginning or end of a row in the entire file or in a certain number of rows.
Vi file
: 3, $ s/^/some string/
Insert some string before the beginning of the first line to the last line of the file
: % S/$/some string/g add some string at the end of each row of the entire file
: % S/string1/string2/g replace string1 with string2 in the entire file
: S/string1/string2/replace string1 in the third to seventh rows of the file with string2
Note: s is substitute, % indicates all rows, g indicates global

5) EDIT two files at the same time and copy and paste the text in the two files.
Vi file1 file2
Yy simultaneously open two files and copy the row where the cursor of file 1 is located
: N switch to file 2 (n = next)
P paste the copied row at the cursor of file 2
: N switch back to file 1

6) Replace the path in the file
: % S #/usr/bin # g. replace all the paths/usr/bin in the file with/bin.
Or use
: % S/\/usr \/bin/g indicates '/' is a single character '/' with the \ symbol before '/'

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.