Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

366
Views
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?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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/myoutput
sublimetext /tmp/myoutput
over 4 years ago · Santiago Trujillo Report

0

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

echo 'potato potato potato' | vipe
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!