Monday, October 8, 2007

Run vim without being there...

Say you have a text file. You need to alter it in some regular way before sending it on somewhere else. Instead of editing by hand, there is a neat option you can use to edit the file with familiar vim commands (instead of awk's lovely syntax), without having to open vim interactively.

The principle is simple: You make a file containing each of the commands you want to run, one per line. They will be the same form as if you typed them in a vim session, starting with ":". Make sure that ":wq" is the last one. Then you run vim with:
Example case. My file to edit is tester.txt, contents being:,,,,,
Then I have a commands.txt file containing:
Finally I ran it as vim -s commands.txt tester.txt. As a result, cat tester.txt:
You could put something like this in a cron job, making transitions between data manipulation steps quick and easy, without having to mess with the files by hand each time.


Blogger Non said...

Very nicely the $64 question is, how do you tell VI not to display any output while running these scripts?

I can redirect output to another terminal, but it still wants to write to stdout.

September 29, 2008 at 8:59 AM  
Blogger by Immortal Curiosity said...

I tried redirecting stdout to /dev/null, and a few other permutations, always ended up getting "Vim: Warning: Output is not to a terminal". Not sure how to get around that yet.

I think you could use the script command ( ), and redirect output to another display, then close it in your original script, without the originating shell ever being the wiser.

September 29, 2008 at 9:24 AM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home