How to pipe input to sublimetext on linux? How to pipe input to sublimetext on linux? linux linux

How to pipe input to sublimetext on linux?


Assuming Sublime still doesn't support opening STDIN, a straightforward solution is to dump the output to a temporary file, open the file in sublime, and then delete the file. Say you create a script called tosubl in your bin directory as follows:

#!/bin/bashTMPDIR=${TMPDIR:-/tmp}  # default to /tmp if TMPDIR isn't setF=$(mktemp $TMPDIR/tosubl-XXXXXXXX)cat >| $F  # use >| instead of > if you set noclobber in bashsubl $Fsleep .3  # give subl a little time to open the filerm -f $F  # file will be deleted as soon as subl closes it

Then you could use it by doing something like this:

$ ps | tosubl

But a more efficient and reliable solution would be to use to use Process Substitution if your shell supports it (it probably does). This does away with the temporary files. Here it is with bash:

tosubl() {    subl <(cat)}echo "foo" | tosubl

I'm pretty sure you can somehow remove the use of cat in that function where it's redundantly shoveling bits from stdin to stdout, but the syntax to do so in that context eludes me at the moment.


I don't know Sublime Text, but your problem should be generic in that it applies to any program that does accept a filename as argument, but refuses to read from stdin.

Fortunately, Bash allows you to pipe stdout from one process into some kind of temporary file, then pass the name of that file to another process.

From man bash:

Process substitution is supported on systems that support named pipes (FIFOs) or the /dev/fd method of naming open files. It takes the form of <(list) or >(list). The process list is run with its input or output connected to a FIFO or some file in /dev/fd. The name of this file is passed as an argument to the current command as the result of the expansion. If the >(list) form is used, writing to the file will provide input for list. If the <(list) form is used, the file passed as an argument should be read to obtain the output of list.

Assuming SomeProcess produces output that you would like to capture in your editor:

sublimetext <(SomeProcess)

or:

SomeProcess | sublimetext <(cat)

If you think you will be typing this in by hand a lot, then you may want to put sublimetext <(cat) into a shell script or alias.

Just in case your OS does not support process substitution, then you can always specify a temporary file yourself of course:

SomeProcess > /tmp/myoutputsublimetext /tmp/myoutput


You might find vipe from moreutils useful. If you've set EDITOR='subl --wait', you can simply:

echo 'potato potato potato' | vipe