-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrep.py
29 lines (21 loc) · 918 Bytes
/
grep.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# http://superuser.com/questions/452189/how-can-i-filter-a-file-for-lines-containing-a-string-in-sublime-text-2
import sublime, sublime_plugin
def filter(view, e, needle):
# get non-empty selections
regions = [s for s in view.sel() if not s.empty()]
# if there's no non-empty selection, filter the whole document
if len(regions) == 0:
regions = [ sublime.Region(0, view.size()) ]
for region in reversed(regions):
lines = view.split_by_newlines(region)
for line in reversed(lines):
if not needle in view.substr(line):
view.erase(e, view.full_line(line))
class GrepCommand(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)