help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: some vi equivalents please?


From: Toby Cubitt
Subject: Re: some vi equivalents please?
Date: Fri, 31 Oct 2008 16:01:01 +0100
User-agent: Thunderbird 2.0.0.17 (X11/20080929)

rustom wrote:
> 1. In vi I can delete a line that contains a <pat> with
> :g/<pat>/d
> 
> How is this done in emacs?

  M-x query-replace-regexp

then enter "^.*<pat>.*$" as the regexp (no quotes), and leave the
replacement blank.


> 2. match-variables:
> If I want to remove everything after the second : (in a file that has
> 3 fields separated with two :'s)
> I do
> :s/\(.*\):\(.*\):.*/\1 \2
> How do I do that in emacs?

  M-x query-replace-regexp

then "^[^:]*\(.*\):\(.*\):.*$" for the regexp, and "\1 \2" for the
replacement (again, no quotes).

The "^" and "$" both here and above are only there to prevent the regexp
matching across lines (unlike sed, regexp searches in an Emacs buffer
aren't line-oriented). Depending on your file, you might get away with
something simpler. E.g. for 2, if you know there will always be two
colons on every line, you could use "\(.*?\):\(.*?\):.*" instead ("?"
makes a wildcard non-greedy).

query-replace-regexp is interactive, like query-replace. I find this
useful just to check I've got the regexps right, and then hit "!" once
I'm happy with it to replace all the rest non-interactively. The
non-interactive version is replace-regexp.

There are no doubt many other ways to accomplish this in Emacs...

HTH,

Toby




reply via email to

[Prev in Thread] Current Thread [Next in Thread]