Sublime Forum

Plugin API: What is the easiest method to find a string?

#1

Hi,

I have a plugin command which is launched when some keyword is entered. In order to check whether the command has to be actually run or not, I have to look at the text following the keyword and check whether some pattern is found. E.g. I enter

abc [ENTER]

and then search the next 10 following lines after abc whether some keyword (say “def”) is found. I currently have absolutely no idea how to achieve this. What is the easiest method? It should work as follows

abd [ENTER]
if “def” is found on one of the next 10 lines: execute the command. Don’t execute otherwise.

0 Likes

#2
source_region = view.sel()[0]
# Use this as starting point for our "region to scan"
a = source_region.end()
# Calculate text point of end of the 10th line following our starting point
source_row, _ = view.rowcol(a)
b = view.text_point(source_row + 10 + 1, 0) - 1
text = view.substr(sublime.Region(a, b))
# Do stuff with the text
if "def" in text:
    pass
1 Like