Visual Studio Code 鍵盤快速鍵

Visual Studio Code 讓您可以直接透過鍵盤執行大多數的工作。本文說明如何修改 VS Code 內建的預設鍵盤快速鍵。

注意

如果您在 Mac 上瀏覽此頁面,將會看到 Mac 的鍵盤快速鍵。如果使用 Windows or Linux 瀏覽,則會看到該平臺的按鍵。如果您需要其他平臺的鍵盤快速鍵,請將滑鼠游標停在您感興趣的按鍵上。

鍵盤快速鍵編輯器

VS Code 透過「鍵盤快速鍵」編輯器提供豐富的鍵盤快速鍵編輯體驗。此編輯器會列出所有可用的命令 (包含有或沒有鍵盤快速鍵的命令),並讓您使用可用的動作來變更、移除或重設其鍵盤快速鍵。若要尋找命令或鍵盤快速鍵,請使用搜尋方塊並輸入命令或快速鍵來篩選清單。

若要開啟「鍵盤快速鍵」編輯器,請選取 檔案 (File) > 偏好設定 (Preferences) > 鍵盤快速鍵 (Keyboard Shortcuts) 功能表,或在命令選擇區中使用 Preferences: Open Keyboard Shortcuts 命令 (⌘K ⌘S (Windows, Linux Ctrl+K Ctrl+S))。「鍵盤快速鍵」編輯器預設會在編輯器區域上方的互動視窗覆疊中開啟。

Keyboard Shortcuts

注意

鍵盤快速鍵會符合您目前的鍵盤配置。例如,在美式鍵盤配置中的鍵盤快速鍵 Cmd+\,當配置變更為德文時,會顯示為 Ctrl+Shift+Alt+Cmd+7。用於變更鍵盤快速鍵的對話方塊會根據您的鍵盤配置指派正確且所需的鍵盤快速鍵。

自訂 UI 動作的快速鍵

若要自訂 UI 動作的鍵盤快速鍵,請在工作臺中的任何動作項目上按一下滑鼠右鍵,然後選取 設定按鍵繫結 (Configure Keybinding)。這會開啟「鍵盤快速鍵」編輯器,並篩選至對應的命令。如果該動作具有 when 子句,系統會自動將其包含在內,讓您更容易按照所需方式設定鍵盤快速鍵。

鍵盤對應延伸模組

鍵盤對應延伸模組會修改 VS Code 快速鍵,使其符合其他編輯器的快速鍵,因此您不需要學習新的鍵盤快速鍵。

選取 檔案 (File) > 偏好設定 (Preferences) > 從以下來源移轉鍵盤快速鍵 (Migrate Keyboard Shortcuts from...) 功能表,以取得熱門的鍵盤對應延伸模組清單。Marketplace 中也有 Keymaps 類別的延伸模組。

鍵盤快速鍵參考

我們提供預設鍵盤快速鍵的列印版本。選取 說明 (Help) > 鍵盤快速鍵參考 (Keyboard Shortcut Reference),以顯示適合列印的壓縮 PDF 版本,方便您隨時參考。

下列連結提供三個平臺專屬版本的存取權 (美式英文鍵盤)

偵測鍵盤快速鍵衝突

如果您安裝了許多延伸模組或修改過鍵盤快速鍵,可能會發生鍵盤快速鍵衝突,也就是相同的鍵盤快速鍵被對應到多個命令。這可能會導致混淆的行為,特別是當不同的鍵盤快速鍵在您於編輯器中移動時進出範圍時。

在鍵盤快速鍵清單中的項目上按一下滑鼠右鍵,然後選取 顯示相同的按鍵繫結 (Show Same Keybindings),以檢視具有相同鍵盤快速鍵的所有項目。

show keyboard shortcut conflicts menu

疑難排解鍵盤快速鍵

若要對鍵盤快速鍵問題進行疑難排解,您可以執行 Developer: Toggle Keyboard Shortcuts Troubleshooting 命令。這會啟用已分派鍵盤快速鍵的記錄,並開啟包含對應記錄檔的「輸出」面板。

接著,您可以按下所需的鍵盤快速鍵,並檢查 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.

在範例記錄中,第一個按鍵按下事件是針對 MetaLeft 按鍵 (cmd),且無法分派。第二個按鍵按下事件是針對 Slash 按鍵 (/),並被分派為 meta+[Slash]。有兩個鍵盤快速鍵項目從 meta+[Slash] 對應,而相符的項目是針對 editor.action.commentLine 命令,該命令具有 editorTextFocus && !editorReadonlywhen 條件,且為內建的鍵盤快速鍵項目。

檢視已修改的鍵盤快速鍵

若要篩選清單以僅顯示您修改過的快速鍵,請選取 更多動作 (...) 功能表中的 顯示使用者按鍵繫結 (Show User Keybindings) 命令。這會將 @source:user 篩選條件套用至「鍵盤快速鍵」編輯器 (**來源 (Source)** 為 'User')。

Default Keyboard Shortcuts

進階自訂

VS Code 會在 keybindings.json 檔案中追蹤您自訂的鍵盤快速鍵。若要進行進階自訂,您也可以直接修改 keybindings.json 檔案。

若要開啟 keybindings.json 檔案

  • 開啟鍵盤快速鍵編輯器,然後選取編輯器標題列右側的開啟鍵盤快速鍵 (JSON) 按鈕。

    Open Keyboard Shortcuts JSON button

  • 或者,在命令選擇區中使用 開啟預設鍵盤快速鍵 (JSON) 命令 (⇧⌘P (Windows, Linux Ctrl+Shift+P))。

鍵盤規則

VS Code 中的鍵盤快速鍵設定也稱為鍵盤規則。每個規則由下列屬性組成

  • key:描述按下的按鍵,例如 ⌘F (Windows, Linux Ctrl+F)
  • command:要執行的 VS Code 命令識別碼,例如 workbench.view.explorer 用於開啟「總管」檢視。
  • when:(選擇性)包含布林運算式的子句,該運算式會根據目前的內容進行評估。

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

當按下按鍵時,會套用下列評估規則

  • 規則是從底部頂部進行評估的。
  • 同時符合 keywhen 子句的第一個規則會被接受。
  • 如果找到規則,就不會再處理其他規則。
  • 如果找到規則且已設定 command,就會執行該 command

其他的 keybindings.json 規則會在執行階段附加到預設規則的底部,因此允許它們覆寫預設規則。keybindings.json 檔案受到 VS Code 的監視,因此在 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] ]

移除鍵盤快速鍵

若要移除鍵盤快速鍵,請在「鍵盤快速鍵」編輯器中的項目上按一下滑鼠右鍵,然後選取 移除按鍵繫結 (Remove Keybinding)

若要透過 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)
  • 用於 VK_TAB (0x09) 的 tab
  • 用於 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 中的所有鍵盤快速鍵都會使用目前系統的鍵盤配置來呈現。例如,當使用法文 (法國) 鍵盤配置時,Split Editor 現在會呈現為 Ctrl+*

render keyboard shortcut

在編輯 keybindings.json 時,VS Code 會反白顯示容易混淆的鍵盤快速鍵,也就是在檔案中以標準美式鍵盤配置下產生的字元表示,但在目前系統鍵盤配置下需要按下具有不同標籤之按鍵的快速鍵。例如,以下是使用法文 (法國) 鍵盤配置時預設鍵盤快速鍵規則的樣子

keybindings.json guidance

也有一個 UI 控制項可在編輯 keybindings.json 時協助輸入鍵盤快速鍵規則。若要啟動 定義按鍵繫結 (Define Keybinding) 控制項,請按下 ⌘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 中可見且作用中的元素,設定各種內容按鍵和特定值。例如,內建的 開始偵錯 (Start Debugging) 命令具有鍵盤快速鍵 F5,該快速鍵僅在有適當的偵錯工具可用 (內容 debuggersAvailable 為 true) 且編輯器不在偵錯模式中 (內容 inDebugMode 為 false) 時才會啟用

Start Debugging when clause in the Keyboard Shorts editor

您也可以直接在預設的 keybinding.json 中檢視鍵盤快速鍵的 when 子句 (偏好設定:開啟預設鍵盤快速鍵 (JSON))

{ "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 命令讓您可以針對特定重構 (程式碼動作) 設定鍵盤快速鍵。例如,下列鍵盤快速鍵會觸發 擷取函式 (Extract function) 重構程式碼動作

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

這在重構一文中已有深入探討,您可以在其中了解不同種類的程式碼動作,以及在有多種可能重構的情況下如何為其設定優先順序。

常見問題

如何找出哪個命令繫結至特定的按鍵?

在「鍵盤快速鍵」編輯器中,您可以篩選特定的按鍵輸入,以查看哪些命令繫結至哪些按鍵。在下列螢幕擷取畫面中,您可以看到 Ctrl+Shift+P 已繫結至 顯示所有命令 (Show All Commands) 以叫用命令選擇區。

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。不幸的是,此時只能透過不斷嘗試與除錯來解決。

English 한국어 中文(简体) 中文(繁體)
© . This website operates independently and is not affiliated with or endorsed by Microsoft. All brand names, logos, and trademarks are the property of their respective owners.