Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pipe input to sublimetext on linux?

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?

like image 630
wim Avatar asked Jan 14 '14 22:01

wim


People also ask

How do I use Sublime Text in Linux?

To install Package Control, click Tools > Install Package Control. Sublime Text will now take a few seconds and automatically install it for you. Bring up Command Palette by pressing Ctrl + Shift + P, and then search "package control" in the search window.

How do I start sublime from terminal in Linux?

Type : subl in terminal to launch Sublime Text 3 from terminal.

How do I run Sublime Text from terminal 3?

Type 'Terminus' and select it. Wait for it to Complete installation and Restart sublime text. And save it. Note: The above code is for Linux users for Windows users you have to enter “cmd.exe” in place of “bash”, also here we kept the shortcut key as “alt+1” you can use your own key.


1 Answers

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

like image 173
tylerl Avatar answered Sep 29 '22 21:09

tylerl