Friday, December 19, 2014

Re: How to use bufdo?

[copying the mailing list as you seem to have just replied to me
personally -- my own knowledge is limited, so it's good to tap the
communal brain in case you stump me]

On 2014-12-19 09:03, Ven Tadipatri wrote:
> Hmm...I probably should have explained my scenario more clearly.
> I have 2 buffer windows open that I have not saved to a file and
> wanted to do a "grep -v" rather than a grep.

Ah, I can only give you answers to the questions you ask. ;-)

For this scenario, I'd use

:set hidden
:bufdo v/pattern/d

then, if everything looked copacetic, I would issue

:wa

to write all the modified buffers. If not, I believe that issuing

:bufdo u

will undo all the changes in each buffer (there might be weirdness if
you'd modified a buffer prior to issuing the "bufdo" command, and
then the bufdo command didn't make any changes in that buffer, it
would undo your prior change instead of your filtering change)

> It seems like the bufdo g/somestring/ command prints out the results
> in a new buffer, which is not what I want. I tried it with the p
> and # options, and it always opens up a new buffer rather than
> replacing the contents of the buffer.

It's not quite its own buffer, but rather the output stream. You can
use any Ex command (":help ex-cmd-index"), so instead of using
"#" (print the line with the line-number) or "p" (just print the
line), my command above (in this reply) uses "d" to delete the line.
Note that it uses ":v" instead of ":g" which performs the action on
lines that *don't* match the pattern, akin to "grep -v".

Other Ex commands work just fine ("m" to move the line elsewhere in
the file, "t" to copy the line elsewhere in the file, ">" to indent
or "<" to dedent the line, etc). This is a vi/vim power-tool :-D

> Actually, playing around with the g command, it looks you can give
> it a d option to delete all matching lines.

Had I read your message all the way through before replying, I would
have gotten this far and noticed that you'd already tinkered with
this. Note that #/p/d are not options, but Ex commands.

I hope this makes your future editing sessions all the easier!

-tim






--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment