After the last two days, let’s see a less brain melting, but related topic, how to remove lines with given pattern from a file.
It’s not something I use daily, but with rust code, I use it a lot. Around
tests, sometimes there are #[ignore
lines to tell the system to ignore this
test function. When I’m done I want to remove all of those from the test file,
and the easiest way to use vim.
The :global
command executed an Ex (:
commands) command on the lines within
the provided range that matches a pattern. In this case we want to run d
on
all lines that matches ^#\[ignore
, so the command will be :g/^#\[ignore/d
.
That d
is not a regular expression flag there, it’s a command.
But we can do it better, we don’t want those lines to be on our clipboard, and
luckily :delete
has an optional argument for the target register.
:[range]d[elete] [x] Delete [range] lines (default: current line)
[into register x].
That’s perfect, we can use the _
“spam” register to keep all our registers
clean. Our final command will be :g/^\[ignore/d _
.