Skip to content

Instantly share code, notes, and snippets.

@nietonfir
Last active November 24, 2020 15:20
Show Gist options
  • Save nietonfir/8ae3c323cb6e339301bbce1fc78f00ac to your computer and use it in GitHub Desktop.
Save nietonfir/8ae3c323cb6e339301bbce1fc78f00ac to your computer and use it in GitHub Desktop.
Allow piping to Sublime Text on Linux. Doesn't interfere with normal use.
#!/bin/bash
# Allow piping to Sublime Text. Doesn't interfere with normal use.
subl=/usr/bin/subl
# Is stdin a terminal?
if test -t 0; then
# Stdin is a terminal.
# Open sublime normally.
$subl "$@"
else
# See https://stackoverflow.com/a/31035834/838733
TMPDIR=${TMPDIR:-/tmp} # default to /tmp if TMPDIR isn't set
F=$(mktemp $TMPDIR/subl-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
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment