Sublime Forum

Searching backward

#1

Hello,

I am writing a command for Sublime 3, and I would need to search backward from the current position, something like this

def run(self, edit):
    pos = self.view.sel()[0].begin()
    region = self.view.find(r"---..---", pos) 
    self.view.sel().clear()
    self.view.sel().add(region)

but self.view.find() only searches forward. How can I search backward?

If there is no appropriate method, then can I use the self.view.run_command() method to call the find command (the same way as Ctrl+f does) and force that command to search backward?

Thanks

0 Likes

#2


you can use Python’s re module, but its not possible to set arbitrary text/patterns in the find dialog unless it is the selected text in the main view

it is possible to set the flag toggle buttons etc programmatically though in the find dialog

0 Likes

#3

When I needed such a feature, I basically used find_all, and then you can easily find the region just before your position.

0 Likes