Joe Smith
Joe Smith

Reputation: 1920

Is there a way to let vi run editing commands in a file?

Let's say I have a file "edit_commands" with some editing commands, like:

:1,$s/good/better/g
:1,$s/bad/worse/g

Is there a way to let vi load and run the commands in "edit_commands"?

Update: It appears the "-S" option can be used for this. Refer to: How to run a series of vim commands from command prompt

Upvotes: 6

Views: 124

Answers (2)

mattb
mattb

Reputation: 3053

It seems like you would want to use a program like sed which shares a common ancestor with vi (i.e. the 'ed' editor) for such a task:

sed -i 's/good/better/g; s/bad/worse/g' your_file

See this great sed tutorial.

Is there a reason you need to use vi to do it? You could use perl if you need more advanced regex capabilities.

Upvotes: 4

Aditya
Aditya

Reputation: 364

The solution in Perl may look this way:

perl -i.old -pe 's/good/better/g || s/bad/worse/g' your_file

The -i.old option saves a copy of your old file under the name your_file.old, what can be very useful when bad comes to worse and worse comes to worst...

Upvotes: 3

Related Questions