ChatGPT解决这个技术问题 Extra ChatGPT

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

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

ctrl + ' ` ' 将是最好的#-> Linux
ctrl + ' ` ' 也适用于 OS X,无需任何额外的键绑定。
ctrl + ' ` ' 在 Windows 上也能完美运行!
CTRL+` 的问题在于它会创建一个新终端,即使它已经存在。
@Guss 不再 (v1.55.0)

i
informatik01

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

解决方案

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

打开命令面板(Ctrl+Shift+P Windows/Linux 或 ⇧ ⌘ P Mac)。键入“首选项:打开键盘快捷键文件”,然后按 Enter。将以下条目添加到 keybindings.json 文件:

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

使用这些快捷键,您可以使用相同的击键在编辑器和集成终端之间集中注意力。

笔记

在现代版本的 VS Code(截至 2022 年)中,默认键盘快捷键 (JSON) 文件是只读,这就是为什么自定义设置,您需要编辑一个单独的专用文件keybindings.json

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

Visual Studio Code 的键绑定:高级自定义


到目前为止,这是最符合人体工程学的解决方案的最佳答案:扩展现有的键绑定。赞!
谢谢,这工作得很好,节省了很多时间。对于国际键盘用户:"ctrl+`" 可以指定为 "ctrl+oem_3"
自从我写下我的答案以来,@Roman 1.18.0 的行为肯定是一种改进,但我认为上述情况仍然更好。不同的是使用按键时终端不会消失,只是焦点会移动。我碰巧喜欢显示我的终端:)
请注意,这些快捷方式应粘贴到 keybindings.json 文件中。
从编辑器中打开 keybindings.json:CMD-SHIFT-P -> Preferences: Open Keyboard Shortcuts File。也是一个不错的资源:code.visualstudio.com/docs/getstarted/keybindings
R
Rahul Shukla

ctrl+` : 专注于集成终端

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

https://i.stack.imgur.com/OubM3.gif

更多信息:https://www.rscoder.com/2020/11/14/how-to-switch-focus-between-editor-and-integrated-terminal-in-visual-studio-code/


任何键盘上的`在哪里🙄
就在 esc 键下面:)
谢谢你。 Ctrl + 1 是我需要的,因为在某些情况下, ctrl+` 将打开外部终端并且不能再次使用它来返回 VSCode 编辑器
Control + '~' 将用于两者之间的切换。我已经使用了一段时间。
@surenderpal ctrl+'~' 创建新终端,它不会将焦点切换到现有终端。
R
RayLoveless

Ctrl+J 有效;但也显示/隐藏控制台。

cmd+J for mac


MacOS 上的 Cmd+J。
无法在 Ubuntu Linux 16.04 (Xenial) + VSC 1.45.0 中运行
您也可以对 cmd+j { "key": "cmd+j", "command": "workbench.action.terminal.focus"}, { "key": "cmd+j", "command": "workbench.action.focusActiveEditorGroup", "when": "terminalFocus"} 使用已接受答案中的相同技巧,这样我就不必把手从主行移开。
@AldoBassanini 适用于 Ubuntu 21.04 + VSC 1.57.0
不知道这存在,否则我不会尝试上面的示例,这应该是公认的答案(MacOS 12.0.1)
P
Pang

游戏有点晚了,但我在 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"
}

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


映射 alt+` 似乎有问题 - 在 github.com/Microsoft/vscode/issues/41024#issuecomment-363096692 跟踪
我发现 altctrl 更容易按,所以在我的配置中它是相反的。
这是一个很棒的建议,尽管我选择保留原始绑定并使用 alt 来切换焦点。
@BenCreasy 这仅适用于 macOS,在 Windows 上可以正常工作。
S
Shubham Jain

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

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

https://i.stack.imgur.com/7Lipk.png

设置快捷方式

默认情况下已经设置了编辑器焦点。

https://i.stack.imgur.com/0vJQT.png


由于它们提供了额外的键盘映射,所以这个答案更简单、更快
这专注于第一个编辑组 - 可能不是 OP 想要的。可能想要当前/活动的编辑组。
这是 2020 年的答案,任何 >= 1.45.0
伟大的。感谢您的澄清。
P
Pang

我将我的配置如下,因为我发现 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 KeysCaps Lock 配置为 ctrl


w
wisbucky

另一种选择是使用 F6shift+F6

F6 执行“聚焦下一部分”,这会将焦点从编辑器移至下方的 Panel(终端、输出、调试控制台等)。

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

ctrl + ` 相比,它的优势在于:

它不会隐藏终端/面板(如果这是您喜欢的。如果您喜欢隐藏/取消隐藏终端,那么只需使用 ctrl + `)。这适用于任何面板(终端、输出、调试控制台等)。


J
Jheison Rodriguez

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

ctrl + shift + p 并查找首选项:键盘快捷键

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

在搜索框中查找终端:焦点终端,我为自己设置 alt + T alt + T 但您可以选择您想要的组合 在搜索框中查看查看:焦点活动编辑器组,为自己设置 alt + E alt + E 但您可以再次选择您想要的组合

就是这样,希望对你有帮助


我将它与 tmux ctrl + a down 和 ctrl + a up 相匹配,现在我可以像在 tmux 中一样工作......谢谢
P
Pang

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

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

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

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

很好,谢谢!我正在寻找你提到的第二个答案。如果有人想知道将焦点放回编辑器的键盘快捷键,那么它是:Command + 1(在 Mac 上)
我认为 focusPanel 适用于一般区域(包括“问题”、“输出”和“调试控制台”等内容。还有一个专门针对集成终端 workbench.action.terminal.focus 的操作。
P
Pang

通常,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+j 只是切换面板。因此,如果上次使用的面板视图是“问题”,您将跳转到那里而不是终端。此外,ctrl+k 被广泛用作和弦开头,因此覆盖它会破坏很多快捷方式。
Z
ZingAju

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

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

https://i.stack.imgur.com/Zb15J.png

输入您喜欢的未使用的快捷方式,然后按 Enter。转到编辑器模式并尝试使用您的快捷方式。现在按 Alt+Shift+T 转到终端。想回到编辑器?只需按 Ctrl+tab

使用 VSCode(1.52.1) 在 Windows 10 机器上测试


G
Gaurav Grover

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


M
Misha Akopov

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

https://i.stack.imgur.com/PPLin.png

到 Ctrl + ;

https://i.stack.imgur.com/rLRkm.png

到 Ctrl + L

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


这为我解决了!键盘快捷键 --> 终端:焦点终端 (workbench.action.terminal.focus) --> 将键绑定更改为我想要的组合。在 vscode Insiders 1.43.0 上运行
J
JStrahl

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

我使用的是芬兰语键盘,所以上述方法都不起作用,但这应该适用于所有键盘。

终端焦点:将鼠标悬停在集成终端中的终端文本上。将弹出专注于终端的快捷方式 - 例如我的说 CTRL+ö。

编辑器焦点:如上所述使用 CTRL+1。


谢谢你,谢谢你,谢谢你!如此简单,如此容易被忽视。这是迄今为止最好的答案。
P
Pang

这并不完全是所要求的,但我发现它非常有用且相关。

如果有人想从一个终端切换到另一个终端,也可以在 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"
  },

P
Pang

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

这在 Windows 上对我有用,也应该在 Linux 上工作。

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

M
Maksym Dudyk

对我的 1.56 VS Code 有用的是:

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

t
totalhack

这是我的方法,它提供了在活动终端之间导航以及在终端和编辑器窗格之间跳转的一致方式无需关闭终端视图。您可以尝试将其直接添加到您的 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"
    },

N
Naman Jain

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 :从终端切换到编辑器。


S
Sreejith Nair

对于 ctrl+` 组合之间切换,我尝试了所有列出的答案,但没有运气。对于那些与我有类似问题的人,请在 keybindings.json 中尝试以下快捷方式:Tested on VSCode 1.59+

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

https://i.stack.imgur.com/u4y57.gif


使用简单的反引号而不是 oem_8 就像 OSX 上的冠军一样。其余的规则工作得很好!
N
Niels Bom

我的解决方案:

有一把钥匙

如果还没有终端:打开一个终端并专注于它

如果焦点在终端上:隐藏面板并切换回编辑器

如果焦点在编辑器上并且有一个终端:取消隐藏终端窗格并专注于它

  {
    "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"
  }

效果很好 - 对我来说没有“alt” - 但它不会在“集中树路径”上打开 - Q)知道怎么做吗? 🤔
如果您不需要“alt”,我怀疑您使用的是非 macOS 计算机。当然,解决方案并不重要。 OP 没有要求打开“重点树路径”。我看到了两个挑战。 1) 命令不带参数。 2)打开的正确路径是什么?也许看看 VSCode 的 predefined variablesthe Multi-Command extension。让我知道你是否能正常工作?
我正在使用#macos :-) - 你知道我已经关注了一些我选择女巫箭头的树项目 - 然后“输入”正在重命名 - “cmd + 向下箭头”是打开的 - 但想念我“在终端中打开那个路径”所以我可以为该文件运行命令
M
Mahad Ali

我通过转到设置>键盘快捷方式然后在它提供搜索栏类型焦点终端的部分并选择该选项来做到这一点。它将要求输入您要为此操作设置的组合。去做吧。至于编辑器焦点类型“编辑器焦点”在搜索栏中并输入您想要的键。如果您出色地添加了密钥。可以通过编辑上面评论中提到的 jason 来删除它


s
surender pal

control + '~' 将用于在两者之间切换。 '`' 就在选项卡按钮的上方。此快捷方式仅适用于 mac。


L
LazerDance

快捷键根据键盘布局(QWERTY/QWERTZ/AZERTA 等)而变化

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

从那里搜索 View:Toggle Terminal

https://i.stack.imgur.com/4nzVA.png


J
Jthorpe

使用 keybindings.json 中的键绑定:

CTRL+j 和 CTRL+k 在编辑器组中的编辑器和终端窗口之间切换焦点

CTRL+h 和 CTRL+l 在包括终端在内的编辑器组之间切换焦点

vim 用户应该觉得这些键绑定特别自然。其他人可能希望将交换 h/j/k/l 更改为 left/down/up/right

// 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"
},

我使用 Vim 扩展,Ctrl+wj 用于向下到控制台,向上我使用 ctrl+1
1
1da

ctrl + - 也可以,表示返回上一个光标位置