參加你附近的 ,瞭解 VS Code 中的 AI 輔助開發。

Visual Studio Code 的鍵盤快捷方式

Visual Studio Code 允許您直接透過鍵盤執行大多數任務。本文介紹如何修改 VS Code 自帶的預設鍵盤快捷方式。

注意

如果您在 Mac 上訪問此頁面,您將看到 Mac 的鍵盤快捷方式。如果您使用 Windows 或 Linux 訪問,您將看到適用於該平臺的按鍵。如果您需要其他平臺的鍵盤快捷方式,請將滑鼠懸停在您感興趣的按鍵上。

鍵盤快捷方式編輯器

VS Code 透過“鍵盤快捷方式”編輯器提供了豐富的鍵盤快捷方式編輯體驗。該編輯器列出了所有可用命令(無論有無鍵盤快捷方式),並允許您使用可用操作來更改、刪除或重置它們的鍵盤快捷方式。要查詢命令或鍵盤快捷方式,請使用搜索框並輸入命令或快捷方式來篩選列表。

要開啟“鍵盤快捷方式”編輯器,請選擇選單 檔案 > 首選項 > 鍵盤快捷方式,或在命令面板中使用 首選項:開啟鍵盤快捷方式 命令 (⌘K ⌘S (Windows、Linux 為 Ctrl+K Ctrl+S))。

Keyboard Shortcuts

注意

鍵盤快捷方式會匹配您當前的鍵盤佈局。例如,在美國鍵盤佈局下的快捷方式 Cmd+\,當佈局更改為德語時,將顯示為 Ctrl+Shift+Alt+Cmd+7。用於更改鍵盤快捷方式的對話方塊會根據您的鍵盤佈局分配正確且所需的鍵盤快捷方式。

為 UI 操作自定義快捷方式

要為 UI 操作自定義鍵盤快捷方式,請在工作臺中的任何操作項上右鍵單擊,然後選擇 配置鍵繫結。這會開啟“鍵盤快捷方式”編輯器,並篩選到相應的命令。如果該操作有 when 子句,它也會被自動包含進來,從而讓您更輕鬆地按照自己的需要設定鍵盤快捷方式。

鍵盤對映擴充套件

鍵盤對映擴充套件可以修改 VS Code 的快捷方式,使其與其他編輯器的快捷方式相匹配,這樣您就不需要學習新的鍵盤快捷方式了。

選擇選單 檔案 > 首選項 > 從...遷移鍵盤快捷方式,可以獲取一個熱門鍵盤對映擴充套件的列表。在 Marketplace 中也有一個 鍵盤對映類別 的擴充套件。

鍵盤快捷方式參考

我們提供了一個可列印的預設鍵盤快捷方式版本。選擇 幫助 > 鍵盤快捷方式參考,可以顯示一個簡明的 PDF 版本,適合打印出來以便參考。

以下連結提供了三個特定平臺版本的訪問(美式英語鍵盤):

檢測鍵盤快捷方式衝突

如果您安裝了許多擴充套件或修改了鍵盤快捷方式,可能會出現鍵盤快捷方式衝突,即同一個鍵盤快捷方式被對映到多個命令。這可能會導致令人困惑的行為,尤其是在您在編輯器中移動時,不同的鍵盤快捷方式的作用域會進進出出。

在鍵盤快捷方式列表中的某個專案上右鍵單擊,然後選擇 顯示相同的鍵繫結,以檢視所有使用相同鍵盤快捷方式的條目。

show keyboard shortcut conflicts menu

鍵盤快捷方式問題排查

要排查鍵盤快捷方式問題,您可以執行命令 開發人員:切換鍵盤快捷方式問題排查。這將啟用對已分派鍵盤快捷方式的日誌記錄,並開啟帶有相應日誌檔案的“輸出”面板。

然後您可以按下您想要的鍵盤快捷方式,並檢查 VS Code 檢測到的是哪個快捷方式以及呼叫了哪個命令。

例如,在 macOS 的程式碼編輯器中按下 cmd+/ 時,日誌輸出會是:

[KeybindingService]: / Received  keydown event - modifiers: [meta], code: MetaLeft, keyCode: 91, key: Meta
[KeybindingService]: | Converted keydown event - modifiers: [meta], code: MetaLeft, keyCode: 57 ('Meta')
[KeybindingService]: \ Keyboard event cannot be dispatched.
[KeybindingService]: / Received  keydown event - modifiers: [meta], code: Slash, keyCode: 191, key: /
[KeybindingService]: | Converted keydown event - modifiers: [meta], code: Slash, keyCode: 85 ('/')
[KeybindingService]: | Resolving meta+[Slash]
[KeybindingService]: \ From 2 keybinding entries, matched editor.action.commentLine, when: editorTextFocus && !editorReadonly, source: built-in.

在示例日誌中,第一個 keydown 事件是針對 MetaLeft 鍵(cmd)的,無法被分派。第二個 keydown 事件是針對 Slash 鍵(/)的,被分派為 meta+[Slash]。有兩個鍵盤快捷方式條目從 meta+[Slash] 對映而來,匹配的是命令 editor.action.commentLine,它具有 when 條件 editorTextFocus && !editorReadonly,並且是一個內建的鍵盤快捷方式條目。

檢視已修改的鍵盤快捷方式

要篩選列表以僅顯示您已修改的快捷方式,請在 更多操作 (...) 選單中選擇 顯示使用者鍵繫結 命令。這會將 @source:user 篩選器應用到 鍵盤快捷方式 編輯器(來源 為“使用者”)。

Default Keyboard Shortcuts

高階自定義

VS Code 會在 keybindings.json 檔案中跟蹤您已自定義的鍵盤快捷方式。對於高階自定義,您也可以直接修改 keybindings.json 檔案。

要開啟 keybindings.json 檔案:

  • 開啟 鍵盤快捷方式 編輯器,然後選擇編輯器標題欄右側的 開啟鍵盤快捷方式 (JSON) 按鈕。

    Open Keyboard Shortcuts JSON button

  • 或者,在命令面板 (⇧⌘P (Windows、Linux 為 Ctrl+Shift+P)) 中使用 開啟預設鍵盤快捷方式 (JSON) 命令。

鍵盤規則

VS Code 中的鍵盤快捷方式配置也被稱為鍵盤規則。每條規則由以下屬性組成:

  • key:描述按下的一個或多個鍵,例如 ⌘F (Windows、Linux 為 Ctrl+F)
  • command:要執行的 VS Code 命令的識別符號,例如 workbench.view.explorer 用於開啟資源管理器檢視。
  • when:(可選)一個子句,包含一個布林表示式,該表示式會根據當前的上下文進行求值。

組合鍵(兩次獨立的按鍵操作)透過用空格分隔兩次按鍵來描述。例如,Ctrl+K Ctrl+C

當一個鍵被按下時,將應用以下評估規則:

  • 規則從進行評估。
  • 第一條同時匹配 keywhen 子句的規則將被接受。
  • 如果找到一條規則,則不再處理更多規則。
  • 如果找到一條規則且設定了 command,則執行該 command

額外的 keybindings.json 規則在執行時會附加到預設規則的底部,從而允許它們覆蓋預設規則。VS Code 會監視 keybindings.json 檔案,因此在 VS Code 執行時編輯它會即時更新規則。

鍵盤快捷方式的分派是透過分析一個用 JSON 表示的規則列表來完成的。以下是一些示例:

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

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

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

// Global keyboard shortcuts using chords (two separate keypress actions)
{ "key": "ctrl+k enter",    "command": "workbench.action.keepEditor" },
{ "key": "ctrl+k ctrl+w",   "command": "workbench.action.closeAllEditors" },

可接受的按鍵

key 由修飾鍵和按鍵本身組成。

接受以下修飾鍵:

平臺 修飾鍵
macOS Ctrl+, Shift+, Alt+, Cmd+
Windows Ctrl+, Shift+, Alt+, Win+
Linux Ctrl+, Shift+, Alt+, Meta+

接受以下按鍵:

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

命令引數

您可以帶引數呼叫命令。如果您經常對特定檔案或資料夾執行相同的操作,這將非常有用。您可以新增一個自定義的鍵盤快捷方式來精確執行您想要的操作。

以下是一個覆蓋 Enter 鍵以列印一些文字的示例:

{
  "key": "enter",
  "command": "type",
  "args": { "text": "Hello World" },
  "when": "editorTextFocus"
}

type 命令將收到 {"text": "Hello World"} 作為其第一個引數,並在檔案中新增“Hello World”,而不是執行預設命令。

有關接受引數的命令的更多資訊,請參閱內建命令

執行多個命令

可以使用 runCommands 命令將一個鍵盤快捷方式配置為按順序執行多個命令。

  • 執行多個不帶引數的命令

    以下示例將當前行向下複製,將當前行標記為註釋,並將游標移動到複製的行。

    {
      "key": "ctrl+alt+c",
      "command": "runCommands",
      "args": {
        "commands": [
          "editor.action.copyLinesDownAction",
          "cursorUp",
          "editor.action.addCommentLine",
          "cursorDown"
        ]
      }
    },
    
  • 向命令傳遞引數

    此示例建立一個新的未命名 TypeScript 檔案並插入一個自定義程式碼片段。

    {
      "key": "ctrl+n",
      "command": "runCommands",
      "args": {
        "commands": [
          {
            "command": "workbench.action.files.newUntitledFile",
            "args": {
              "languageId": "typescript"
            }
          },
          {
            "command": "editor.action.insertSnippet",
            "args": {
              "langId": "typescript",
              "snippet": "class ${1:ClassName} {\n\tconstructor() {\n\t\t$0\n\t}\n}"
            }
          }
        ]
      }
    },
    

請注意,由 runCommands 執行的命令將 "args" 的值作為第一個引數接收。在前面的示例中,workbench.action.files.newUntitledFile 接收 {"languageId": "typescript" } 作為其第一個也是唯一的引數。

要傳遞多個引數,您需要將 "args" 設定為一個數組:

{
  "key": "ctrl+shift+e",
  "command": "runCommands",
  "args": {
    "commands": [
      {
        // command invoked with 2 arguments: vscode.executeCommand("myCommand", "arg1", "arg2")
        "command": "myCommand",
        "args": ["arg1", "arg2"]
      }
    ]
  }
}

要將一個數組作為第一個引數傳遞,請將該陣列包裝在另一個數組中:"args": [ [1, 2, 3] ]

刪除鍵盤快捷方式

要刪除一個鍵盤快捷方式,請在“鍵盤快捷方式”編輯器中的條目上右鍵單擊,然後選擇 刪除鍵繫結

要使用 keybindings.json 檔案刪除鍵盤快捷方式,請在 command 前新增一個 -,該規則將成為一條刪除規則。

這是一個示例:

// In Default Keyboard Shortcuts
...
{ "key": "tab", "command": "tab", "when": ... },
{ "key": "tab", "command": "jumpToNextSnippetPlaceholder", "when": ... },
{ "key": "tab", "command": "acceptSelectedSuggestion", "when": ... },
...

// To remove the second rule, for example, add in keybindings.json:
{ "key": "tab", "command": "-jumpToNextSnippetPlaceholder" }

要用一個空操作覆蓋特定的鍵盤快捷方式規則,您可以指定一個空命令:

// To override and disable any `tab` keyboard shortcut, for example, add in keybindings.json:
{ "key": "tab", "command": "" }

鍵盤佈局

注意

本節僅涉及鍵盤快捷方式,與在編輯器中輸入文字無關。

按鍵是虛擬鍵的字串表示,不一定與按下它們時產生的字元相關。更準確地說:

  • 參考:虛擬鍵程式碼 (Windows)
  • tab 對應 VK_TAB (0x09)
  • ; 對應 VK_OEM_1 (0xBA)
  • = 對應 VK_OEM_PLUS (0xBB)
  • , 對應 VK_OEM_COMMA (0xBC)
  • - 對應 VK_OEM_MINUS (0xBD)
  • . 對應 VK_OEM_PERIOD (0xBE)
  • / 對應 VK_OEM_2 (0xBF)
  • ` 對應 VK_OEM_3 (0xC0)
  • [ 對應 VK_OEM_4 (0xDB)
  • \ 對應 VK_OEM_5 (0xDC)
  • ] 對應 VK_OEM_6 (0xDD)
  • ' 對應 VK_OEM_7 (0xDE)
  • 等等。

不同的鍵盤佈局通常會重新定位這些虛擬鍵或更改按下它們時產生的字元。當使用非標準美國鍵盤佈局時,Visual Studio Code 會執行以下操作:

所有的鍵盤快捷方式都會在 UI 中使用當前系統的鍵盤佈局進行渲染。例如,在使用法語(法國)鍵盤佈局時,拆分編輯器 現在被渲染為 Ctrl+*

render keyboard shortcut

在編輯 keybindings.json 時,VS Code 會高亮顯示具有誤導性的鍵盤快捷方式,這些快捷方式在檔案中是用標準美國鍵盤佈局下產生的字元來表示的,但在當前系統鍵盤佈局下需要按不同標籤的鍵。例如,以下是使用法語(法國)鍵盤佈局時預設鍵盤快捷方式規則的外觀:

keybindings.json guidance

還有一個 UI 控制元件可以幫助在編輯 keybindings.json 時輸入鍵盤快捷方式規則。要啟動 定義鍵繫結 控制元件,請按 ⌘K ⌘K (Windows、Linux 為 Ctrl+K Ctrl+K)。該控制元件會監聽按鍵,並在文字框中及其下方渲染序列化後的 JSON 表示,以及 VS Code 在您當前鍵盤佈局下檢測到的按鍵。一旦您輸入了想要的組合鍵,可以按 Enter,就會插入一條規則片段。

keyboard shortcut widget

注意

在 Linux 上,VS Code 會在啟動時檢測您當前的鍵盤佈局,然後快取此資訊。我們建議您在更改鍵盤佈局後重新啟動 VS Code。

與鍵盤佈局無關的繫結

使用掃描碼,可以定義不隨鍵盤佈局變化而改變的鍵盤快捷方式。例如:

{ "key": "cmd+[Slash]", "command": "editor.action.commentLine", "when": "editorTextFocus" }

可接受的掃描碼

  • [F1]-[F19], [KeyA]-[KeyZ], [Digit0]-[Digit9]
  • [Backquote], [Minus], [Equal], [BracketLeft], [BracketRight], [Backslash], [Semicolon], [Quote], [Comma], [Period], [Slash]
  • [ArrowLeft], [ArrowUp], [ArrowRight], [ArrowDown], [PageUp], [PageDown], [End], [Home]
  • [Tab], [Enter], [Escape], [Space], [Backspace], [Delete]
  • [Pause], [CapsLock], [Insert]
  • [Numpad0]-[Numpad9], [NumpadMultiply], [NumpadAdd], [NumpadComma]
  • [NumpadSubtract], [NumpadDecimal], [NumpadDivide]

when 子句上下文

VS Code 透過可選的 when 子句,讓您精確控制鍵盤快捷方式何時啟用。如果您的鍵盤快捷方式沒有 when 子句,那麼它將始終全域性可用。when 子句會求值為 true 或 false,以決定是否啟用鍵盤快捷方式。

VS Code 會根據 VS Code UI 中可見和活動的元素,設定各種上下文鍵和特定值。例如,內建的 開始除錯 命令的鍵盤快捷方式是 F5,它僅在有合適的偵錯程式可用(上下文 debuggersAvailable 為 true)且編輯器不處於除錯模式(上下文 inDebugMode 為 false)時才啟用。

Start Debugging when clause in the Keyboard Shorts editor

您也可以在預設的 keybindings.json (首選項:開啟預設鍵盤快捷方式 (JSON)) 中直接檢視鍵盤快捷方式的 when 子句。

{ "key": "f5",  "command": "workbench.action.debug.start",
                   "when": "debuggersAvailable && !inDebugMode" },

條件運算子

對於 when 子句的條件表示式,以下條件運算子對鍵盤快捷方式很有用:

運算子 符號 示例
等於 == "editorLangId == typescript"
不等於 != "resourceExtname != .js"
|| "isLinux||isWindows"
&& "textInputFocus && !editorReadonly"
匹配 =~ "resourceScheme =~ /^untitled$|^file$/"

您可以在 when 子句上下文 參考中找到 when 子句條件運算子的完整列表。

可用的上下文

您可以在 when 子句上下文參考 中找到一些可用的 when 子句上下文。

該列表並不詳盡,您可以透過在“鍵盤快捷方式”編輯器 (首選項:開啟鍵盤快捷方式) 中搜索和篩選,或檢視預設的 keybindings.json 檔案 (首選項:開啟預設鍵盤快捷方式 (JSON)) 來查詢其他 when 子句上下文。

為重構自定義鍵盤快捷方式

editor.action.codeAction 命令允許您為特定的重構(程式碼操作)配置鍵盤快捷方式。例如,下面的鍵盤快捷方式會觸發 提取函式 重構程式碼操作:

{
  "key": "ctrl+shift+r ctrl+e",
  "command": "editor.action.codeAction",
  "args": {
    "kind": "refactor.extract.function"
  }
}

這在重構文章中有深入介紹,您可以在其中瞭解不同型別的程式碼操作,以及在有多種可能的重構時如何確定它們的優先順序。

常見問題

我如何找出哪個命令繫結到了特定的鍵?

在“鍵盤快捷方式”編輯器中,您可以按特定的按鍵組合進行篩選,以檢視哪些命令繫結到了哪些鍵。在下面的截圖中,您可以看到 Ctrl+Shift+P 繫結到了 顯示所有命令,以調出命令面板。

Keyboard shortcuts quick outline

如何為一個操作新增鍵盤快捷方式,例如,為“刪除行”新增 Ctrl+D?

預設鍵盤快捷方式 中找到觸發該操作的規則,並在您的 keybindings.json 檔案中編寫一個修改後的版本:

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

我如何只為某些檔案型別新增鍵盤快捷方式?

在您的 when 子句中使用 editorLangId 上下文鍵:

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

我已經在 keybindings.json 中修改了我的鍵盤快捷方式;為什麼它們不生效?

最常見的問題是檔案中的語法錯誤。否則,請嘗試移除 when 子句或選擇一個不同的 key。不幸的是,在這一點上,這是一個反覆試驗的過程。