To remove all lines containing a particular
string in the vi or
Vim
text editors, you can use the g
command to globally search for the
specified string and then, by putting a "d" at the end of the command line,
specify that you want all lines containing the specified string deleted. E.g.,
If I wanted to remove all lines containing the string "dog", I could use the
following command.
:g/dog/d
That command would also remove any lines containing "dogs", "dogged", etc.
If I just wanted to remove lines containing "dog", I could use
:g/dog /d
.
You can, of course, specify the pattern on which you wish to search using regular expressions. E.g., if I wanted to remove any lines containing either "dog" or "hog", I could use the command below.
g/[dh]og/d
By putting the leters "d" and "h" within brackets, I indicate to vi that it should remove any line that has either a "d" or an "h" followed by "og".
[ More Info ]