Vi's common use skills

xiaoxiao2021-03-06  77

Vi's common use skills

This article is reproduced from: http://www.linuxforum.net

Author: Wu Ah Ting Jephe wu (2001-09-05 07:00:00)

One: Introduction:

The VI command can be said to be the most commonly used editing file in the UNIX / Linux world, many people don't like VI because it

Many of the orders, but we only need to master the basic commands and use it flexibly, I believe you will like me.

Sample is happy.

This article is intended to have some most common commands and some advanced applications that are summarized in summary.

2: Basic order introduction:

1) Code command

K, J, H, L ---, left and right cursor motion commands, although you can use the four cursor keys on the right side of the keyboard in Linux,

But remember that these four orders are also very useful, that is, the right hand placed on the keyboard.

Ng ---- n is the number of rows, which immediately jumps to the specified line immediately.

Ctrl g ----- Row and column number report in the location of the cursor

W, b ------ Skip the cursor forward or backward

2) Edit command

I, A, R ------- In front of the cursor, insert the character command (i = I = insert, a = append, r = replace)

CW, DW ------ Change (Replace) / Delete the word command (c = change, d = delete)

X, D $, DD ----- Delete a character, all characters of the cursor to the end of the line, and the command

3) Find commands

/ string,? string ----- Look for the corresponding string of the corresponding string after the point of the cursor

4) Copy copy command

YY, P ----- Copy a line to the clipboard / take out the content of the contents in the clipboard

Three: Frequently Asked Questions and Application Skills

1) Remove the user name part in a new file in a new file

vi file

: R / etc / passwd Reads in the cursor in the open file file in the cursor in ETC / Passwd

:% s /:.*// G Delete / etc / passwd User Name Back from the colon start until all parts of the end

: 3r / etc / passwd This is read in the file after the specified line number

Another way to delete all blank lines in the file and the notes started with #

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

2) After opening a file editor, you know that the user does not write right for the file, and cannot be stored.

vi file

: w / tmp / 1 Since there is no way to store, do not want to give up all the modifications made, temporarily save / TMP / 1

: 20, 59W / TMP / 1 or only the contents between the 20th to 59 lines into files / TMP / 1

3) Edit a file with VI, but you need to delete the content of the large segment

vi file

Ctrl g moves the cursor to the row you need to delete, press Ctrl G to display the line number, and then press Ctrl G at the end.

: 23,1045d assume that the two line numbers are 23 and 1045, the contents of these content are completely deleted

It is also possible to use the MA, the MB command mark after starting and ending two rows: 'a,' bd deletion.

4) Add some strings at the head or row at the entire file or a few lines.

vi file

: 3, $ S / ^ / Some String / Insert Some String before the first line of the file to the last line

:% S / $ / SOME STRING / G Take some string at the end of each line of the entire file

:% S / STRING1 / STRING2 / G Replace String1 in String2 in the entire file

: 3, 7S / String1 / String2 / String1 in the third to seven lines only in the file String2

Note: s Substitution,% means all rows, g represents Global

5) Edit two files at the same time, copy the clippers in two files

vi file1 file2

YY opens two files at the same time, copying in the cursor of the file 1: n Switch to file 2 (n = next)

P Paste the copy of the copy of the cursor at the cursor of the file 2

: N Switch back to file 1

6) Replace the path in the file

:% s # / usr / bin # / bin # g Replace all paths in the file / usr / bin

Use

:% s // usr / bin // bin / g In '/' before the symbol pointed out '/' is a true single character '/'

转载请注明原文地址:https://www.9cbs.com/read-93850.html

New Post(0)