VS Code 的快捷按键(Key Bindings for Visual Studio Code)

优质
小牛编辑
130浏览
2023-12-01

Visual Studio Code lets you perform most tasks directly from the keyboard. This page lists out the default bindings and describes how you can update them.

Visual Studio Code允许您直接从键盘执行大多数任务。此页面列出了默认绑定,并介绍了如何更新它们。

Note: If you visit this page on a Mac, you will see the key bindings for the Mac. If you visit using Windows or Linux, you will see the keys for that OS. If you need the key binding for another OS, hover your mouse over the key you are interested in.

注意: 如果您在Mac上访问此页面,您将看到Mac的键绑定。如果使用Windows或Linux访问,您将看到该操作系统的密钥。如果您需要另一个操作系统的键绑定,将鼠标悬停在您感兴趣的键上。

Note: The following keys are rendered assuming a standard US keyboard layout. If you use a different keyboard layout, please read below.

注意: 以下键是使用标准美式键盘布局呈现的。如果您使用不同的键盘布局,请阅读这一部分。

基本编辑(Basic Editing)

KeyCommandCommand id
kb(editor.action.clipboardCutAction)Cut line (empty selection)editor.action.clipboardCutAction
kb(editor.action.clipboardCopyAction)Copy line (empty selection)editor.action.clipboardCopyAction
kb(editor.action.deleteLines)Delete Lineeditor.action.deleteLines
kb(editor.action.insertLineAfter)Insert Line Beloweditor.action.insertLineAfter
kb(editor.action.insertLineBefore)Insert Line Aboveeditor.action.insertLineBefore
kb(editor.action.moveLinesDownAction)Move Line Downeditor.action.moveLinesDownAction
kb(editor.action.moveLinesUpAction)Move Line Upeditor.action.moveLinesUpAction
kb(editor.action.copyLinesDownAction)Copy Line Downeditor.action.copyLinesDownAction
kb(editor.action.copyLinesUpAction)Copy Line Upeditor.action.copyLinesUpAction
kb(editor.action.addSelectionToNextFindMatch)Add Selection To Next Find Matcheditor.action.addSelectionToNextFindMatch
kb(editor.action.moveSelectionToNextFindMatch)Move Last Selection To Next Find Matcheditor.action.moveSelectionToNextFindMatch
kb(cursorUndo)Undo last cursor operationcursorUndo
kb(editor.action.selectHighlights)Select all occurrences of current selectioneditor.action.selectHighlights
kb(editor.action.changeAll)Select all occurrences of current wordeditor.action.changeAll
kb(expandLineSelection)Select current lineexpandLineSelection
kb(editor.action.insertCursorBelow)Insert Cursor Beloweditor.action.insertCursorBelow
kb(editor.action.insertCursorAbove)Insert Cursor Aboveeditor.action.insertCursorAbove
kb(editor.action.jumpToBracket)Jump to matching bracketeditor.action.jumpToBracket
kb(editor.action.indentLines)Indent Lineeditor.action.indentLines
kb(editor.action.outdentLines)Outdent Lineeditor.action.outdentLines
kb(cursorHome)Go to Beginning of LinecursorHome
kb(cursorEnd)Go to End of LinecursorEnd
kb(cursorBottom)Go to End of FilecursorBottom
kb(cursorTop)Go to Beginning of FilecursorTop
kb(scrollLineDown)Scroll Line DownscrollLineDown
kb(scrollLineUp)Scroll Line UpscrollLineUp
kb(scrollPageDown)Scroll Page DownscrollPageDown
kb(scrollPageUp)Scroll Page UpscrollPageUp
kb(editor.fold)Fold (collapse) regioneditor.fold
kb(editor.unfold)Unfold (uncollapse) regioneditor.unfold
kb(editor.foldAll)Fold (collapse) all regionseditor.foldAll
kb(editor.unfoldAll)Unfold (uncollapse) all regionseditor.unfoldAll
kb(editor.action.addCommentLine)Add Line Commenteditor.action.addCommentLine
kb(editor.action.removeCommentLine)Remove Line Commenteditor.action.removeCommentLine
kb(editor.action.commentLine)Toggle Line Commenteditor.action.commentLine
kb(editor.action.blockComment)Toggle Block Commenteditor.action.blockComment
kb(actions.find)Findactions.find
kb(editor.action.startFindReplaceAction)Replaceeditor.action.startFindReplaceAction
kb(editor.action.nextMatchFindAction)Find Nexteditor.action.nextMatchFindAction
kb(editor.action.previousMatchFindAction)Find Previouseditor.action.previousMatchFindAction
kb(toggleFindCaseSensitive)Toggle Find Case SensitivetoggleFindCaseSensitive
kb(toggleFindRegex)Toggle Find RegextoggleFindRegex
kb(toggleFindWholeWord)Toggle Find Whole WordtoggleFindWholeWord
kb(editor.action.toggleTabFocusMode)Toggle Use of Tab Key for Setting Focuseditor.action.toggleTabFocusMode
kb(toggleRenderWhitespace)Toggle Render WhitespacetoggleRenderWhitespace

更加丰富的语言编辑(Rich Languages Editing)

KeyCommandCommand id
kb(editor.action.triggerSuggest)Trigger Suggesteditor.action.triggerSuggest
kb(editor.action.triggerParameterHints)Trigger Parameter Hintseditor.action.triggerParameterHints
kb(editor.action.format)Format Codeeditor.action.format
kb(editor.action.goToDeclaration)Go to Definitioneditor.action.goToDeclaration
kb(editor.action.previewDeclaration)Peek Definitioneditor.action.previewDeclaration
kb(editor.action.openDeclarationToTheSide)Open Definition to the Sideeditor.action.openDeclarationToTheSide
kb(editor.action.quickFix)Quick Fixeditor.action.quickFix
kb(editor.action.referenceSearch.trigger)Show Referenceseditor.action.referenceSearch.trigger
kb(editor.action.rename)Rename Symboleditor.action.rename
kb(editor.action.inPlaceReplace.down)Replace with Next Valueeditor.action.inPlaceReplace.down
kb(editor.action.inPlaceReplace.up)Replace with Previous Valueeditor.action.inPlaceReplace.up
kb(editor.action.smartSelect.grow)Expand AST Selecteditor.action.smartSelect.grow
kb(editor.action.smartSelect.shrink)Shrink AST Selecteditor.action.smartSelect.shrink
kb(editor.action.trimTrailingWhitespace)Trim Trailing Whitespaceeditor.action.trimTrailingWhitespace
kb(workbench.action.editor.changeLanguageMode)Change Language Modeworkbench.action.editor.changeLanguageMode

导航(Navigation)

KeyCommandCommand id
kb(workbench.action.showAllSymbols)Show All Symbolsworkbench.action.showAllSymbols
kb(workbench.action.gotoLine)Go to Line...workbench.action.gotoLine
kb(workbench.action.quickOpen)Go to File..., Quick Openworkbench.action.quickOpen
kb(workbench.action.gotoSymbol)Go to Symbol...workbench.action.gotoSymbol
kb(workbench.action.showErrorsWarnings)Show Errors and Warningsworkbench.action.showErrorsWarnings
kb(editor.action.marker.next)Go to Next Error or Warningeditor.action.marker.next
kb(editor.action.marker.prev)Go to Previous Error or Warningeditor.action.marker.prev
kb(workbench.action.showCommands)Show All Commandsworkbench.action.showCommands
kb(workbench.action.openPreviousEditor)Navigate Historyworkbench.action.openPreviousEditor
kb(workbench.action.navigateBack)Go Backworkbench.action.navigateBack
kb(workbench.action.navigateForward)Go Forwardworkbench.action.navigateForward

编辑器/窗口管理(Editor/Window Management)

KeyCommandCommand id
kb(workbench.action.newWindow)New Windowworkbench.action.newWindow
kb(workbench.action.closeWindow)Close Windowworkbench.action.closeWindow
kb(workbench.action.closeActiveEditor)Close Editorworkbench.action.closeActiveEditor
kb(workbench.action.closeFolder)Close Folderworkbench.action.closeFolder
kb(workbench.action.cycleEditor)Cycle Between Opened Editorsworkbench.action.cycleEditor
kb(workbench.action.splitEditor)Split Editorworkbench.action.splitEditor
kb(workbench.action.focusFirstEditor)Focus into Left Hand Editorworkbench.action.focusFirstEditor
kb(workbench.action.focusSecondEditor)Focus into Side Editorworkbench.action.focusSecondEditor
kb(workbench.action.focusThirdEditor)Focus into Right Hand Editorworkbench.action.focusThirdEditor
kb(workbench.action.focusLeftEditor)Focus into Next Editor on the Leftworkbench.action.focusLeftEditor
kb(workbench.action.focusRightEditor)Focus into Next Editor on the Rightworkbench.action.focusRightEditor
kb(workbench.action.moveActiveEditorLeft)Move Active Editor Leftworkbench.action.moveActiveEditorLeft
kb(workbench.action.moveActiveEditorRight)Move Active Editor Rightworkbench.action.moveActiveEditorRight

文件管理(File Management)

KeyCommandCommand id
kb(workbench.action.files.newUntitledFile)New Fileworkbench.action.files.newUntitledFile
kb(workbench.action.files.openFile)Open File...workbench.action.files.openFile
kb(workbench.action.files.save)Saveworkbench.action.files.save
kb(workbench.action.files.saveAll)Save Allworkbench.action.files.saveAll
kb(workbench.action.files.saveAs)Save As...workbench.action.files.saveAs
kb(workbench.files.action.closeFile)Close Fileworkbench.files.action.closeFile
kb(workbench.files.action.closeAllFiles)Close All Filesworkbench.files.action.closeAllFiles
kb(workbench.files.action.closeOtherFiles)Close Other Filesworkbench.files.action.closeOtherFiles
kb(workbench.files.action.addToWorkingFiles)Add to Working Filesworkbench.files.action.addToWorkingFiles
kb(workbench.files.action.openNextWorkingFile)Open Next Working Fileworkbench.files.action.openNextWorkingFile
kb(workbench.files.action.openPreviousWorkingFile)Open Previous Working Fileworkbench.files.action.openPreviousWorkingFile
kb(workbench.action.files.copyPathOfActiveFile)Copy Path of Active Fileworkbench.action.files.copyPathOfActiveFile
kb(workbench.action.files.revealActiveFileInWindows)Reveal Active File in Windowsworkbench.action.files.revealActiveFileInWindows
kb(workbench.action.files.showOpenedFileInNewWindow)Show Opened File in New Windowworkbench.action.files.showOpenedFileInNewWindow
kb(workbench.files.action.compareFileWith)Compare Opened File Withworkbench.files.action.compareFileWith

Display

KeyCommandCommand id
kb(workbench.action.toggleFullScreen)Toggle Full Screenworkbench.action.toggleFullScreen
kb(workbench.action.zoomIn)Zoom inworkbench.action.zoomIn
kb(workbench.action.zoomOut)Zoom outworkbench.action.zoomOut
kb(workbench.action.toggleSidebarVisibility)Toggle Sidebar Visibilityworkbench.action.toggleSidebarVisibility
kb(workbench.view.debug)Show Debugworkbench.view.debug
kb(workbench.view.explorer)Show Explorerworkbench.view.explorer
kb(workbench.view.git)Show Gitworkbench.view.git
kb(workbench.view.search)Show Searchworkbench.view.search
kb(workbench.action.search.toggleQueryDetails)Toggle Search Detailsworkbench.action.search.toggleQueryDetails
kb(workbench.action.terminal.openNativeConsole)Open New Command Promptworkbench.action.terminal.openNativeConsole
kb(workbench.action.output.toggleOutput)Show Outputworkbench.action.output.toggleOutput
kb(workbench.action.markdown.togglePreview)Toggle Markdown Previewworkbench.action.markdown.togglePreview
kb(workbench.action.markdown.openPreviewSideBySide)Open Preview to the Sideworkbench.action.markdown.openPreviewSideBySide

界面(Preferences)

KeyCommandCommand id
kb(workbench.action.openGlobalSettings)Open User Settingsworkbench.action.openGlobalSettings
kb(workbench.action.openWorkspaceSettings)Open Workspace Settingsworkbench.action.openWorkspaceSettings
kb(workbench.action.openGlobalKeybindings)Open Keyboard Shortcutsworkbench.action.openGlobalKeybindings
kb(workbench.action.openSnippets)Open User Snippetsworkbench.action.openSnippets
kb(workbench.action.selectTheme)Select Color Themeworkbench.action.selectTheme
kb(workbench.action.configureLocale)Configure Display Languageworkbench.action.configureLocale

调试(Debug)

KeyCommandCommand id
kb(editor.debug.action.toggleBreakpoint)Toggle Breakpointeditor.debug.action.toggleBreakpoint
kb(workbench.action.debug.continue)Continueworkbench.action.debug.continue
kb(workbench.action.debug.start)Pauseworkbench.action.debug.start
kb(workbench.action.debug.stepInto)Step Intoworkbench.action.debug.stepInto
kb(workbench.action.debug.stepOut)Step Outworkbench.action.debug.stepOut
kb(workbench.action.debug.stepOver)Step Overworkbench.action.debug.stepOver
kb(workbench.action.debug.stop)Stopworkbench.action.debug.stop
kb(editor.action.showHover)Show Hovereditor.action.showHover

任务(Tasks)

KeyCommandCommand id
kb(workbench.action.tasks.build)Run Build Taskworkbench.action.tasks.build
kb(workbench.action.tasks.test)Run Test Taskworkbench.action.tasks.test

扩展(Extensions)

KeyCommandCommand id
kb(workbench.extensions.action.installExtension)Install Extensionworkbench.extensions.action.installExtension
kb(workbench.extensions.action.listExtensions)Show Installed Extensionsworkbench.extensions.action.listExtensions
kb(workbench.extensions.action.listOutdatedExtensions)Show Outdated Extensionsworkbench.extensions.action.listOutdatedExtensions
kb(workbench.extensions.action.listSuggestedExtensions)Show Extension Recommendationsworkbench.extensions.action.listSuggestedExtensions

自定义快捷键(Customizing Shortcuts)

All keyboard shortcuts in VS Code can be customized via the User/keybindings.json file.

VS Code 中的所有键盘快捷键都可以通过 User/keybindings.json 文件进行自定义。

  • To configure keyboard shortcuts the way you want, go to the menu under File > Preferences > Keyboard Shortcuts.

  • This will open the Default Keyboard Shortcuts on the left and your User/keybindings.json file where you can overwrite the default bindings on the right.

  • 要根据需要配置键盘快捷键,请转到 文件>首选项>键盘快捷键 下的菜单。

  • 这将打开左侧的默认键盘快捷键和您的 User/keybindings.json 文件,您可以在右侧文件中,覆盖左侧的默认绑定。

快捷键规则(Keyboard Rules)

The keyboard shortcuts dispatching is done by analyzing a list of rules that are expressed in JSON. Here are some examples:

键盘快捷键分配通过以 JSON 表示的规则列表来完成。下面是一些例子:

// Keybindings that are active when the focus is in the editor
{ "key": "home",       "when": "editorTextFocus", "command": "cursorHome" },
{ "key": "shift+home", "when": "editorTextFocus", "command": "cursorHomeSelect" },

// Keybindings that are complementary
{ "key": "f5",         "when": "inDebugMode",     "command": "workbench.action.debug.continue" },
{ "key": "f5",         "when": "!inDebugMode",    "command": "workbench.action.debug.start" }

// Global keybindings
{ "key": "ctrl+f",                                "command": "actions.find" },
{ "key": "alt+left",                              "command": "workbench.action.navigateBack" },
{ "key": "alt+right",                             "command": "workbench.action.navigateForward" },

// Global keybindings using chords
{ "key": "ctrl+k enter",                          "command": "workbench.files.action.addToWorkingFiles" },
{ "key": "ctrl+k ctrl+w",                         "command": "workbench.files.action.closeAllFiles" },

Each rule consists of:

每个规则包括:

  • a required key that describes the pressed keys.

  • an optional when containing a boolean expression that will be evaluated depending on the current context.

  • an optional command containing the identifier of the command to execute.

  • 描述按下的键的 必需键 key

  • when 包含将根据当前上下文进行求值的布尔表达式,非必须。

  • 一个包含要执行的 命令的标识符可选命令

When a key is pressed:

当按下一个键:

  • the rules are evaluated from bottom to top.

  • the first rule that matches, both the key and in terms of when, is accepted.

  • no more rules are processed.

  • if a rule is found and has a command set, the command is executed.

  • 快捷键规则从底层到顶层寻找,顶层(用户设置)会覆盖默认。

  • 匹配的第一个规则,无论是 key 还是 when ,都被接受。

  • 不再处理任何规则。

  • 如果发现规则并且具有 命令集,则按照命令集合依次执行命令。

The additional User/keybindings.json rules are appended at runtime to the bottom of the default rules, thus allowing them to overwrite the default rules. The User/keybindings.json file is watched by VS Code so editing it while VS Code is running will update the rules at runtime.

附加的 User/keybindings.json 规则在运行时附加到默认规则的底部,从而允许它们覆盖默认规则。 VS Code 监视 User/keybindings.json 文件,因此在VS Code运行时进行编辑将立即更新规则。

接受的键(Accepted keys)

The key is made up of modifiers and the key itself.

key 由修改符和 key 本身组成。

The following modifiers are accepted:

接受以下修饰符:

OS|Modifiers --|--------- OS X|kbstyle(ctrl+), kbstyle(shift+), kbstyle(alt+), kbstyle(cmd+) Windows|kbstyle(ctrl+), kbstyle(shift+), kbstyle(alt+), kbstyle(win+) Linux|kbstyle(ctrl+), kbstyle(shift+), kbstyle(alt+), kbstyle(meta+)

The following keys are accepted:

接受以下键:

  • kbstyle(f1-f19), kbstyle(a-z), kbstyle(0-9)
  • kbstyle(`), kbstyle(-), kbstyle(=), kbstyle([), kbstyle(]), kbstyle(\), kbstyle(;), kbstyle('), kbstyle(,), kbstyle(.), kbstyle(/)
  • kbstyle(left), kbstyle(up), kbstyle(right), kbstyle(down), kbstyle(pageup), kbstyle(pagedown), kbstyle(end), kbstyle(home)
  • kbstyle(tab), kbstyle(enter), kbstyle(escape), kbstyle(space), kbstyle(backspace), kbstyle(delete)
  • kbstyle(pausebreak), kbstyle(capslock), kbstyle(insert)
  • kbstyle(numpad0-numpad9), kbstyle(numpad_multiply), kbstyle(numpad_add), kbstyle(numpad_separator)
  • kbstyle(numpad_subtract), kbstyle(numpad_decimal), kbstyle(numpad_divide)

Chords are described by separating the two keypresses with a space. E.g.: kbstyle(ctrl+k ctrl+c).

通过用空格分隔两个按键来描述和弦。例如:kbstyle(ctrl + k ctrl + c)。

键盘布局(Keyboard layouts)

Note: This section relates only to key bindings, not to typing in the editor.

注意: 本节仅涉及键绑定,而不是在编辑器中键入。

The keys above are string representations for virtual keys and do not necessarily relate to the produced character when they are pressed. More precisely:

上面的键是虚拟键的字符串形式表示,并且当它们被按下时不与所产生的字符相关。也就是说:

  • Reference: https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85)
  • kbstyle(tab) for VK_TAB (0x09)
  • kbstyle(;) for VK_OEM_1 (0xBA)
  • kbstyle(=) for VK_OEM_PLUS (0xBB)
  • kbstyle(,) for VK_OEM_COMMA (0xBC)
  • kbstyle(-) for VK_OEM_MINUS (0xBD)
  • kbstyle(.) for VK_OEM_PERIOD (0xBE)
  • kbstyle(/) for VK_OEM_2 (0xBF)
  • kbstyle(`) for VK_OEM_3 (0xC0)
  • kbstyle([) for VK_OEM_4 (0xDB)
  • kbstyle(\) for VK_OEM_5 (0xDC)
  • kbstyle(]) for VK_OEM_6 (0xDD)
  • kbstyle(') for VK_OEM_7 (0xDE)
  • etc.

Different keyboard layouts usually reposition the above virtual keys or change the characters produced when they are pressed. When using a different keyboard layout than the standard US, Visual Studio Code does the following:

不同的键盘布局通常重新定位上述虚拟键或改变按下时产生的字符。当使用与标准美国不同的键盘布局时,Visual Studio Code执行以下操作:

All the key bindings are rendered in the UI using the current system's keyboard layout. For example, Split Editor when using a French (France) keyboard layout is now rendered as kbstyle(Ctrl+*):

所有的键绑定都使用当前系统的键盘布局在UI中呈现。例如,使用法语(法国)键盘布局时的 Split Editor 现在呈现为 kbstyle(Ctrl + *)

render key binding

When editing keybindings.json, VS Code highlights misleading key bindings - those that are represented in the file with the character produced under the standard US keyboard layout, but which need pressing keys with different labels under the current system's keyboard layout. For example, here is how the Default keybindings rules look like when using a French (France) keyboard layout:

当编辑 keybindings.json 时,VS Code 突出显示误导键绑定 - 在文件中用在美国标准键盘布局下产生的字符表示的那些,但是需要在当前系统的键盘布局下按下具有不同标签的键。例如,以下是使用法语(法国)键盘布局时 默认键盘 绑定规则的外观:

keybindings.json guidance

There is also a widget that helps input the key binding rule when editing keybindings.json. To launch the Define Keybinding widget, press kb(editor.action.defineKeybinding). The widget listens for key presses and renders the serialized JSON representation in the text box and below it, the keys that VS Code has detected under your current keyboard layout. Once you've typed the key combination you want, you can press kbstyle(Enter) and a rule snippet will be inserted.

还有一个小部件,用于在编辑 keybindings.json 时帮助输入键绑定规则。要启动定义键绑定小部件,请按 kb(editor.action.defineKeybinding)。窗口小部件监听按键并在文本框及其下面呈现序列化的 JSON 表示,VS Code 在您当前的键盘布局下检测到的键。键入所需的组合键后,您可以按 kbstyle(Enter),然后插入规则片段。

key binding widget

Note: Visual Studio Code detects your current keyboard layout on start-up and then caches this information. For a good experience, we recommend restarting VS Code if you change your keyboard layout.

注意: Visual Studio Code 在启动时检测您当前的键盘布局,然后缓存此信息。为了获得良好的体验,我们建议您在更改键盘布局时重新启动VS Code。

下一步(Next Steps)

Now that you know about our Key binding support, what's next...

现在你知道了我们支持快捷键自定义,接下来是什么...

  • Customization - Configure Code the way you want - Themes, Settings and more
  • Language Support - Our Good, Better, Best language grid to see what you can expect
  • Debugging - This is where VS Code really shines
  • Node.js - End to end Node.js scenario with a sample app

Common Questions

Q: How to find out what command is bound to a specific key?

A: In the Default Keyboard Shortcuts, open Quick Outline by pressing kb(workbench.action.gotoSymbol)

Key bindings quick outline

Q: How to add a key binding to an action? E.g. Add Ctrl+D to Delete Lines

A: Find a rule that triggers the action in the Default Keyboard Shortcuts and write a modified version of it in your User/keybindings.json file:

// Original, in Default Keyboard Shortcuts
{ "key": "ctrl+shift+k",          "command": "editor.action.deleteLines",
                                     "when": "editorTextFocus" },
// Modified, in User/keybindings.json, Ctrl+D now will also trigger this action
{ "key": "ctrl+d",                "command": "editor.action.deleteLines",
                                     "when": "editorTextFocus" },

Q: How to remove a key binding from an action? E.g. Remove Ctrl+Shift+K from Delete Lines

A: Find a rule that triggers the action in the Default Keyboard Shortcuts and write a modified version of it in your User/keybindings.json file:

// Original, in Default Keyboard Shortcuts
{ "key": "ctrl+shift+k",          "command": "editor.action.deleteLines",
                                     "when": "editorTextFocus" },
// Modified, in User/keybindings.json, Ctrl+Shift+K won't do anything anymore since command is empty
{ "key": "ctrl+shift+k",             "when": "editorTextFocus" },

Q: How can I add a key binding for only certain file types?

A: Use the editorLangId context key in your when clause:

{ "key": "shift+alt+a",           "command": "editor.action.blockComment",
                                     "when": "editorTextFocus && editorLangId == 'csharp'" },

Q: I have modified my key bindings in User/keybindings.json, why don't they work?

A: The most common problem is a syntax error in the file. Otherwise, try removing the when clause or picking a different key. Unfortunately, at this point, it is a trial and error process.