在编辑器和集成终端之间切换焦点

有人知道在Visual Studio Code中在编辑器和集成终端之间切换焦点的键盘快捷键(Mac和Linux)吗?

280134 次浏览

根据VS Code键盘快捷键留档页面,切换集成终端的默认键绑定是“Ctrl+'”。如果您不喜欢该快捷键,您可以通过添加类似于以下内容的内容在键绑定文件中更改它:

{ "key": "ctrl+l", "command": "workbench.action.terminal.toggleTerminal" }

似乎没有用于简单聚焦底部面板的默认键绑定。因此,如果您不想切换底部面板,则需要在键绑定文件中添加类似于以下内容的内容:

{ "key": "ctrl+t", "command": "workbench.action.focusPanel" }

虽然VS Code有很多模态切换和导航快捷方式,但没有一个专门用于“从编辑器移动到终端,然后再返回”。但是,您可以通过重载key并使用when条款来组合这两个步骤。


解决方案

您可以通过向keybindings.json文件添加适当的设置来实现所需的效果。以下是所需的步骤:

  1. 打开命令面板Ctrl+Shift+P Windows/Linux或P Mac)。

  2. 键入“首选项:打开键盘快捷方式文件”并按输入

  3. 将以下条目添加到keybindings.json文件:

// Toggle between terminal and editor focus{"key":     "ctrl+`","command": "workbench.action.terminal.focus"},{"key":     "ctrl+`","command": "workbench.action.focusActiveEditorGroup","when":    "terminalFocus"}

使用这些快捷方式,您可以使用相同的击键在编辑器和集成终端之间对焦。


这里建议的组合键现在作为默认值内置在VSCode中(从1.72.2开始,可能更早)。在尝试添加它之前,请查看ctrl+'是否有效。

在VS Code的现代版本(截至2022年)中,默认键盘快捷键(JSON)文件是只读,因此对于定制设置,您需要编辑单独的专用文件keybindings.json

更多信息可以在官方的Visual Studio留档页面上找到:

游戏有点晚了,但我在keybindings.json中配置了我的如下:

{"key": "ctrl+`","command": "workbench.action.terminal.focus","when": "editorTextFocus"},{"key": "ctrl+`","command": "workbench.action.focusActiveEditorGroup","when": "terminalFocus"},{"key": "alt+`","command": "workbench.action.terminal.toggleTerminal"}

我想要单独的键来打开/关闭终端和在窗口之间来回切换焦点。

我的配置如下,因为我发现ctrl+'有点难按。

{"key": "ctrl+k","command": "workbench.action.focusActiveEditorGroup","when": "terminalFocus"},{"key": "ctrl+j","command": "workbench.action.terminal.focus","when": "!terminalFocus"}

我还配置了以下内容以在编辑器组之间移动。

{"key": "ctrl+h","command": "workbench.action.focusPreviousGroup","when": "!terminalFocus"},{"key": "ctrl+l","command": "workbench.action.focusNextGroup","when": "!terminalFocus"}

顺便说一句,我在Mac上从System Preferences => keyboard =>Modifier Keys配置了大写锁定ctrl

这并不完全是问什么,但我发现它非常有用和相关。

如果有人想从一个终端切换到另一个终端,也可以在Visual Studio的集成终端面板中打开,您可以搜索:

Terminal: Focus Next Terminal

或者添加以下快捷键,并使用键盘组合更快地完成。

  {"key": "alt+cmd+right","command": "workbench.action.terminal.focusNext","when": "terminalFocus"},{"key": "alt+cmd+left","command": "workbench.action.terminal.focusPrevious","when": "terminalFocus"},

Ctrl+J工作;但也显示/隐藏控制台。

cmd+J for mac

尝试使用ctrl+'切换终端的可见性,从而切换焦点。

我这样做是通过设置>键盘快捷方式,然后在它提供搜索栏类型焦点终端并选择选项的部分中。它会要求键入您要为此操作设置的组合。做吧。至于编辑器焦点,在搜索栏中键入“编辑器焦点”并键入您想要的键。如果您出色地添加了一个键。它可以通过编辑杰森来删除,如上所述评论

从版本:1.26.1(linux)开始,默认情况下不设置快捷方式。设置快捷方式

  1. 打开键盘快捷键面板[ctrl+k, ctrl+s]
  2. 搜索焦点终端

在此处输入图片描述

  1. 设置你的捷径

对于编辑器焦点已默认设置。

在此处输入图片描述

Shubham Jain的答案是现在使用内置键盘快捷键的最佳选择。

我画了输入图片描述

Ctrl+

重新映射输入图片描述

Ctrl+L

通过这种方式,您可以在终端和编辑器之间移动焦点,并在非常接近的位置切换终端。

ctrl+':专注于集成终端

ctrl+1:专注于编辑器(如果编辑器-2命令是ctrl+2

输入图片描述

更多信息:https://medium.com/p/21969576c09c

控件 + '~' 将用于在两者之间切换。'就在选项卡按钮上方。此快捷键仅适用于Mac。

通常,VS Code使用ctrl+j打开终端,所以我创建了一个键绑定来切换ctrl+k组合,如下所示keybindings.json

[{"key": "ctrl+k","command": "workbench.action.terminal.focus"},{"key": "ctrl+k","command": "workbench.action.focusActiveEditorGroup","when": "terminalFocus"}]

适用于任何键盘布局的简单窗口解决方案(可能适用于其他操作系统,但未经过测试)

我使用芬兰键盘,所以上面没有一个工作,但这应该适用于所有键盘。

  • 终端焦点:将鼠标悬停在集成终端中的终端文本上。将弹出专注于终端的快捷方式-我的例如说CTRL+ö。
  • 编辑器焦点:如上所述,使用CTRL+1。

这是我的方法,它提供了在活动终端之间导航以及在终端和编辑器窗格不关闭终端视图之间跳转的一致方式。您可以尝试将其直接添加到您的keybindings.json中,但我建议您通过键绑定UI(Mac上的cmd+K cmd+S),以便您可以查看/管理冲突等。

有了这个,我可以使用ctrl+x <arrow direction>导航到任何可见的编辑器或终端。一旦光标位于终端部分,您可以使用ctrl+x ctrl+upctrl+x ctrl+down循环访问活动终端。

cmd-J仍然用于隐藏/显示终端窗格。

    {"key": "ctrl+x right","command": "workbench.action.terminal.focusNextPane","when": "terminalFocus"},{"key": "ctrl+x left","command": "workbench.action.terminal.focusPreviousPane","when": "terminalFocus"},{"key": "ctrl+x ctrl+down","command": "workbench.action.terminal.focusNext","when": "terminalFocus"},{"key": "ctrl+x ctrl+up","command": "workbench.action.terminal.focusPrevious","when": "terminalFocus"},{"key": "ctrl+x up","command": "workbench.action.navigateUp"},{"key": "ctrl+x down","command": "workbench.action.navigateDown"},{"key": "ctrl+x left","command": "workbench.action.navigateLeft","when": "!terminalFocus"},{"key": "ctrl+x right","command": "workbench.action.navigateRight","when": "!terminalFocus"},

实际上,在VS Code 1.48.1中,有一个toggleTerminal命令;我不知道它是否在以前的版本中可用;)您可以在keybindings.json文件中使用它。

这在Windows上适用于我,也应该适用于Linux。

{"key": "ctrl+alt+right","command": "workbench.action.terminal.toggleTerminal","when": "editorTextFocus || terminalFocus"}

这是一种添加您自己的键绑定以切换焦点的方法。

  1. 打开你的VSCode
  2. Ctrl+Shift+P并搜索键盘快捷键并点击此(首选项:打开键盘快捷键)。
  3. 在搜索面板中搜索“焦点终端”并找到此选项(终端:关注终端视图)并单击加号图标。

在此处输入图片描述

  1. 输入您喜欢的未使用的快捷方式,然后按回车键。
  2. 转到编辑器模式并尝试使用您的快捷方式。
  3. 现在点击Alt+Shift+T进入终端。
  4. 想回到编辑那里吗?点击Ctrl+tab

在带有VSCode(1.52.1)的Windows 10机器上测试

嘿,我做这项工作的步骤是:

  1. ctrl + shift+ p寻找preferences: keyboard shortcuts

或者你可以使用ctrl k + ctrl s直接打开它

  1. 在搜索框中查找Terminal: Focus Terminal,我为自己设置了alt + T alt + T,但您可以选择您想要的组合

  2. 在搜索框中查找View: Focus Active Editor Group,为自己设置alt + E alt + E,但您可以再次选择您想要的组合

就这样吧希望这个能帮上忙

100%工作设置…

[{ "key": "alt+right", "command": "workbench.action.terminal.focus"},{ "key": "alt+left", "command": "workbench.action.focusActiveEditorGroup", "when": "terminalFocus"}]

workbench.action.terminal.focus从编辑器切换到终端。workbench.action.focusActiveEditorGroup:<强>切换从终端到编辑器。

为了在ctrl+'组合之间切换,我尝试了所有列出的答案,但没有运气。对于像我一样有类似问题的人,请尝试keybindings.json内的以下快捷方式:在VSCode 1.59+上测试

[{"key": "ctrl+oem_8","command": "workbench.action.terminal.focus", "when": "!terminalFocus"},{"key": "ctrl+oem_8","command": "workbench.action.focusActiveEditorGroup", "when": "terminalFocus"}]

输入图片描述

另一种选择是使用F6shift+F6

F6执行“聚焦下一步”,将焦点从编辑器移动到下面的小组(终端、输出、调试控制台等)。

shift+F6执行“聚焦前一部分”,将焦点从终端面板移回编辑器。

这比ctrl + `的优势在于:

  1. 它不隐藏终端/面板(如果这是您喜欢的。如果您喜欢隐藏/取消隐藏终端,请使用ctrl + `)。

  2. 这将适用于任何面板(终端、输出、调试控制台等)。

ctrl+-也有效,这意味着回到以前的光标位置

我的解决方案:

  • 有一把钥匙
  • 如果还没有终端:打开一个终端并专注于它
  • 如果焦点在终端上:隐藏面板并切换回编辑器
  • 如果焦点在编辑器上并且有一个终端:取消隐藏终端窗格并关注它
  {"key": "ctrl+shift+alt+cmd+t","command": "workbench.action.terminal.new","when": "!terminalIsOpen"},{"key": "ctrl+shift+alt+cmd+t","command": "terminal.focus","when": "terminalIsOpen && !terminalFocus"},{"key": "ctrl+shift+alt+cmd+t","command": "workbench.action.closePanel","when": "terminalIsOpen && terminalFocus"}

快捷键根据键盘布局(QWERTY/QWERTZ/AZERTA等)进行更改

要查找您的快捷键,请按Ctrl+Shift+P并转到首选项:键盘快捷键。

从那里搜索视图:切换终端

最终结果

使用keybindings.json中的键绑定:

  • CTRL+jCTRL+k在编辑器组中的编辑器和terimal中的终端窗口之间转移焦点
  • CTRL+hCTRL+l在编辑器组之间转移焦点包括终端

这些键绑定对vim用户来说应该特别自然。其他人可能希望将交换#0更改为#1

// In an editor group, ctrl+j and ctrl+k jump between editor windows{ "key": "ctrl+j", "command": "workbench.action.nextEditorInGroup" },{ "key": "ctrl+k", "command": "workbench.action.previousEditorInGroup" },// In the terminal, ctrl+j and ctrl+k jump between terminal windows{"key": "ctrl+j","command": "workbench.action.terminal.focusNext","when": "terminalFocus && terminalHasBeenCreated && !terminalEditorFocus || terminalFocus && terminalProcessSupported && !terminalEditorFocus"},{"key": "ctrl+k","command": "workbench.action.terminal.focusPrevious","when": "terminalFocus && terminalHasBeenCreated && !terminalEditorFocus || terminalFocus && terminalProcessSupported && !terminalEditorFocus"},// In the work area, ctrl+j and ctrl+k jump between editor groups{ "key": "ctrl+h", "command": "workbench.action.focusPreviousGroup" },{ "key": "ctrl+l", "command": "workbench.action.focusNextGroup" },// in the first editor group terminal, jump "back" to the terminal (if there is a terminal open){"key": "ctrl+h","when": " terminalHasBeenCreated && terminalIsOpen && activeEditorGroupIndex == 1","command": "workbench.action.terminal.focus"},// in the last editor group terminal, jump "forward" to the terminal (if there is a terminal open){"key": "ctrl+l","when": "terminalHasBeenCreated && terminalIsOpen && activeEditorGroupLast","command": "workbench.action.terminal.focus"},// in the terminal, jump "back" to the last editor group{"key": "ctrl+h","command": "workbench.action.focusLastEditorGroup","when": "terminalFocus"},// in the terminal, jump "forward" to the last first group{"key": "ctrl+l","command": "workbench.action.focusFirstEditorGroup","when": "terminalFocus"},

我的1.56 VS Code的工作原理是:

Ctrl + ~     to focus on terminal window from editorCtrl + 9     to focus back on editor from terminal