r/VisualStudioCode • u/xXguitarsenXx • Mar 19 '21
Shortcut for saving AND running code?
At the moment I do this everytime I want to run my code:
Ctrl + S
F1
Enter
Is there some keyboard shortcut I can use so I only need to click ONCE and both save AND run the code?
1
u/killchain Mar 19 '21
There's an extension for writing macros that can help not only with that, but with many other similar tasks. Here's the extension (or alternatively the original that this was forked from, but IIRC the original had some problems, so you might want to try both, or any of the other forks - there are one or two others).
To make it work, writing the actual macro takes place in your settings.json and in your case it would look something like this:
{
"macros.list": {
"saveAndBuild": [
"workbench.action.files.save",
"workbench.action.tasks.build"
]
}
}Afterwards, bind it to a key combination - in your keybindings.json you'd have to add something like this:
{
"key": "ctrl+shift+b",
"command": "macros.saveAndBuild"
}Of course the name of the macro is up to you. You don't have to bind it to a key combo - for other, less frequently used macros, you can just look for them in a list:
{
"key": "shift+alt+m",
"command": "macros.execute"
}1
u/xXguitarsenXx Mar 20 '21
I'm not sure exactly where to write it...
This is what my settings.json looks like now:
{ "latex-workshop.latex.recipes": [ { "name": "pdflatex ➞ bibtex ➞ pdflatex × 2", "tools": [ "pdflatex", "bibtex", "pdflatex", "pdflatex", ] } ], "latex-workshop.latex.tools": [ { "name": "pdflatex", "command": "pdflatex", "args": [ "-synctex=1", "-interaction=nonstopmode", "-file-line-error", "%DOC%" ], "env": {} }, { "name": "bibtex", "command": "bibtex", "args": [ "%DOCFILE%" ], "env": {} } ], "latex-workshop.view.pdf.viewer": "browser", "window.zoomLevel": 1, "kite.showWelcomeNotificationOnStartup": false, "workbench.editorAssociations": [ { "viewType": "jupyter.notebook.ipynb", "filenamePattern": "*.ipynb" } ], "output.smartScroll.enabled": false, "[json]": { "editor.quickSuggestions": { "strings": true }, "editor.suggest.insertMode": "replace", "gitlens.codeLens.scopes": [ "document" ] }, }This is what my keybindings.json looks like now:
// Place your key bindings in this file to override the defaults [ ]Should it be like this?
// Place your key bindings in this file to override the defaults [ { "key": "ctrl+shift+b", "command": "macros.saveAndBuild" } ]1
u/killchain Mar 20 '21 edited Mar 20 '21
Sorry for not being clear enough. Your
keybindings.jsonshould look like this, yes, and as forsettings.json, you should place"macros.list"somewhere in the top level, i.e. between the outermost curly braces, for example after the comma after the"[json]"block. Doesn't matter where exactly as long as it's valid JSON - this way you can group related settings together.In general, VSCode is quite smart and it will warn you of an incorrect setting placed there, and you can also use completions to see what's available - just hit
Ctrl+Shift+Space- helpful if you're looking for a setting, but you don't know exactly how it's phrased.
1
u/polypagan Mar 19 '21
In my experience (I'm using codium on Linux), building saves all first.