3

I have a shell command that provides output that I would like to edit before piping it into another command. This is not regular output that can easily be edited with sed and such. My current approach is:

command-one > tmp
vim tmp
command-two < tmp
rm tmp

I would like to avoid the unnecessary creation of a temporary file and instead do something like command-one | vim - | command-two but that doesn't work because the actual onscreen output of vim gets piped into the command instead of being visible for me to edit. This works in commands like git commit which wait for the temp file to be written before using the result.

6
  • pipes create tmp files, its just that you don't have to delete them when your pipeline completes. Make a script with your commands as listed in the first block and you'll be good to go. Good luck!
    – shellter
    Sep 30, 2013 at 15:31
  • This is just one example. I don't do the same scriptable action every time. I just find myself often needing this pattern, so it isn't something that I could put in a script and be done with.
    – Wade Tandy
    Sep 30, 2013 at 15:33
  • you'll get more respondents if you include a tag for the shell you're using (bash|ksh|csh|...). There might be some trick to get what you want, but I'll be surprized if 1. it exists, and 2 its not fragile. Good luck!.
    – shellter
    Sep 30, 2013 at 16:08
  • git commit does not sit in the middle of a pipeline. Rather it takes input, runs $EDITOR to produce a temporary file, then does something with that temporary file.
    – chepner
    Sep 30, 2013 at 17:24
  • Right, understood. Just using it as an example of the workflow, if not the exact thing.
    – Wade Tandy
    Sep 30, 2013 at 20:27

6 Answers 6

6

You could write a script with this code:

#!/bin/sh 
: ${EDITOR:=/usr/bin/vi} 
: ${TMPDIR:=/tmp} 

TMPFILE=${TMPDIR}/.edpipe$$ 

trap "trap '' 0 1 2 3 15; rm -f $TMPFILE; exit 0" 0 1 2 3 15 

cat >$TMPFILE 
$EDITOR ${1:+"$@"} $TMPFILE </dev/tty >/dev/tty 
cat $TMPFILE 
exit 0

Save the script as edpipe for example, and then call it in your pipeline like this

ls | edpipe | wc -l
3
  • This is one of the coolest answers I've ever seen on SO, feels like this should be a built in tool. Oct 3, 2019 at 0:20
  • This is an amazing answer! Just suggesting to add set -e to the top of the script, such that it can be aborted. E.g. with set -e, I can exit vim with :cq to abort the rest of the pipeline.
    – Tiddo
    Jun 15, 2023 at 13:15
  • set -e didn't abort the pipeline for me, it just passed an empty file on. This can be achieve by deleting the contents in vi eg dGZZ
    – Chubler_XL
    Jul 27, 2023 at 4:54
3

This is difficult as stated because, as you've pointed out, to fit into a pipeline like that the editor needs to display one thing to the user and output a different thing on stdout.

However, if the rest of the pipeline isn't too long/horrible, here's something you can do using vim:

$ vim <(first | part | of | pipeline)

This passes vim an argument which is a named pipe outputting the stdout of those commands. The effective result is that you get a buffer containing that output. Edit it, then, before exiting:

:w !rest | of | pipeline

which runs rest | of | pipeline in a shell and feeds the current buffer into its stdin. You'll see the output of the command within vim, and then you'll be able to :q! to discard the modified buffer and return to your shell.

3

There is a tool called vipe (https://linux.die.net/man/1/vipe) that is designed exactly for this purpose:

command | vipe | another-command
1

If you want to use some random editor (i.e. the value of $EDITOR), you'll need to use a temporary file. Most editors do not have the facility to accept an entire file on stdin and write it to stdout when the edit is finished.

See mktemp (or tempfile in the unlikely case that you don't have mktemp) for a safe way to create a temporary file.

0

Try this:

tmpfile=$(mktemp); command-one > $tmpfile && vim $tmpfile && command-two < $tmpfile && rm $tmpfile
0

In case anyone ends up here wanting the same solution for #powershell rather than #bash

function Choose () {
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string[]]
        $Text
    )
    begin {
        $file = New-TemporaryFile
        $Text
    }
    process 
    {
        foreach ($line in $Text){
            $line >> $file
        }
    }
    end{
        start-process nvim $file -wait
        Get-Content $file
        Remove-Item $file 
    }
}

and using it to edit the git sparse checkout info

function Git-Sparse-Checkout-Edit 
{
    git sparse-checkout list | Choose | git sparse-checkout set --stdin

}

The editor I'm using is nvim but should work with any editor. In case you wonder why start-process then I don't really know. The terminal would stall and become unresponsive if I didn't do it this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.