Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to filter a file I'm editing in Sublime Text 2 for lines contain a certain string, if possible including regular expressions.

Consider the following file:

foo bar
baz
qux
quuux baz

When filtered for ba, the result should be:

foo bar
baz
quuux baz

How can I do that?

share|improve this question

3 Answers 3

up vote 60 down vote accepted

Sublime Text 2 is an extensible editor with a Python API. You can create new commands (called Plugins) and make them available from the UI.

Adding basic filtering TextCommand plugin

In Sublime Text 2, select Tools » New Plugin and enter the following text:

import sublime, sublime_plugin

def filter(v, e, needle):
    # get non-empty selections
    regions = [s for s in v.sel() if not s.empty()]

    # if there's no non-empty selection, filter the whole document
    if len(regions) == 0:
        regions = [ sublime.Region(0, v.size()) ]

    for region in reversed(regions):
        lines = v.split_by_newlines(region)

        for line in reversed(lines):
            if not needle in v.substr(line):
                v.erase(e, v.full_line(line))

class FilterCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        def done(needle):
            e = self.view.begin_edit()
            filter(self.view, e, needle)
            self.view.end_edit(e)

        cb = sublime.get_clipboard()
        sublime.active_window().show_input_panel("Filter file for lines containing: ", cb, done, None, None)

Save as filter.py in ~/Library/Application Support/Sublime Text 2/Packages/User

Integration with UI

To add this plugin to the Edit menu, select Preferences… » Browse Packages and open the User folder. If a file called Main.sublime-menu doesn't exist, create it. Add or set the following text to that file:

[
    {
        "id": "edit",
        "children":
        [
            {"id": "wrap"},
            { "command": "filter" }
        ]
    }
]

This will insert the filter command call (essentially, filter is transformed to FilterCommand().run(…) for the plugin call and Filter for the menu label) just below the wrap command. See step 11 here for a more detailed explanation why that is.

To assign a keyboard shortcut, open and edit the file Default (OSX).sublime-keymap on OS X, or the equivalent for other systems, and enter the following:

[  
    {   
        "keys": ["ctrl+shift+f"], "command": "filter"
    }  
]  

This will assign the shortcut F to this command.


To make the command show up in the Commands Palette, you need to create a file named Default.sublime-commands (or edit an existing one) in the User folder. The syntax is similar to the menu file you just edited:

[
    { "caption": "Filter Lines in File", "command": "filter" }
]

Multiple entries (enclosed by curly brackets) need to be separated by commas.

 Behavior and UI integration screenshots

The command, as implemented, will filter all lines that are part of a selection (the entire lines, not just the selected parts), or, if no selection exists, the entire buffer, for a substring that is entered to the input field (default is the — possibly useless multi-line — clipboard) after the command is triggered. It can easily be extended to e.g. support regular expressions, or only leave lines not matching a certain expression.

Menu item

Command in menu

Commands palette entry

Command with different label in Commands Palette

Editor

User entering text to filter file with

Result after executing the command

Adding support for Regular Expressions

To add support for regular expressions, use the following scripts and snippets instead:

filter.py:

import sublime, sublime_plugin, re

def matches(needle, haystack, is_re):
    if is_re:
        return re.match(needle, haystack)
    else:
        return (needle in haystack)

def filter(v, e, needle, is_re = False):
    # get non-empty selections
    regions = [s for s in v.sel() if not s.empty()]

    # if there's no non-empty selection, filter the whole document
    if len(regions) == 0:
        regions = [ sublime.Region(0, v.size()) ]

    for region in reversed(regions):
        lines = v.split_by_newlines(region)

        for line in reversed(lines):

            if not matches(needle, v.substr(line), is_re):
                v.erase(e, v.full_line(line))

class FilterCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        def done(needle):
            e = self.view.begin_edit()
            filter(self.view, e, needle)
            self.view.end_edit(e)

        cb = sublime.get_clipboard()
        sublime.active_window().show_input_panel("Filter file for lines containing: ", cb, done, None, None)

class FilterUsingRegularExpressionCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        def done(needle):
            e = self.view.begin_edit()
            filter(self.view, e, needle, True)
            self.view.end_edit(e)

        cb = sublime.get_clipboard()
        sublime.active_window().show_input_panel("Filter file for lines matching: ", cb, done, None, None)

Main.sublime-menu:

[
    {
        "id": "edit",
        "children":
        [
            {"id": "wrap"},
            { "command": "filter" },
            { "command": "filter_using_regular_expression" }
        ]
    }
]

Default (OSX).sublime-keymap:

[  
    {   
        "keys": ["ctrl+shift+f"], "command": "filter"
    },
    {
        "keys": ["ctrl+shift+option+f"], "command": "filter_using_regular_expression"
    }
]  

A second plugin command, Filter Using Regular Expression will be added below the Filter menu entry.

Default.sublime-commands:

[
    { "caption": "Filter Lines in File", "command": "filter" },
    { "caption": "Filter Lines in File Using Regular Expression", "command": "filter_using_regular_expression" }
]
share|improve this answer
1  
Don't you want to publish this as a package one day? –  slhck May 23 '13 at 12:53
    
@slhck Someone already did (with appropriate attribution), see NovicePhysicist's answer. From what I read in the code, it was improved quite a bit over this answer as well. –  Daniel Beck May 23 '13 at 13:51
    
Cool, I didn't notice that! –  slhck May 23 '13 at 13:55
    
What is that theme you have with the SublimeText that gives that nice colors? Or is it just changing the window color? –  pal4life Jul 16 at 1:22
1  
@pal4life: It's called Solarized (Light). I think Sublime Text even ships with it. I might have installed an alternative one though, so the colors might not match exactly. Screenshots are on OS X, so that's where the window border and title bar are from. –  Daniel Beck Jul 16 at 7:31

There is also a poor man's line filtering algorithm (or is it lazy?):

  1. Select string of interest
  2. Hit Alt+F3 to go into multi-cursor mode on all occurrences
  3. Hit Home
  4. Hit Shift+End
  5. Copy-paste selection to another buffer
share|improve this answer
    
This is pretty much the simplest solution. Bravo! –  gillytech Oct 1 at 22:03
    
Thank you! I prefer not having to install/learn another plugin for as much as possible - Alt+F3 is in my muscle memory so the above solution is actually not that far fetched for me. –  Olof Bjarnason Oct 4 at 18:42

There's now a plugin for filtering lines: https://github.com/davidpeckham/FilterLines

It allows filtering and code folding based on strings or regular expressions.

share|improve this answer
2  
Just installed this plugin - PERFECT for the job. Takes an existing file, lets you enter a filter phrase and puts the results in a new tab. –  Nick Oct 10 '13 at 7:28
1  
Agreed, until now I have only ever dreamed of having this sort of functionality in my text editor. –  Michael12345 Jan 30 at 0:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.