Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a bash script that launches vim abort being in a pipeline?

Tags:

bash

vim

I have a bash script that launches vim for me, after it reads some input from the command line.

I use it to edit scripts I have somewhere in the path, easily. It does some other things too, but the simplest form of it is the following:

vibin.sh

#!/usr/bin/env bash
PROG=$(which "$1")
vim "$PROG"

Often I'll be using this to edit something I recently ran, to make a quick adjustement. For example, if I ran

$ my_script.sh a b c d

I might go back in the history, then insert vibin.sh at the start of the line to edit it.

$ vibin.sh myscript.sh a b c d

This works fine, but fails if my previous command was being piped to something, e.g.

$ vibin.sh myscript.sh a b c d | tee /tmp/out

Is there a way to make my script abort being in a pipeline, and allow this to work correctly? Currently vim gets into a weird state when I do this, which I can exit, but I'd prefer a better solution

Currently I can detect if I'm running in a pipeline, and abort, but i'd like to actually have it do what I wanted - edit the script!

# ensure we're not in a pipeline
if [ ! -t 1 ] ; then 
  exit 1;
fi
like image 458
Brad Parks Avatar asked Jan 24 '26 05:01

Brad Parks


1 Answers

Try

vim "$PROG" < /dev/tty > /dev/tty

That is, force redirect standard input/output from/to the current console. A workaround against pipe/redirects.

like image 100
phd Avatar answered Jan 25 '26 23:01

phd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!