How to show the find and replace panel with a pre-populated search string?
Asked Answered
B

3

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?

Barytone answered 22/7, 2016 at 6:39 Comment(2)
Silly question, but did you try find_string and replace_string?Hartshorn
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...Hartshorn
F
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})
Frangible answered 5/2, 2018 at 21:45 Comment(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.Wherry
C
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..
Cari answered 17/10, 2019 at 22:38 Comment(0)
B
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.

Barytone answered 17/8, 2022 at 7:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.