【问题标题】:How to show the find and replace panel with a pre-populated search string?如何使用预填充的搜索字符串显示查找和替换面板?
【发布时间】:2016-07-22 06:39:57
【问题描述】:

我知道可以使用show_panel 命令(通过键绑定或插件)显示 Sublime Text“查找和替换”面板,并控制启用/禁用哪些参数。

从 Sublime 控制台面板运行示例:

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 })

我想知道的是:有没有办法预先填充 Find What:Replace With: 值?

我找到了this forum post,但没有回复,unofficial documentation 在这种情况下也无济于事。

我试过了:

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

编辑:我也试过:

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

以上都没有任何区别 - 面板总是预先填充了以前的搜索和替换值,而不是我传入的值。


我知道命令 slurp_find_stringslurp_replace_string,它们将分别采用当前选择并更新 Find What / Replace With 值,但我想要一种方法来做到这一点而不必乱来首先选择 - 我只想将值作为参数直接传递给 show_panel 命令。

有谁知道可以使用哪些参数/参数来控制它?

【问题讨论】:

  • 愚蠢的问题,但你试过find_stringreplace_string吗?
  • 该死,它似乎对我不起作用:window.run_command("show_panel", {"panel": "replace", "find_string": "foo", "replace_string": "baz"}) 打开面板,但没有填充文本框...

标签: sublimetext3 sublime-text-plugin


【解决方案1】:

您可以在运行show_panel 命令后立即在窗口上运行插入命令。

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})

【讨论】:

  • 这个插件确实在Find 字段中插入了“hello”,但它使Replace 字段为空。要将光标移动到Replace 字段,以便可以运行第二个insert 命令,我尝试通过run_command() 运行next_field 两次作为sublimewindowview 命令但所有失败的。将 2x "command": "next_field" 放在一个崇高的宏中并运行它也失败了,事后看来我认为 next_field 仅适用于 sn-ps。我认为没有办法解决这个问题,除非有办法以编程方式在面板字段之间移动光标,以便可以使用 insert 填充 Replace 字段。
【解决方案2】:

我想对打开包工具并使用查找搜索窗口的键绑定命令执行相同的操作。我最终得到了一些易于定制和用于任何只使用另一个键绑定的命令的东西。

在我运行我的打包工具之后,我只是使用这个键绑定来插入文本(在我的例子中这是一个复杂的正则表达式)。对我来说,在 OSX 上,它直观地记为 (insert 1) CMD+i, CMD+1

打开Sublime Text / Preferences / Key Bindings

将此添加到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..

【讨论】:

    猜你喜欢
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 2011-02-16
    相关资源
    最近更新 更多