Sublime Text:如何使用键盘从“查找结果”跳转到文件?

问题描述 投票:0回答:6

如果您

File
>
Find in Files...
++F,您将进入
Find Results
,列出文件并突出显示匹配项。您可以双击文件名/路径或匹配的行以打开右侧行的文件。

我想知道是否有一种方法可以完全完成通过键盘双击所做的事情?

凭借 Sublime 出色的文件切换功能,我想一定有办法让您在执行操作时将手放在键盘上

Find in Files...

find sublimetext2 sublimetext sublimetext3
6个回答
65
投票

尝试使用 Shift+F4(铝键盘上的 fn+Shift+F4)。


31
投票

似乎已经创建了一个插件来执行此操作。快速浏览了一下,插件中还有一些附加功能。虽然我下面的原始答案可行,但安装现有插件会更容易。

https://sublime.wbond.net/packages/BetterFindBuffer


可以使用插件。

import sublime
import sublime_plugin
import re
import os
class FindInFilesGotoCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        view = self.view
        if view.name() == "Find Results":
            line_no = self.get_line_no()
            file_name = self.get_file()
            if line_no is not None and file_name is not None:
                file_loc = "%s:%s" % (file_name, line_no)
                view.window().open_file(file_loc, sublime.ENCODED_POSITION)
            elif file_name is not None:
                view.window().open_file(file_name)

    def get_line_no(self):
        view = self.view
        if len(view.sel()) == 1:
            line_text = view.substr(view.line(view.sel()[0]))
            match = re.match(r"\s*(\d+).+", line_text)
            if match:
                return match.group(1)
        return None

    def get_file(self):
        view = self.view
        if len(view.sel()) == 1:
            line = view.line(view.sel()[0])
            while line.begin() > 0:
                line_text = view.substr(line)
                match = re.match(r"(.+):$", line_text)
                if match:
                    if os.path.exists(match.group(1)):
                        return match.group(1)
                line = view.line(line.begin() - 1)
        return None

使用命令

find_in_files_goto
设置键绑定。但这样做时要小心。理想情况下,会有一些设置将此视图标识为“在文件中查找”视图,因此您可以将其用作上下文。但我不知道其中之一。当然,如果你找到了,请告诉我。

编辑 将示例键绑定拉到答案的主体中。

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

22
投票

SublimeText 3
上,我必须使用
F4
(用于转到当前结果文件)和
Shift +F4
(用于先前结果)。

从默认键盘映射...

{ "keys": ["super+shift+f"], "command": "show_panel", "args": {"panel": "find_in_files"} },
{ "keys": ["f4"], "command": "next_result" },
{ "keys": ["shift+f4"], "command": "prev_result" },

我希望这篇文章有帮助。

SP


10
投票

命令“next_result”将执行此操作。使用 muhqu 发布的关于使用范围的巧妙想法,您可以做到这样,您可以在您想要转到的行上按“回车”:

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

5
投票

尝试 Ctrl+P - 这可以按项目中的名称快速打开文件,有关键盘快捷键的完整列表,请参阅此处


3
投票

如果您使用的是 Sublime Text build 4153 或更高版本:

为此目的添加了一个名为

current_result
的新命令:https://github.com/sublimehq/sublime_text/issues/6014#issuecomment-1669019483


如果您使用的是 Sublime Text build 4143 或更早版本:

可以通过执行参数为

drag_select
"by": "words"
命令来模拟 Sublime Text 中的双击(如默认
sublime-mousemap
文件中所示)。

但是,在这项工作中,您需要假装鼠标是插入符号所在的位置。以下插件将执行此操作(工具菜单 -> 开发人员 -> 新插件...,并将模板替换为以下内容):

import sublime
import sublime_plugin


class DoubleClickAtCaretCommand(sublime_plugin.TextCommand):
    def run(self, edit, **kwargs):
        view = self.view
        window_offset = view.window_to_layout((0,0))
        vectors = []
        for sel in view.sel():
            vector = view.text_to_layout(sel.begin())
            vectors.append((vector[0] - window_offset[0], vector[1] - window_offset[1]))
        for idx, vector in enumerate(vectors):
            view.run_command('drag_select', { 'event': { 'button': 1, 'count': 2, 'x': vector[0], 'y': vector[1] }, 'by': 'words', 'additive': idx > 0 or kwargs.get('additive', False) })

与键绑定结合使用,例如:

{ "keys": ["alt+/"], "command": "double_click_at_caret" },
© www.soinside.com 2019 - 2024. All rights reserved.