Sublime Forum

I can't undo previous findbar contents, after ctrl-F selection

#1

When "find_selected_text": true, pressing Ctrl-F copies selected text into the find dialog. This is nice, unless you meant to highlight the find bar instead of selecting text.

Let’s say I spent 20 seconds, maybe several minutes, working on a complex regex. I press Ctrl-F to focus my find bar, but it erases my complex regex. The problem is that I can’t ctrl-Z to recover my original find contents.

Does Sublime have any way to preserve “ctrl-F selection”, but not silently discard findbar contents? (Alternatively, is there any “highlight find bar” keybind that doesn’t overwrite with selection?)

2 Likes

#2

The default binding for Ctrl+F looks like it just uses show_panel to open the find panel, and that command does not have any args that tells it what it should be doing, so I assume that means the copying of the selected text is happening internally while that setting is turned on.

I imagine you could create your own simple custom command that saves the state of that option, turns it off, runs show_panel and then puts the config option back the way it was.

On the other hand, if you want to re-focus the find bar because you did a search, clicked into the document, and then wanted to continue finding things, you might not know that the find panel remembers previous search terms and you can scroll through them with up/down arrows.

Probably though what you’re noticing is that you were scrolling through the document to formulate your search term, in which case you’re hosed because it only remembers search terms that you actually searched for. In which case the custom window command is probably the way to go,

1 Like

#3

Here’s an example of what the plugin code might look like (caveat: not the cleanest code, but it seems to work OK). This creates a new command show_panel_no_clipboard, which is a drop in replacement for the show_panel command.

If you duplicate all of the key bindings that invoke the show_panel command with an argument of panel: "find" and replace the command with this one, the net result will be what you want; the shortcut will open the panel but not clobber the current find text while the option is turned on.

I leave it to you to select what such shortcuts should look like. :wink:

import sublime, sublime_plugin

class ShowPanelNoClipboardCommand(sublime_plugin.WindowCommand):
    def run(self, panel, reverse):
        find_selected_text = self.window.active_view ().settings ().get ("find_selected_text")
        self.window.active_view ().settings ().set ("find_selected_text", False)
        self.window.run_command ("show_panel",
            {
            "panel": panel,
            "reverse": reverse
            })
        self.window.active_view ().settings ().set ("find_selected_text", find_selected_text)

Just save it into e.g show_panel_no_clipboard.py in your User package (Preferences->Browse Packages).

2 Likes

#4

Thanks for this. I mapped it to F3 which is sort of covered by Ctrl+D anyway.

0 Likes