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: Kevin Rodgers
Subject: Re: some vi equivalents please?
Date: Sat, 01 Nov 2008 07:32:58 -0600
User-agent: Thunderbird 2.0.0.17 (Macintosh/20080914)

Toby Cubitt wrote:
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.

Good advice!

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

Since rustom is used to vi's noninteractive way of using sed-like
commands:

C-M-h   ; mark-whole-buffer
C-u M-| ; shell-command-on-region
sed '/<pat>/d'

C-M-h   ; mark-whole-buffer
C-u M-| ; shell-command-on-region
sed 's/^\([^:]*\):\([^:]*\):.*$/\1 \2/' ; note the trailing /

--
Kevin Rodgers
Denver, Colorado, USA





reply via email to

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