Categories
BLOG

sublime pipe

STDIN inputs via Sublime Text (sort of)

Jan 03, 2015

I love Sublime Text. I really do. I can put a ring on it if it had any corporeal form. I’ve been using it so much, that trying to work on anything else is kind of a pain. And yet, when dealing with STDIN inputs, the magic falters. This method describes a workaround to give inputs without a prompt.

Recently, I tried my hand on Competitive Programming, and though I didn’t get really good at it, I did encounter a frustation. Entering the same input again and again after every change I make to the code. I wanted a simpler method.

Here’s what a friend of mine came up with: Enter the input in comments.

gives the output:

I wrote up a quick and really dirty plugin to do it for Python, and though it was rather clunky, I had a rough idea of what I wanted, and how to do it. Pipes! Good ol’ pipes!

Now, what I had was a clunky implementation which just executes the following command:

I started jotting down a list of things I needed to add to the plugin. The first thing was to make the execution asynchronous. I couldn’t let the whole editor hang when a program’s under execution. Also, to be able to kill programs. And proper error reporting, platform independency, etc.

It turns out all these things were already implemented, in the default build system of Sublime Text itself. I decided to merge my plugin it.

Apart from things like input extraction, handling filenames, the behavious of things in Windows, etc, the main trick was changing

A few other fixes, and the whole thing was running smooth as butter. No more rapid switching between the Console and Sublime Text to execute a program.

The plugin has now been nicely packaged and uploaded. It can easily be installed via Package Control, and the source is available on Github.

Hi. I’m Sid. I write software code for a living.

Interested in humor, filmmaking, and a decentralised internet.

I’m currently available for hire. Feel free to reach out if you think we might work well together.
My previous employers are Tower Research Capital, CultureAlley and Smallcase.

STDIN inputs via Sublime Text (sort of) Jan 03, 2015 I love Sublime Text. I really do. I can put a ring on it if it had any corporeal form. I’ve been using it so much, that trying to work on

How to pipe input to sublimetext on linux?

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

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?

6 Answers 6

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:

Then you can use it by doing something like this:

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.

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). 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 sublimetext 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:

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

Piggybacking on https://stackoverflow.com/a/31035834/2429912 a bit, since for me it does 90% but not all of it:

Using a temporary file is a way that can be used with virtually any editor. Even better if the editor supports waiting until the file is closed( sublime -w for Sublime Text) you can actually edit it on the fly, which makes it more versatile. To do that you need to alter the script @tylerl provided – script named tosubl in your PATH:

Now running echo “hello” | tosubl > local.file would open the output of the first script in Sublime and once you have closed it, save it to local.file .

If you want to display colored text with ANSI escape sequences (a terminal buffer for example), you can install the package ANSIescape and use the following command:

F=$(mktemp); cat > $F; subl $F; subl –command ansi; sleep .5; rm -f $F

Well, it’s 2020 and sublime still can’t (won’t?) read from stdin. None of the answers in this thread were working satisfactorily for me due to the program I was having pass input to sublime.

In my case, I wanted to take advantage of the “paste current selection to program” capability of the kitty terminal emulator. That functionality takes the current selection in the terminal and pipes it into a program for you when you press the keyboard shortcut. For some reason, the developer of kitty decided to pass the selection as an argument rather than via stdin.

To remedy this, I wrote a small C script that can handle both scenarios. In order to run the script, you’ll need to install TCC. You may want to check your repo first. I know on debian you can install it with apt: sudo apt install tcc . Alternatively, you can compile the following code with GCC, just remove the first line.

The above script will read from stdin unless an argument is passed to the program. In either case, it simply reads the input ( stdin or argv[1] ) into a temporary file, opens the file with sublime, waits

250ms to give time for sublime to open the file, and deletes the temporary file.

You can copy the above code to a location in your path (like /usr/local/bin/subl_pipe). Make sure set the permissions: chmod 755 /usr/local/bin/subl_pipe .

Anyway, I hope this is useful to somebody else eventually. ^_^

How to pipe input to sublimetext on linux? How do I receive text from stdin into sublimetext editor? With vim it works like this: The same thing works with gedit, creating a new document with ]]>