Sublime Forum

Filter (selected) text through (external) command?

#1

Hi! Can I somehow pipe (selected) text through an external filter/command? I’m still unhappy with the way the internal wrapping command (alt-q) works but I’ve got a simple (cygwin) shell script [1] which does exactly what I want/need via par (and basically mimics the way TextMate does this) — works very well with jEdit.

Thanks!

Chris

[1]

#!/bin/sh
PARINIT=“rTbgqR B=.,?!_A_a Q=_s>|+” /usr/bin/par

0 Likes

#2

I don’t know if there’s an out-of-the-box way to do this, but here’s a simple plugin to run an external program: http://pastie.org/private/bclbdgxzbkb1gs2jfqzehg

You would then put something like this in your user keymap:

<binding key="alt+q" command="runExternal c:/cygwin/bin/bash.exe /usr/bin/par -someargs"/>
0 Likes

#3

Looks promising! I’ll have to try that. Thank you! :smile:

0 Likes

#4

Almost works …

Traceback (most recent call last): File ".\sublimeplugin.py", line 119, in execTextCommand File ".\runExternal.py", line 26, in run output, error = p.communicate(view.substr(region)) File ".\subprocess.py", line 668, in communicate File ".\subprocess.py", line 885, in _communicate UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 126: ordinal not in range(128) Traceback (most recent call last): File ".\sublimeplugin.py", line 119, in execTextCommand File ".\runExternal.py", line 26, in run output, error = p.communicate(view.substr(region)) File ".\subprocess.py", line 668, in communicate File ".\subprocess.py", line 885, in _communicate UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 126: ordinal not in range(128) Traceback (most recent call last): File ".\sublimeplugin.py", line 119, in execTextCommand File ".\runExternal.py", line 26, in run output, error = p.communicate(view.substr(region)) File ".\subprocess.py", line 668, in communicate File ".\subprocess.py", line 885, in _communicate UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 126: ordinal not in range(128) Writing file C:\Users\cb\Desktop\test.txt with encoding UTF-8 Traceback (most recent call last): File ".\sublimeplugin.py", line 119, in execTextCommand File ".\runExternal.py", line 26, in run File ".\subprocess.py", line 668, in communicate File ".\subprocess.py", line 885, in _communicate UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 126: ordinal not in range(128)

Is that an “error” in the runExternal script itself or in the underlying Subilme Text python libraries?

Thanks!

0 Likes

#5

That’s an encoding problem: your text probably has special or non-english characters.

I updated the pastie to encode the string to utf-8 prior to sending it to the subprocess.

Let me know if it works

0 Likes

#6

Thanks for your help!

The error message has gotten shorter:

Traceback (most recent call last): File ".\sublimeplugin.py", line 119, in execTextCommand File ".\runExternal.py", line 31, in run UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 9: ordinal not in range(128)

I suppose there’s another place in the script where the encoding has to be added. I tried a few guesses but none worked (I’m not really a programmer and/or don’t really know any Python …).

0 Likes

#7

It’s probably decoding that’s required :smile:
I updated the pastie, let me know if it works

0 Likes

#8

Fantastic, it’s working! Thank you again! :smile:

0 Likes

#9

The above pasties do not work with the current beta of Sublime Text 2. I updated and extended the code to make it work with beta 2181:

gist.github.com/1910413

0 Likes

#10

ST3: https://gist.github.com/trigeorgis/6921376
also: https://packagecontrol.io/packages/FilterPipes

0 Likes