sublimetext3 - Sublime - 用于转到第一个选择 'Find in Files' 的键盘快捷键

标签 sublimetext3 sublimetext sublime-text-plugin

当我使用“在文件中查找”在 Sublime Text 中搜索关键字时,我总是必须使用鼠标单击我想要在该文件中转到的选择。有什么方法可以使用键盘来进行选择跳转吗?或者是否有任何插件可以做到这一点?

enter image description here

最佳答案

默认情况下,您可以使用Find > Find Results 中的菜单项或其关联的键绑定(bind)(在菜单中可见)在查找操作的所有结果之间导航。这样做时,结果会按向前或向后的顺序排列,如果有很多结果,这可能是理想的,也可能不是理想的。

除了通常的文件导航之外,没有任何导航键可以在文件输出中的查找内容中跳转,但您可以使用插件添加这样的导航键:

import sublime
import sublime_plugin

class JumpToFindMatchCommand(sublime_plugin.TextCommand):
    """
    In a find in files result, skip the cursor to the next or previous find
    match, based on the location of the first cursor in the view.
    """
    def run(self, edit, forward=True):
        # Find the location of all matches and specify the one to navigate to
        # if there aren't any in the location of travel.
        matches = self.view.find_by_selector("constant.numeric.line-number.match")
        fallback = matches[0] if forward else matches[-1]

        # Get the position of the first caret.
        cur = self.view.sel()[0].begin()

        # Navigate the found locations and focus the first one that comes
        # before or after the cursor location, if any.
        pick = lambda p: (cur < p.begin()) if forward else (cur > p.begin())
        for pos in matches if forward else reversed(matches):
            if pick(pos):
                return self.focus(pos)

        # not found; Focus the fallback location.
        self.focus(fallback)

    def focus(self, location):
            # Focus the found location in the window
            self.view.show(location, True)

            # Set the cursor to that location.
            self.view.sel().clear()
            self.view.sel().add(location.begin())

这实现了一个新命令jump_to_find_match,它采用可选参数forward来确定跳跃应该向前还是向后,并将 View 集中在下一个或上一个查找结果基于文件中第一个光标的光标位置,根据需要换行。

结合此插件,可以设置如下键绑定(bind)来使用该命令。这里我们使用 TabShift+Tab 键;每个中的上下文确保绑定(bind)仅在查找结果中处于事件状态。

{
    "keys": ["tab"],
    "command": "jump_to_find_match",
    "args": {
        "forward": true
    },
    "context": [
        { "key": "selector", "operator": "equal", "operand": "text.find-in-files", "match_all": true },
    ],
},

{
    "keys": ["shift+tab"],
    "command": "jump_to_find_match",
    "args": {
        "forward": false
    },
    "context": [
        { "key": "selector", "operator": "equal", "operand": "text.find-in-files", "match_all": true },
    ],
},

这将允许您在查找面板中的匹配项之间导航,但您仍然需要使用鼠标才能实际跳转到相关文件中的匹配位置。

要通过键盘执行此操作,您可以使用 this plugin ,它实现了一个模拟双击光标位置的命令。只要光标位于查找匹配项上,如下所示的键绑定(bind)就会触发命令以响应 Enter 键:

{
    "keys": ["enter"],
    "command": "double_click_at_caret",
    "context": [
        { "key": "selector", "operator": "equal", "operand": "text.find-in-files", "match_all": true },
    ],
},

关于sublimetext3 - Sublime - 用于转到第一个选择 'Find in Files' 的键盘快捷键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52210149/

相关文章:

python - Python 文件的所有行都在 Sublime Text 2 中突出显示

html - 在 HTML sublime text 2 中自动完成 css 类或 id

sublimetext3 - 如何在 Sublime Text 3 中隐藏底部控制台文本输出区域?

sublimetext3 - Sublime选项卡的缩进被白色边框或矩形包围

angularjs - Sublimetext 3 和 AngularJS - 语法高亮 (HTML)

sublimetext - 有没有一种方法可以打开所有带有高级文本的文件的远程目录(Windows)

css - Sublime Text 3 - 代码片段不会在选项卡上触发,仅在 CSS 文件上触发

C++ 在 Sublime Text 2 中编译和运行

sublimetext2 - 是否可以在 Sublime 侧边栏中的文件名旁边显示文件大小?

python - 如何将 args 传递给 sublime_plugin.WindowCommand 的实例?