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

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

Re: how to %g/notice/d


From: nick
Subject: Re: how to %g/notice/d
Date: Fri, 16 Jul 2004 17:16:24 GMT
User-agent: Gnus/5.1002 (Gnus v5.10.2) Emacs/21.3 (gnu/linux)

There are many ways:

o you can write a lisp function to do it - that is a good
exercise if you want to learn emacs lisp, but of course,
it will take a while to write and debug the function, so
it is not really an option if you want something quickly.

o you can write a macro (as kgold suggested) that remembers the
keystrokes you type while you are doing *one* deletion, go to the top
of the buffer and then invoke the macro N times (with a prefix
argument of course), where N is some number greater or equal to the
number of occurrences of the word ``notice'' in your file.

o if you are on Unix, you can use standard Unix tools - let me expand
on this last one:

There is a function in emacs called ``shell-command-on-region''.  It
is bound by default to the key M-| (meta verticalbar).  The idea is to
mark the whole buffer as the region and then use this function to
invoke an arbitrary shell command on it. It is as if (and the notation
is designed to encourage that notion) you pipe the buffer through the
command. The only remaining problem is to make sure that the output of
the command replaces the contents of the region (by default, a temp
buffer is created to hold the output of the command). Having the
output replace the contents of the region can be done by giving a
prefix argument to this command. You can give a prefix argument to a
command by hitting C-u before the command.

That is the long explanation. Here is the sequence of operations:

M-x mark-whole-buffer RET
C-u M-| sed '/notice/d' RET

In my case, I have bound the mark-whole-buffer function to a function
key, since I use it very often, so the resulting operation is shorter.
In my .emacs, I have

(define-key global-map [f9] 'mark-whole-buffer)

which makes the <f9> function key invoke this function (there are
better ways to write this for compatibility reasons, but this works
for me, so I have not bothered to improve it). So I do

<f9> C-u M-| sed '/notice/d' RET

If you want to localize the transformation to a *part* of the
buffer, all you have to do is mark the region that you want to
transform, instead of marking the whole buffer:

Go to the beginning of the region, and mark it with C-SPC (or C-@
if C-<SPC> does not work on your machine). Go the end of the region.
Say

C-u M-| sed 'notice/d' RET

As you can see, it is much harder to describe than to do, but if you
understand the basic idea, then the power of the mechanism becomes
obvious. Note that you can put an arbitrary shell command in place of
the sed, which allows you to do all sorts of transformations on the
input.

Hope this helps.

-- 
nick  (nicholas dot dokos at hp dot com)


reply via email to

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