Home > Operation and Maintenance > Linux Operation and Maintenance > How to use Vim to delete even or odd lines in Linux

How to use Vim to delete even or odd lines in Linux

WBOY
Release: 2023-05-22 17:07:22
forward
2052 people have browsed it

The method of deleting even-numbered lines is as follows:

:g/^/+1 d
Copy after login

The gbobal command is used above. The gbobal command format is as follows:

:[range]global/{pattern}/{command}
Copy after login

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
Copy after login

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
Copy after login

What is a Linux system

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template