-
-
Save sligodave/2586767 to your computer and use it in GitHub Desktop.
Sublime Text 2: "Filter Through Command" plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# saved from: http://pastie.org/private/bclbdgxzbkb1gs2jfqzehg | |
import sublime | |
import sublime_plugin | |
import subprocess | |
class RunExternalCommand(sublime_plugin.TextCommand): | |
""" | |
Runs an external command with the selected text, | |
which will then be replaced by the command output. | |
""" | |
def run(self, edit, args): | |
if self.view.sel()[0].empty(): | |
# nothing selected: process the entire file | |
region = sublime.Region(0L, self.view.size()) | |
else: | |
# process only selected region | |
region = self.view.line(self.view.sel()[0]) | |
p = subprocess.Popen( | |
args, | |
shell=True, | |
bufsize=-1, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE, | |
stdin=subprocess.PIPE) | |
output, error = p.communicate(self.view.substr(region).encode('utf-8')) | |
if error: | |
sublime.errorMessage(error.decode('utf-8')) | |
else: | |
self.view.replace(edit, region, output.decode('utf-8')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample usage:
view.run_command('run_external', {'args': "sed 's/Hi/Bye/g'"})