如何实现切换至SCM时自动聚焦提交消息文本框?
Absolutely! You can make VS Code automatically focus the commit message text box whenever you open the SCM panel—here are two straightforward ways to set this up, covering both keyboard shortcuts and mouse clicks:
VS Code has a native setting made exactly for this scenario—no custom code required!
- Open your VS Code settings: Press
Ctrl+,(Windows/Linux) orCmd+,(Mac), or go to File > Preferences > Settings - In the search bar, type
scm focus commit message - Look for the setting labeled SCM: Focus Commit Message On Open and check the box next to it
That's all! Now whenever you open the SCM panel—whether by clicking the SCM icon in the activity bar or using the Ctrl+Shift+G shortcut—the commit message text box will automatically get focus.
If you want more control over the behavior (like only triggering this for the keyboard shortcut), you can chain the "open SCM" and "focus commit box" commands in your keybindings:
- Open the keyboard shortcuts JSON file: Press
Ctrl+K Ctrl+Sto open the shortcuts UI, then click the curly braces{}icon in the top-right corner - Add the following configuration to the file:
{ "key": "ctrl+shift+g", "command": "runCommands", "args": { "commands": [ "workbench.view.scm", "scm.inputBox.focus" ] }, "when": "!scmVisible" }
- The
runCommandsaction executes both commands in sequence: first it opens the SCM panel, then it focuses the commit message input - The
whenclause ensures this only runs when the SCM panel isn't already open, so you don't accidentally refocus the input if the panel is already visible
Either method will solve your problem perfectly. The built-in setting is the quickest way to cover all cases (mouse and keyboard), while the custom shortcut gives you extra flexibility if you need it.
内容的提问来源于stack exchange,提问作者solr

