How to pipe input to sublimetext on linux?

wim picture wim · Jan 14, 2014 · Viewed 9.5k times · Source

How do I receive text from stdin into sublimetext editor? With vim it works like this:

echo "potato potato potato" | vim -

The same thing works with gedit, creating a new document with the contents.

Sublimetext seems to just start editing a file called "-" in a new tab, is there some other shell trick which could work?

Answer

tylerl picture tylerl · Jun 24, 2015

The simplest solution if your sublime installation doesn't support opening STDIN (e.g. current version on Linux) is to dump the output to a temporary file, open the file in sublime, and then delete the file. I created a script called tosubl in my bin directory as follows:

#!/bin/bash

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

Then you can use it by doing something like this:

$ ifconfig | tosubl