The method of deleting even-numbered lines is as follows:
:g/^/+1 d
The gbobal command is used above. The gbobal command format is as follows:
:[range]global/{pattern}/{command}
The global command is actually executed in two steps: first scan [range ] All lines within the specified range, mark the lines matching {pattern}; then execute the {command} command on the marked lines in sequence, if the marked lines are deleted or moved during the command operation on the previous matching lines or merge, its mark disappears automatically without executing the {command} command on the line. {command} can be an ex command, or multiple ex commands separated by |, so that we can perform a variety of different operations on the marked line, or the line addressed from the marked line.
This command will first match all lines, and then delete the even-numbered lines (use 1 to delete the next line of the current line). Why is it interlaced? Because when the 1d command is executed on the first line, the second line is deleted, and although the second line is also marked, it no longer exists, so the command to delete the third line will not be executed.
The command to delete several rows is as follows:
:g/^/d|m
The function of m is to remove the mark of even rows to prevent even rows from being deleted.
In addition, you can also use the normal command to delete even-numbered lines, as follows (the first command is the number of lines):
:%norm jkdd :%norm jdd
Linux is a UNIX-like operating system that is free to use and spread freely. It is a multi-user, multi-task, multi-thread and multi-CPU operating system based on POSIX. Linux can run major Unix tool software, applications and networks. protocol.
The above is the detailed content of How to use Vim to delete even or odd lines in Linux. For more information, please follow other related articles on the PHP Chinese website!