Global search (and replace) using MacVim?

49 views
Skip to first unread message

DK

unread,
Nov 20, 2010, 3:58:35 PM11/20/10
to vim_mac
Hi all, I am new to the group... Been trying out Macvim and I think I
have covered most of what I used in other editors except for how to
handle global searches and replaces for text in files (i.e. not
looking for file names, but search and replace text within all files
below a specific directory).

Can anyone give me some advice on what they use for such tasks? I have
been poking around and see :grep as the main method, although not sure
if I can use it to do a replace? If you can give me a few examples
specific to MacVim that would be great.

Thanks,

David

Kyle Lippincott

unread,
Nov 20, 2010, 6:14:27 PM11/20/10
to vim...@googlegroups.com
:set autowrite
:args **/*.txt
:argdo s/from/to/g

I think will do what you wish; if not exact syntax (I typed it from memory), it's close and should help you find the right thing to do in the help files. :)  This is going to be quite a bit slower than using a special-built tool (like sed on the commandline, I'm not sure if it's there on os x) since vim is going to open a buffer for each file and that has overhead.


--
You received this message from the "vim_mac" 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

Wincent Colaiuta

unread,
Nov 21, 2010, 4:41:27 AM11/21/10
to vim_mac
On 21 nov, 00:14, Kyle Lippincott <spect...@gmail.com> wrote:
> :set autowrite
> :args **/*.txt
> :argdo s/from/to/g
>
> I think will do what you wish; if not exact syntax (I typed it from memory),

Right, except you probably want:

:argdo %s/from/to/g

To replace in the entire file (note the %).

The variants I use are the following:

" this one to replace across all buffers, confirming each
replacement
:bufdo %s/pattern/substituion/gce | update

And:

" same, confirming each replacement
:args **/*.txt
:argdo %s/pattern/substitution/gce | update

Note the "update" means you don't need to set autowrite first, and the
"e" flag suppresses errors for any files which don't contain the
search pattern.

Based on:

http://vim.wikia.com/wiki/VimTip1512

Cheers,
Wincent
Reply all
Reply to author
Forward
0 new messages