How do I launch an editor from a shell script? How do I launch an editor from a shell script? shell shell

How do I launch an editor from a shell script?


I answered my own question! You have to redirect terminal input and output:

#!/bin/tcshvi my_file < `tty` > `tty`


The reason you're getting the error is that when you start a shell in your environment, it's starting in a subshell that has STDIN and STDOUT not connected to a TTY — probably because this is in something like a pipeline. When you redirect, you're opening a new connection directly to the device. So, for example, your command line turns

$ vi < `tty` > `tty`

into

$ vi < /dev/ttys000 > /dev/ttys000

So you're not really using your old STDIN/STDOUT, you're creating two new files and mapping them to your vi process's STDIN/STDOUT.

Now, tell us what you're doing with this and we'll tell you how to avoid this kludge.


I wanted to do something similar. I wanted an alias that would find the last file I was working on, and open it in vi(1) for editing. Anyway, I couldn't figure out how to do it as a readable alias (in tcsh) so I just created an ugly shell script (csh because I'm old) instead:

#!/bin/cshset DIR = "~/www/TooMuchRock/shows/"set file = $DIR`ls -t $DIR | head -1`set tty = `tty`vi $file <$tty >$tty

(1) kraftwerk:bin> which vivi: aliased to /usr/local/bin/vim -u ~/.exrc