7

I know that it is possible to show the Sublime Text "Find and Replace" panel using the show_panel command (either via a keybinding or a plugin), and control which arguments are enabled/disabled.

Example run from Sublime Console panel:

window.run_command('show_panel', { 'panel': 'replace', 'regex': True, 'case_sensitive': False, 'whole_word': False, 'in_selection': False, 'wrap': True, 'highlight': True, 'preserve_case': True })

What I would like to know, is: is there a way to pre-populate the Find What: and Replace With: values?

I found this forum post but it has no reply, and the unofficial documentation is of no help in this case.

I have tried:

  • 'find_what': 'string'
  • 'replace_with': 'string'
  • 'find_history': 'string'
  • 'replace_history': 'string'
  • 'find_history': ['string']
  • 'replace_history': ['string']
  • 'find': 'string'
  • 'replace': 'string'

EDIT: I have also tried:

  • characters
  • find_characters
  • look_for
  • search_for
  • find_regex
  • find_string
  • search_string
  • replacement
  • search_characters

and none of the above makes any difference - the panel is always pre-populated with the previous search and replace values, not what I pass in.


I am aware of the commands slurp_find_string and slurp_replace_string, which will take the current selection and update the Find What / Replace With values respectively, but I would like a way to do it without having to mess around with selections first - I just want to pass the values as arguments directly to the show_panel command.

Does anyone know what parameters/arguments can be used to control this?

Keith Hall
  • 15,362
  • 3
  • 53
  • 71
  • Silly question, but did you try `find_string` and `replace_string`? – MattDMo Jul 22 '16 at 18:08
  • Darn, it doesn't seem to be working for me: `window.run_command("show_panel", {"panel": "replace", "find_string": "foo", "replace_string": "baz"})` opens the panel, but doesn't populate the text boxes... – MattDMo Jul 22 '16 at 18:11

3 Answers3

3

You can run an insert command on the window just after you've run the show_panel command.

import sublime_plugin


class ShowPanelPrefilledCommand(sublime_plugin.WindowCommand):
    def run(self, interactive=True):
        self.window.run_command('show_panel', {
            'panel': 'find_in_files',
            'where': '<open folders>',
            'whole_word': False,
            'case_sensitive': False,
            'preserve_case': False,
            'regex': False,
            'use_buffer': False,
            'show_context': False,
        })

        self.window.run_command('insert', {'characters': 'hello'})

        if not interactive:
            self.window.run_command('find_all', {'close_panel': True})
Gerard Roche
  • 6,162
  • 4
  • 43
  • 69
  • 1
    This plugin does insert "hello" in the `Find` field but it leaves the `Replace` field empty. To move the cursor to the `Replace` field, so a 2nd `insert` command could be run, I tried running `next_field` twice as a `sublime`, `window`, and `view` command via `run_command()` but all failed. Placing 2x `"command": "next_field"` in a sublime-macro and running that also failed, in hindsight I think `next_field` is for snippets only. I see no way around this unless there is a way to programmatically move the cursor between panel fields so the `Replace` field can be filled using an `insert`. – mattst Jul 17 '19 at 15:24
1

I wanted to do the same thing for a key binding command that opens a Package Tool and uses the Find Search window. I ended up with something easy to customize and use for any command that just uses another key binding.

After I run my Package Tool, I just use this key binding to insert the text (which is a complicated regex in my case). For me on OSX, it's intuitively remembered as (insert 1) CMD+i, CMD+1

Open Sublime Text / Preferences / Key Bindings

Add this to the User - Default (OS).sublime-keymap

{ "keys": ["super+i", "super+1"], "command": "insert", "args": { "characters": "my-custom-insert-text1" } },
{ "keys": ["super+i", "super+2"], "command": "insert", "args": { "characters": "my-custom-insert-text2" } },

// etc..
1

This feature was added in Sublime Text build 4123 on 6 December 2021.

The show_panel command for the find and find in files panels can now take "pattern" and "replace_pattern" arguments.

Keith Hall
  • 15,362
  • 3
  • 53
  • 71