Search This Blog

2013-09-10

UNIX: Running vi commands in script

Product: vi
Version: All

I was trying to search for ways to run vi scripted commands alike running the vi command manually, but most of the answer I found in Internet are to use shell scripts, awk, or sed. It seems like most people unaware that vi itself can run its command from a script.

So following is an example,

I have following file to do 4 search-replace vi commands

File: replace1.vi

:%s/:1/33807/g
:%s/:2/32/g
:%s/:3/33/g
:%s/:4/34/g

Let say I wanted to execute above vi script inside another SQL script to replace several bind variables

1. vi demo1.sql
2. Type ":so replace1.vi"
3. vi will execute above script and replace all 4 bind variables if found. In Linux, it will display warning message if it found nothing to replace

No comments: