Sublime Forum

How to filter content with shell

#1

For example invoke a shell using the selected text on stdio (or as parameter) and replace the selected text with the output of the shell (bourne, bash, python … whatever) ?

How can I do this ?

Should this be a feature request ?

andy

0 Likes

#2

Maybe https://github.com/technocoreai/SublimeExternalCommand ?

0 Likes

#3

Great, thanks Bizoo, it does the job perfectly -andy

0 Likes

#4

Well - almost … tiny problem left … how to set the PATH it gets -andy

0 Likes

#5

On Windows it use the shell, so PATH is the same as the shell.
On linux I don’t know.
This is the code used to run the command:

[code] env = dict(os.environ)
if not (‘LC_CTYPE’ in env or ‘LC_ALL’ in env or ‘LANG’ in env):
env’LC_CTYPE’] = ‘en_US.UTF-8’

    self.proc = subprocess.Popen(
        self.cmdline,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        shell=True,
        env=env)[/code]

Maybe the modification of env has side effect on PATH ?

0 Likes

#6

That’s very useful code, thankyou.

I could set it in the code of the package
Its a few years since I did any python - I remember there is a command to set the PATH but my memory isn’t strong enough to know it properly - is it something like

import sys
sys.PATH = whatever ?

but this isn’t really the best solution - better would be to set the path that the editor sees (assuming the package gets it from there). The PATH the package uses is quite sensible (minimal) but I want it to see my own utils directory without directly putting stuff in /bin or /usr/bin. Of course in my other stuff I set that in my own .bashrc and .profile. I haven’t yet waded through those and /etc/{profile,bashrc} to see if the paths set there match what sublime is getting - the real question is where on earth does sublime (well the shell window-pane) get its PATH from anyway.

andy

0 Likes

#7

Solved it - but yet not quite.

The trick is to put something in /etc/launchd.conf
Commands in there are executed at launchd startup and a PATH set there will be inherited by all GUI apps.
This is documented in the responses to this question

stackoverflow.com/questions/1356 … es-in-os-x

However, that still isn’t exactly what I want - I can do that but then every gui programme on my mac will inherit the PATH I set here - and since it includes looking in a subdirectory of my main user (/Users/andy/utils) it isn’t sensible - it works, yes but its just not sensible for the every prog on the system to look there.

Of course its easy to do by starting Sublime from a terminal

PATH=$PATH:blah /Applications/Sublime\ Text\ 2.app/Contents/MacOS/Sublime\ Text\ 2

(well actually the path from there is ok anyway without setting it in front of the command. However, for real ease of use (and elegance) what is needed is a way to pass a PATH to just this gui app and not the others. I think such a way on the mac does not exist unless the app sets its own PATH (perfectly possible - but does Sublime do it?)

andy

0 Likes