Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

visual studio code keybindings - Running two or more commands with one shortcut

I have the following keybinding in VS code which toggles the position of the cursor between the active document and built-in terminal:

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

Before i click the shortcut key to move the cursor to the terminal, i first have to save the active file.

I would therefore like to run the file saving command, which after searching on google i believe is :workbench.action.files.save

How would i do this please? i have tried adding the above code snippet at the end of the "command" line but it has not worked.

cheers

like image 652
Nick Avatar asked Apr 02 '18 12:04

Nick


People also ask

How do I run multiple commands in VS Code?

You can always mark your 2 most common commands with isBuildCommand and isTestCommand to run them via cmd + shift + b or cmd + shift + t respectively. This answer has some helpful information that might be useful to you as well.

What is Ctrl K in VS Code?

To launch the Define Keybinding widget, press Ctrl+K Ctrl+K. 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.

What is Ctrl Shift L in VS Code?

Press Ctrl + Shift + L . This will select all words that match your current selection. It also adds a cursor to each text span matching your current selection. Simply start typing your replacement text and vscode will start replacing all instances of text matching your selection in-editor as you type.

How do I make multiple cursors in VS Code?

VS Code supports multiple cursors for fast simultaneous edits. You can add secondary cursors (rendered thinner) with Alt+Click. Each cursor operates independently based on the context it sits in. A common way to add more cursors is with Shift+Alt+Down or Shift+Alt+Up that insert cursors below or above.


2 Answers

You would need a macro extension to run multiple commands from one keybinding.

I now use multi-command and there are other macro extensions now.

You can use this keybinding (in your keybindings.json) with the multi-command extension - no need for anything in settings.json:

{   "key": "oem_8",                            // or whatever keybinding you wish   "command": "extension.multiCommand.execute",   "args": {     "sequence": [       "workbench.action.files.save",       "workbench.action.terminal.focus"     ]   },   "when": "editorTextFocus"  // if you want this, you probably do } 

If you have more complicated macros you can still build them in your settings.json if you wish.

like image 96
Mark Avatar answered Sep 27 '22 20:09

Mark


Another extension to run multiple commands: Commands

{     "key": "oem_8",     "command": "commands.run",     "args": [         "workbench.action.files.save",         "workbench.action.terminal.focus"     ],     "when": "editorTextFocus" } 

I made this extension. It's great.

like image 33
Alex Avatar answered Sep 27 '22 19:09

Alex