VIM delete command

1. Delete a single line

1.1 Delete the line where the current cursor is located

In normal mode , enter dd to delete the line where the cursor is located.

1.2 Delete a specified row

In command line mode , enter [num]d, for example, to delete line 5, enter 5d

1.2.1 Delete the last line

In command line mode , enter$d

2. Delete multiple lines

2.1 Delete n lines at the current position

In normal mode , before you use the dd command to delete a line, you can specify a number in front of it to delete several lines at a time in Vim.

[num]dd

When you specify a number instead of num, Vim will start deleting lines, which you can think of as Vim executing the dd command num multiple times.
For example, in normal mode, typing 5ddwill delete 5 lines after the current line (including the current line).

2.2 Specify the range of rows to be deleted

Characters used to specify a range (if you don’t understand, continue reading)

  • .current row
  • $last line in file
  • %all rows

Command to increase movement speed

  • ggMove to the first line of the buffer.
  • GMove to the last line of the buffer.

2.2.1 Delete mn line

In command line mode ,

:[begin],[end]d

As shown in the figure, enter to 3,5ddelete the data between row 3 and row 5

111111
222222
333333
444444
555555
666666
777777
888888
999999
~
~
:3,5d

After pressing Enter, the result is as follows

111111
222222
666666
777777
888888
999999
~                                                                     
~

2.2.2 Delete all lines before the current line

Command line mode, enter1,.-1d

2.2.3 Delete all lines after the current line

In command line mode, enter.+1,$d

3 Delete all rows

按一下ESC键,确保退出编辑模式
按 两次g键,即gg。             让光标移动到文本的首行
按 dG键。                     其中d小写,G大写

按一下ESC键,确保退出编辑模式
输入:1,$d

按一下ESC键,确保退出编辑模式
输入:%d   
%表示文件中的所有行。

4 Delete lines matching a specific pattern

Delete rows matching a specific pattern

:g/PATTERN/d

Delete lines that do not match the specified pattern

:g!/PATTERN/d

Breaking it down we get the following elements:

  • g - Search globally (i.e. the entire file)
  • !- reverse match
  • PATTERN - The pattern to match
  • d- Delete command

Example

  1. Delete lines containing text keyword:g/text/d
  2. Delete lines that do not contain the # keyword:g!/#/d
  3. Remove all empty lines:g/^$/d
  4. Delete comments starting with #:g/^#/d

Guess you like

Origin blog.csdn.net/weixin_37909391/article/details/129827445
Recommended