unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* how to %g/notice/d
@ 2004-07-16  8:39 Chang PilHun
  2004-07-16  9:10 ` Damien Wyart
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Chang PilHun @ 2004-07-16  8:39 UTC (permalink / raw)


I want to delete all lines that includes 'notice' string.
in vim, I can do that by :%g/notice/d
How to in emacs?

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: how to %g/notice/d
  2004-07-16  8:39 how to %g/notice/d Chang PilHun
@ 2004-07-16  9:10 ` Damien Wyart
  2004-07-16 13:09 ` kgold
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: Damien Wyart @ 2004-07-16  9:10 UTC (permalink / raw)


* "Chang PilHun" <cph@nhncorp.com> in gnu.emacs.help:
> I want to delete all lines that includes 'notice' string.
> in vim, I can do that by :%g/notice/d
> How to in emacs?

The flush-lines function can be used for this purpose. See C-h f
flush-lines for more details.

-- 
DW

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: how to %g/notice/d
  2004-07-16  8:39 how to %g/notice/d Chang PilHun
  2004-07-16  9:10 ` Damien Wyart
@ 2004-07-16 13:09 ` kgold
  2004-07-16 16:45 ` Kevin Rodgers
  2004-07-16 17:16 ` nick
  3 siblings, 0 replies; 7+ messages in thread
From: kgold @ 2004-07-16 13:09 UTC (permalink / raw)


"Chang PilHun" <cph@nhncorp.com> writes:
> I want to delete all lines that includes 'notice' string.
> in vim, I can do that by :%g/notice/d
> How to in emacs?

Besides the "right" way to do it, I use keyboard macros all the time
for tasks like this, where it might take longer to find the right
command than to do the edit.

The keyboard macro would be:

- search for "notice"
- go to the beginning of the line
- kill the line

Issue a command to execute the macro 1000 times and you're done.

If you don't know about keyboard macros, you're missing a great emacs
feature.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: how to %g/notice/d
  2004-07-16  8:39 how to %g/notice/d Chang PilHun
  2004-07-16  9:10 ` Damien Wyart
  2004-07-16 13:09 ` kgold
@ 2004-07-16 16:45 ` Kevin Rodgers
  2004-07-16 17:16 ` nick
  3 siblings, 0 replies; 7+ messages in thread
From: Kevin Rodgers @ 2004-07-16 16:45 UTC (permalink / raw)


Chang PilHun wrote:
 > I want to delete all lines that includes 'notice' string.
 > in vim, I can do that by :%g/notice/d
 >
 > How to in emacs?

What could be more obvious than `M-x delete-matching-lines'?

Since vim/vi users love the sed syntax, you can always run sed on your
Emacs buffer like this:

C-x h	; mark-whole-buffer
C-u M-|	; [replace region with output from] shell-command-on-region
sed '...' RET	; e.g. /notice/d

-- 
Kevin Rodgers

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: how to %g/notice/d
  2004-07-16  8:39 how to %g/notice/d Chang PilHun
                   ` (2 preceding siblings ...)
  2004-07-16 16:45 ` Kevin Rodgers
@ 2004-07-16 17:16 ` nick
  2004-07-16 18:33   ` Kevin Rodgers
  2004-07-16 18:36   ` Peter Lee
  3 siblings, 2 replies; 7+ messages in thread
From: nick @ 2004-07-16 17:16 UTC (permalink / raw)


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)

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: how to %g/notice/d
  2004-07-16 17:16 ` nick
@ 2004-07-16 18:33   ` Kevin Rodgers
  2004-07-16 18:36   ` Peter Lee
  1 sibling, 0 replies; 7+ messages in thread
From: Kevin Rodgers @ 2004-07-16 18:33 UTC (permalink / raw)


nick wrote:
 > 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.

Don't guess what N is, just use 0.  (I've always thought 0 was a poor
choice for `C-x e's special prefix arg.  0 should mean execute 0 times; a
negative number should mean execute until error.)

...

 > 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)

mark-whole-buffer is bound by default to `C-x h'.

...

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

That should be: C-u M-| sed '/notice/d' RET

-- 
Kevin Rodgers

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: how to %g/notice/d
  2004-07-16 17:16 ` nick
  2004-07-16 18:33   ` Kevin Rodgers
@ 2004-07-16 18:36   ` Peter Lee
  1 sibling, 0 replies; 7+ messages in thread
From: Peter Lee @ 2004-07-16 18:36 UTC (permalink / raw)


>>>> nick  writes:

    nick> That is the long explanation. Here is the sequence of
    nick> operations: M-x mark-whole-buffer RET

Just as a side note.. the default binding for mark-whole-buffer is
'C-x h' if you don't want to rebind it.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2004-07-16 18:36 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-07-16  8:39 how to %g/notice/d Chang PilHun
2004-07-16  9:10 ` Damien Wyart
2004-07-16 13:09 ` kgold
2004-07-16 16:45 ` Kevin Rodgers
2004-07-16 17:16 ` nick
2004-07-16 18:33   ` Kevin Rodgers
2004-07-16 18:36   ` Peter Lee

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).