Multiline delete and copy in Vim

Multiline delete and copy in Vim

Law one:

Single row deletion
: 1 (row to be deleted) d
Multi row deletion
: 1,10d

Law two:

The line where the cursor is
dd
N lines below the line where the cursor is located
Ndd

Law three:

Put the cursor on the 6th line
Input : 2yy Put
the cursor on the 9th line
Input : p
This method is suitable for copying a few lines of text, copy the 2 lines of data below the 6th line (including), and put it below the 9th line.

Law four:

In command line mode, enter
6,9 co 12
to copy the content between lines 6 to 9 to the back of line 12.

Law five:

Sometimes when you don't want to look at how many lines or copy a lot of lines, you can use labels instead. Move the
cursor to the start line, enter ma
to move the cursor to the end line, enter mb
to move the cursor to the paste line, enter mc
and then: 'a,'b co 'c Change co to m and it will be cut.
To delete multiple lines, you can use: 5, 9 de

VIM common commands

1) Cursor command

k,j,h,l - 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, but it is still very useful to remember these four commands, that is, where the right hand is placed on the keyboard Part
nG --n is the number of lines, this command immediately jumps the cursor to the specified line.
Ctrl+G - Report the number of rows and columns where the cursor is located
w,b - Skip the cursor forward or backward one word

2) Edit command

i, a, r ——--Insert character command before, after, and above the cursor (i=insert, a=append, r=replace)
cw, dw—— Command to change (replace)/delete the word where the cursor is located (c=change,d=delete)
x,d$,dd ——–deletes a character, all characters from the cursor position to the end of the line, and the command of the entire line

3) Find command

/string, ?string - command to find the corresponding string backward/forward from the cursor position

4) Copy copy command

yy,p —– command to copy a line to the clipboard/remove the contents of the clipboard

Common problems and application skills
1) Read the contents of /etc/passwd in a new file, take out the username part
vi file
:r /etc/passwd Read /etc/passwd at the cursor position in the opened file file
:% s/:.*//g Delete all parts from the colon to the end of the line after the username in
/etc/passwd: 3r /etc/passwd This is to read the contents of the file after the specified line number.
Another way to delete Remove all blank lines and comment lines starting with # in the file#
cat squid.conf.default | grep -v '^$' | grep -v '^#'

2) After opening a file for editing, it is known that the logged-in user does not have the right to write the file and cannot save
vi file
:w /tmp/1 Since there is no way to save the file, I don’t want to give up all the modifications made, and temporarily save it to /tmp/ 1
:20,59w /tmp/1 or just save the content between lines 20 to 59 as file /tmp/1
3) Edit a file with VI, but need to delete large sections of content
vi file
Ctrl+G Move the cursor to the line to be deleted and press ctrl+G to display the line number, and then press Ctrl+G again at the end.
:23,1045d Assuming that the two line numbers are 23 and 1045, then the contents of these spaces are all delete

You can also use the ma, mb command to mark the beginning and the end of the two lines and delete them with: 'a, 'bd.
4) Add some strings at the beginning or end of the line in the whole file or some lines
vi file
: 3, $ s/^/some string / Insert some string before the beginning of the first line to the last line in the file
:%s/$/ some string/g Add some string at the end of each line in the entire file

:%s/string1/string2/g replace string1 with string2 in the whole file
:3,7s/string1/string2/ replace string1 with string2 only in the third to seventh lines in the file

Note: s is a substitute, % means all lines, g means global

5) Edit two files at the same time, copy the clipping text in the two files
vi file1 file2
yy Open two files at the same time, copy the line where the cursor of file 1 is
: n switch to file 2 (n=next)
p in the file 2 paste the copied line where the cursor is
: N switch back to file 1
6) replace the path in the file

:%s#/usr/bin#/bin#g Replace all paths /usr/bin in the file with /bin
or use
:%s//usr/bin//bin/g to indicate with a symbol before '/' /' is really a single character '/'
7) Multi-line comment with vi
If you want to comment a multi-line program, a stupid way is to insert #, then use j to jump to the next line with the . command, repeating the previous command. If you want to comment out a few hundred lines, this approach is probably too stupid. A clever way is:
:.,+499 s/^/#/g

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325599977&siteId=291194637