默认情况下,您可以使用Find > Find Results
中的菜单项或其关联的键绑定(在菜单中可见)在查找操作的所有结果之间导航。这样做,结果按顺序前进或后退,如果有很多结果,这可能是也可能不是。
除了通常的文件导航之外,没有任何导航键可以在find in files输出内跳转,但你可以使用插件添加:
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
的可选参数来确定跳转是向前还是向后,并根据文件中第一个光标的光标位置将视图聚焦在下一个或上一个查找结果上。如所须。
与此插件结合使用时,可以设置以下键绑定以使用该命令。这里我们使用Tab和Shift + Tab键;每个中的context
确保绑定仅在查找结果时才有效。
{
"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,它实现一个模拟光标位置双击的命令。只要光标位于查找匹配项上,以下键绑定就会触发该命令以响应Enter键:
{
"keys": ["enter"],
"command": "double_click_at_caret",
"context": [
{ "key": "selector", "operator": "equal", "operand": "text.find-in-files", "match_all": true },
],
},