Sublime Forum

Can I make shortcut to Find specific string, predefined? (not selection or typing in Find box)

#1

So, I organize my Python code with sections like:

# < part1 >
do_stuff()
... many lines of code

# < part2 >
... etc

What I really, desperately want is to make a keyboard shortcut that will jump to the next occurrence of # < without my having to select it first or type it in the Find box. If I had this, I could skip to the next (and previous?) parts of my code happily all day!

Is there some command that would do this? In my dreams it’d be like {“command”: “find_next_occurrence_of_this_pattern”, “args”: {“pattern”: “# <”} }

(Whereas the commands I’m finding are all like find selection, find_next, show_panel… they’d take much longer.)

Can you help? Thanks! :grin:

0 Likes

#2

Sounds like a job for https://packagecontrol.io/packages/Select%20By%20Regex.

0 Likes

#3

hmm thanks, I’m not seeing how though, their example shows “command”: “select_by_regex_next” without an argument for what to select… like one has to type it in, there. Same goes for another selection plugin I just checked out, SelectExact.

looking at their source code, maybe I’ll need to write a plugin, inherit from sublime_plugin.TextCommand, and use self.view.find(pattern)… but I was really hoping I could just access a command like that view.find(pattern) without getting into making a whole plugin.

Anyone know how to call that view.find() method directly from just a sublime-keymap shortcut mapping?

Or from the console? (I also don’t understand in general how to call a command from the console, like if there’s a command “find_next”, which I know from a key shortcut or looking at a command log, is there somewhere I can therefore call something like like sublime.View.find_next()?)

0 Likes

#4

Ah sorry. It isn’t documented, but I looked into the source code, which shows that the select_by_regex_next command accepts a parameter named “regex”, and then forgot to mention it.
The downside is that you still need to “confirm” the popup that opens with the specified regex prefilled.

I propose you create a ticket suggesting the author to document it and make the regex skip the input panel completely.

0 Likes