Skip to content

Instantly share code, notes, and snippets.

@olifante
Forked from jefftriplett/filter_through_command.py
Created February 25, 2012 20:20
Show Gist options
  • Select an option

  • Save olifante/1910413 to your computer and use it in GitHub Desktop.

Select an option

Save olifante/1910413 to your computer and use it in GitHub Desktop.

Revisions

  1. olifante revised this gist Feb 25, 2012. 1 changed file with 36 additions and 9 deletions.
    45 changes: 36 additions & 9 deletions filter_through_command.py
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,33 @@
    # based on http://pastie.org/private/bclbdgxzbkb1gs2jfqzehg
    #!/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 a "filter through sort" shortcut by inserting something like
    this in your user's keymap file:
    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"}
    "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)
    shell = True,
    bufsize = -1,
    stdout = subprocess.PIPE,
    stderr = subprocess.PIPE,
    stdin = subprocess.PIPE)

    output, error = p.communicate(self.view.substr(region).encode('utf-8'))

  2. olifante revised this gist Feb 25, 2012. 1 changed file with 21 additions and 13 deletions.
    34 changes: 21 additions & 13 deletions filter_through_command.py
    Original 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.
    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:
    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.
    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():
    # nothing selected: process the entire file
    ## if nothing selected, process the entire file:
    region = sublime.Region(0L, self.view.size())
    else:
    # process only selected region
    ## 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 a popup
    ## 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
    ## 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'))
  3. olifante revised this gist Feb 25, 2012. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions filter_through_command.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # saved from: http://pastie.org/private/bclbdgxzbkb1gs2jfqzehg
    # 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
    >>> view.run_command('run_external', dict(args="sort"))
    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, args):
    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(
    args,
    command,
    shell=True,
    bufsize=-1,
    stdout=subprocess.PIPE,
  4. olifante revised this gist Feb 25, 2012. 1 changed file with 20 additions and 10 deletions.
    30 changes: 20 additions & 10 deletions filter_through_command.py
    Original file line number Diff line number Diff line change
    @@ -1,22 +1,27 @@
    # saved from: http://pastie.org/private/bclbdgxzbkb1gs2jfqzehg

    import sublime
    import sublimeplugin
    import sublime_plugin
    import subprocess

    class RunExternalCommand(sublimeplugin.TextCommand):
    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, view, args):
    if view.sel()[0].empty():
    def run(self, edit, args):
    if self.view.sel()[0].empty():
    # nothing selected: process the entire file
    region = sublime.Region(0L, view.size())
    region = sublime.Region(0L, self.view.size())
    else:
    # process only selected region
    region = view.line(view.sel()[0])
    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(view.substr(region).encode('utf-8'))

    output, error = p.communicate(self.view.substr(region).encode('utf-8'))
    if error:
    sublime.errorMessage(error.decode('utf-8'))
    # 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:
    view.replace(region, output.decode('utf-8'))
    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'))
  5. @jefftriplett jefftriplett revised this gist Sep 29, 2011. 1 changed file with 8 additions and 7 deletions.
    15 changes: 8 additions & 7 deletions filter_through_command.py
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,15 @@
    # saved from: http://pastie.org/private/bclbdgxzbkb1gs2jfqzehg

    import sublime, sublimeplugin
    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)
    shell=True,
    bufsize=-1,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
    stdin=subprocess.PIPE)

    output, error = p.communicate(view.substr(region).encode('utf-8'))

  6. @jefftriplett jefftriplett revised this gist Sep 29, 2011. 1 changed file with 24 additions and 24 deletions.
    48 changes: 24 additions & 24 deletions filter_through_command.py
    Original 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])
    """
    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)
    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'))
    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'))
    if error:
    sublime.errorMessage(error.decode('utf-8'))
    else:
    view.replace(region, output.decode('utf-8'))
  7. @jefftriplett jefftriplett created this gist Sep 29, 2011.
    33 changes: 33 additions & 0 deletions filter_through_command.py
    Original 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'))