How to use vim as sed?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to use vim as sed?

Peng Yu
Hi,

Suppose I need to regular expression replacement for a bunch of files.
Of cause, I can use sed. However, it seems that sed is not fully
compatible with vim. I'm more familiar with vim. I know the vim
commands that I should use to do the replacements.

I'm wondering if there is any automatic way (say, from command line or
shell script) to process these files.

Thanks,
Peng
Reply | Threaded
Open this post in threaded view
|

Re: How to use vim as sed?

Bill McCarthy
On Sat 21-Oct-06 8:37pm -0600, Peng Yu wrote:

> Suppose I need to regular expression replacement for a bunch of files.
> Of cause, I can use sed. However, it seems that sed is not fully
> compatible with vim. I'm more familiar with vim. I know the vim
> commands that I should use to do the replacements.
>
> I'm wondering if there is any automatic way (say, from command line or
> shell script) to process these files.

From the command line, you could use something like the
following to replace all occurrences of 'foo' with 'bar' in
the current directory files named *.fb:

For speed, using a standard startup, not in vi mode, in ex
batch mode with undo off and no swap files:

  vim -u NONE -i NONE -Nnes "+se ul=-1" "+argdo %s/foo/bar/ge|up" "+q" *.fb

To use regular settings:

  vim -nes "+se ul=-1" "+argdo %s/foo/bar/ge|up" "+q" *.fb

I've written these so everything in the second pair of
quotes is the variable part where you substitute the action
you want (as is the file selection part) - easy to turn into
a shell script.

BTW, if your shell is 4nt, replace "%s" with "%%s" - so that
%s isn't treated as a variable.  This is a good practice for
CMD users also, even though not necessary for this example.

--
Best regards,
Bill