Forked from jefftriplett/filter_through_command.py
Created
February 25, 2012 20:20
-
-
Save olifante/1910413 to your computer and use it in GitHub Desktop.
Revisions
-
olifante revised this gist
Feb 25, 2012 . 1 changed file with 36 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,33 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- ## based on http://pastie.org/private/bclbdgxzbkb1gs2jfqzehg import sublime import sublime_plugin import subprocess class PromptRunExternalCommand(sublime_plugin.WindowCommand): """ To create a ⌥⌘R or ctrl-alt-R shortcut to "Filter through command" that prompts for a command, insert this somewhere in your keymap file: { "keys": ["alt+super+r"], "command": "prompt_run_external" } """ def run(self): self.window.show_input_panel("Filter through command:", "", self.on_done, None, None) pass def on_done(self, text): try: if self.window.active_view(): self.window.active_view().run_command("run_external", {"command": text} ) except ValueError, e: sublime.status_message(e) class RunExternalCommand(sublime_plugin.TextCommand): """ Runs an external command with the selected text, which will then be @@ -14,14 +38,17 @@ class RunExternalCommand(sublime_plugin.TextCommand): view.run_command('run_external', dict(command="sort")) You could add filter shortcuts for specific commands by inserting something similar to the following in your user's keymap file: { "keys": ["alt+super+s"], "command": "run_external", "args": {"command": "sort -u"} } This creates a ⌥⌘S or ctrl-alt-S shortcut that lets you sort your file with the Unix "sort -u" command """ def run(self, edit, command): @@ -34,11 +61,11 @@ def run(self, edit, command): p = subprocess.Popen( command, shell = True, bufsize = -1, stdout = subprocess.PIPE, stderr = subprocess.PIPE, stdin = subprocess.PIPE) output, error = p.communicate(self.view.substr(region).encode('utf-8')) -
olifante revised this gist
Feb 25, 2012 . 1 changed file with 21 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -6,23 +6,30 @@ class RunExternalCommand(sublime_plugin.TextCommand): """ Runs an external command with the selected text, which will then be replaced by the command output. For example, to sort the selected text with the Unix "sort" command, open the console (cmd-` or ctrl-`), input the following and press enter: view.run_command('run_external', dict(command="sort")) You could add a "filter through sort" shortcut by inserting something like this in your user's keymap file: { "keys": ["alt+super+s"], "command": "run_external", "args": {"command": "sort"} } """ def run(self, edit, command): if self.view.sel()[0].empty(): ## if 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( @@ -34,13 +41,14 @@ def run(self, edit, command): stdin=subprocess.PIPE) output, error = p.communicate(self.view.substr(region).encode('utf-8')) if error: # sublime.error_message(error.decode('utf-8')) ## I prefer seeing the error message in the status bar ## instead of inside a popup window. sublime.status_message(error.decode('utf-8')) else: self.view.replace(edit, region, output.decode('utf-8')) ## Comment the above and uncomment the following line ## if you want to insert the output at the start of the file: # self.view.insert(edit, 0, output.decode('utf-8')) -
olifante revised this gist
Feb 25, 2012 . 1 changed file with 7 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # based on http://pastie.org/private/bclbdgxzbkb1gs2jfqzehg import sublime import sublime_plugin @@ -9,13 +9,15 @@ class RunExternalCommand(sublime_plugin.TextCommand): Runs an external command with the selected text, which will then be replaced by the command output. If you open the console (cmd-` or ctrl-`) and enter this: view.run_command('run_external', dict(command="sort")) the selected text (or whole file if no selection) will be sorted using the "sort" command. """ def run(self, edit, command): if self.view.sel()[0].empty(): # nothing selected: process the entire file region = sublime.Region(0L, self.view.size()) @@ -24,7 +26,7 @@ def run(self, edit, args): region = self.view.line(self.view.sel()[0]) p = subprocess.Popen( command, shell=True, bufsize=-1, stdout=subprocess.PIPE, -
olifante revised this gist
Feb 25, 2012 . 1 changed file with 20 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,22 +1,27 @@ # 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. If you open the console (cmd-` or ctrl-`) and enter >>> view.run_command('run_external', dict(args="sort")) the selected text (or whole file if no selection) will be sorted using the "sort" command. """ 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, @@ -26,9 +31,14 @@ def run(self, view, args): stderr=subprocess.PIPE, stdin=subprocess.PIPE) output, error = p.communicate(self.view.substr(region).encode('utf-8')) if error: # sublime.error_message(error.decode('utf-8')) # I prefer seeing the error message in the status bar instead of a popup sublime.status_message(error.decode('utf-8')) else: self.view.replace(edit, region, output.decode('utf-8')) # Comment the above and uncomment the following line # if you want to insert the output at the start of the file # self.view.insert(edit, 0, output.decode('utf-8')) -
jefftriplett revised this gist
Sep 29, 2011 . 1 changed file with 8 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,14 +1,15 @@ # saved from: http://pastie.org/private/bclbdgxzbkb1gs2jfqzehg import sublime import sublimeplugin import subprocess class RunExternalCommand(sublimeplugin.TextCommand): """ Runs an external command with the selected text, which will then be replaced by the command output. """ def run(self, view, args): if view.sel()[0].empty(): # nothing selected: process the entire file @@ -19,11 +20,11 @@ def run(self, view, args): p = subprocess.Popen( args, shell=True, bufsize=-1, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) output, error = p.communicate(view.substr(region).encode('utf-8')) -
jefftriplett revised this gist
Sep 29, 2011 . 1 changed file with 24 additions and 24 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -4,30 +4,30 @@ import subprocess class RunExternalCommand(sublimeplugin.TextCommand): """ Runs an external command with the selected text, which will then be replaced by the command output. """ def run(self, view, args): if view.sel()[0].empty(): # nothing selected: process the entire file region = sublime.Region(0L, view.size()) else: # process only selected region region = view.line(view.sel()[0]) p = subprocess.Popen( args, shell = True, bufsize = -1, stdout = subprocess.PIPE, stderr = subprocess.PIPE, stdin = subprocess.PIPE) output, error = p.communicate(view.substr(region).encode('utf-8')) if error: sublime.errorMessage(error.decode('utf-8')) else: view.replace(region, output.decode('utf-8')) -
jefftriplett created this gist
Sep 29, 2011 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,33 @@ # saved from: http://pastie.org/private/bclbdgxzbkb1gs2jfqzehg import sublime, sublimeplugin import subprocess class RunExternalCommand(sublimeplugin.TextCommand): """ Runs an external command with the selected text, which will then be replaced by the command output. """ def run(self, view, args): if view.sel()[0].empty(): # nothing selected: process the entire file region = sublime.Region(0L, view.size()) else: # process only selected region region = view.line(view.sel()[0]) p = subprocess.Popen( args, shell = True, bufsize = -1, stdout = subprocess.PIPE, stderr = subprocess.PIPE, stdin = subprocess.PIPE) output, error = p.communicate(view.substr(region).encode('utf-8')) if error: sublime.errorMessage(error.decode('utf-8')) else: view.replace(region, output.decode('utf-8'))