Reputation: 3433
I have a text file with multiple lines such as:
amanda: foo
robert: bla
amanda: bar
peter: da
I'd like to remove all lines with amanda
. I use ctrl-s
and kill each line individually. Is possible to remove all lines at once?
Upvotes: 2
Views: 603
Reputation: 18055
Although @Marcos's answer is idiomatic one (you should use it), let's explore other variants. Let's say you have a text buffer and you want to delete a line containing li
in it:
Vladimir
Ilich
Ulyanov
Remember, that ^
matches beginning of line and $
end of line in regex. $
doesn't touch the newline character after the line, so replace with regex ^.*li.*$
will produce an empty line, as per @ataylor's answer:
Vladimir
Ulyanov
For some reason it's impossible to match before ^
and after $
in regex, therefore \s-^.*li.*$
nor ^.*li.*$\s-
won't work. Note, \s-
matches any whitespace character, (i.e. space, tab, newline and carriage return), so intuitively the regexes should've deleted the newline too, as newline is the only possible whitespace character before ^
or after $
. To match exactly newline, you should enter it verbatim, C-q C-j by default. Emacs frequently denotes the newline in separate font color as ^J
, it's not a sequence of ^
and J
, but a single character, please pay attention.
Therefore to delete a line containing li
, you could run command query-replace-regexp
on string ^.*li.*^J
, where ^J
is newline:
Vladimir
Ulyanov
Upvotes: 1
Reputation: 66069
One way is to use query-replace-regexp
with a regular expression of ^.*amanda.*$
to an empty string.
Upvotes: 1
Reputation: 3433
M-x delete-matching-lines
. It's possible to use regular expression.
Upvotes: 6