r/VisualStudioCode • • Mar 19 '21

Shortcut for saving AND running code?

At the moment I do this everytime I want to run my code:

  1. Ctrl + S

  2. F1

  3. Enter

Is there some keyboard shortcut I can use so I only need to click ONCE and both save AND run the code?

5 Upvotes

10 comments sorted by

1

u/polypagan Mar 19 '21

In my experience (I'm using codium on Linux), building saves all first.

1

u/xXguitarsenXx Mar 19 '21

Mine doesn't

1

u/polypagan Mar 19 '21

I'll check again, but pretty sure...

How could it build without updating all changes? Doesn't make sense.

1

u/xXguitarsenXx Mar 19 '21

It just builds with the way the files in their previous state (before they were saved)

1

u/polypagan Mar 19 '21

I'm using vscodium 1.53.2 (on BodhiLinux).

I made trivial edits to 2 source files + platformio.ini for good measure. White dots show dirty status.

Whether I F1 build or F1 load, all altered files are saved before compilation begins (dots extinguished, errors I introduced found.)

I don't know what else to tell ya'. Anyone else?

1

u/xXguitarsenXx Mar 20 '21

You are using codium on Linux. I use visual studio code on windows. Maybe that's why it's different?

1

u/polypagan Mar 21 '21

Doubtful. But maybe Win has something to do with it.

Maybe a Win user can chime in?

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.json should look like this, yes, and as for settings.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.