Sublime Forum

Standard In (stdin) from CLI


According to the OSX/MacOS docs for Sublime Text 3, receiving input from stdin is supported:

echo “Hello, World” | subl -

This same behavior does not seem to exist in Linux. Any ideas on how pipe text from stdin to Sublime Text on Linux?



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):


set -e

# Is the last argument a single dash?
if [ "${@: -1}" == "-" ]
    # Capture stdin to a temporary file
    STDIN=$(mktemp -t stdin.XXXXXX)
    cat > $STDIN

    # remove the last argument
    set -- "${@:1:$(($#-1))}"

# 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.

1 Like


Excellent! It’s somewhat unfortunate that it’s not baked into the CLI, but nothing that some bash scripting can’t overcome.

I think I might run a background sleep for a few seconds and then remove the generated temp file while sublime is running in the foreground.