As far as I know, the only way to do that would be to first store the result of the pipe into a temporary file, then invoke subl
with the name of the temporary file, or something along those lines. You could automate that process with a script something like the following (this implies bash
, so it may not work in other shells without modification):
#!/bin/bash
set -e
# Is the last argument a single dash?
if [ "${@: -1}" == "-" ]
then
# Capture stdin to a temporary file
STDIN=$(mktemp -t stdin.XXXXXX)
cat > $STDIN
# remove the last argument
set -- "${@:1:$(($#-1))}"
fi
# Run subl now
subl $* $STDIN
The general idea is that we first check to see if the last argument on the command line is a solitary -
character (representing stdin
), and if it is then we use mktemp
to create a temporary file and store the value there, then remove that last argument from the arguments to the script. Once that’s done, we can invoke subl
with any arguments the script got, plus the name of the temporary file (presuming one exists).
If you alias
the script to subl
, you could use it as a drop-in replacement for standard subl
as well.
This could of course be more robust, such as allowing the -
to appear in any position, checking that the --
argument was given and then not doing this at all, in case you wanted to actually edit a file named -
or even more enhanced error handling than just “bail if anything at all goes wrong”.
Note that this will leave the temp file behind when you’re done, which may or may not be an issue. Possibly the most expedient end-run on that would be a plugin in Sublime that recognizes files whose names start with /tmp/stdin.
(as in this example) and trying to remove them when they’re closed or something along those lines.