25

Is there a command which does "repeat last command"? If not, how can I set up such a thing?

Basically what I want is to press some shortcut, and for it to repeat whatever the last command was, so I don't have to find it again in the menu or the ctrl-shift-p box.

lonix
  • 14,255
  • 23
  • 85
  • 176
  • 1
    Doesn't look like there is one. Here's the [default keybidings for windows](https://code.visualstudio.com/shortcuts/keyboard-shortcuts-windows.pdf) (other OS's are easy to find). In Visual Studio, we have Undo and Redo (Ctrl-Z and Ctrl-Y). There's something undo related for multi-cursor but no sign of Redo, which is what we'd expect it to be called for VS/VS code familiarity reasons. – Damien_The_Unbeliever Aug 23 '18 at 07:29
  • @Damien_The_Unbeliever That's unfortunate. It would have been useful. – lonix Aug 23 '18 at 07:31
  • Are you interested in a macro solution? And is your "last command" always found in the command palette - you mention finding it in the menu - do you have an example of command in a menu not in the palette? I am just asking because the macro is straightforward for the command palette commands. – Mark Aug 29 '18 at 19:26
  • @Mark a macro that can be bound to some shortcut? For something in the ctrl-p box - sounds like a good alternative. I added an issue to the repo, looks like they'll add this at some point. – lonix Aug 30 '18 at 09:32
  • Not exactly the same, but for those interested in only repeating the **last terminal command**; there is this simple [Re-run Last Command extension](https://marketplace.visualstudio.com/items?itemName=Cameron.rerun-last-command) available. Shortkey is configurable with that. – Juliën Jun 19 '19 at 11:51
  • 1
    @Julien See https://stackoverflow.com/questions/55336497/make-a-keybinding-to-run-previous-or-last-shell-commands No need for an extension. – Mark Oct 13 '19 at 17:49

5 Answers5

17

You can press Ctrl + Shift + P, then Enter it also repeat the lastest command.

Take a look:

enter image description here

Chau Giang
  • 1,414
  • 11
  • 19
  • Just a note: If you have the focus on `main.py` and you run `Run Python File in Terminal` it executes `main.py`. But there was an error in `error.py`. You go and fix it (moved focus to `error.py`). Now you want to run it again and obviously it will execute `error.py` – gelbander Nov 04 '19 at 08:23
  • 2
    Doesn't work if the command has an argument you need to provide. – Zereges Dec 13 '19 at 13:59
7

If you are specifically looking to rerun the last shell command, see Make a keybinding to run previous or last shell commands


Older answer (see above)

So this is a little funky because for the workbench.action.acceptSelectedQuickOpenItem command to work, the command palette must be open. So it will flash open briefly whenever you use the macro keybinding.

Using the macrosRe extension:

"macros": {
    
  "rerunCommand": [
    "workbench.action.showCommands",
    "workbench.action.acceptSelectedQuickOpenItem"
      
  ]
}

I assume you have "workbench.commandPalette.history": 50, set to at least one so that the most recently used command is at the top of the command palette. [I think that setting always puts the last command at the top and selects it.]

And then some keybinding:

{
    "key": "ctrl+;",
    "command": "macros.rerunCommand"
}, 
Mark
  • 143,421
  • 24
  • 428
  • 436
1

On Mac simple Ctrl-P repeats the last command on the terminal. Looks like they updated it!

1

If you are specifically looking to rerun the last shell command, see Ctrl-R, with Make a keybinding to run previous or last shell commands

Actually, with VSCode 1.70 (July 2022), Ctrl-R is no longer limited to running the last command.

See issue 154306 "Add context key for run recent command open"

The views picker (Ctrl-q) lets you hit Ctrl-q again to go down the list:

{ "key": "ctrl+q", 
   "command": >"workbench.action.quickOpenNavigateNextInViewPicker",
   "when": "inQuickOpen && inViewsPicker" 
},

This is behavior we could copy in the run recent command to make it act even more like Ctrl-R in the shell

This is implemented in PR 154552 and released in VSCode Insiders.

You now have the possibility to associate to your key shortcut a

"when": "InTerminalRunCommandPicker"

And with VSCode 1.71 (Aug. 2022):

allow recent commands to be pinned

From issue 154388: Allow pinning of commands in Run recent command quickpick

From

https://user-images.githubusercontent.com/35271042/178366598-9a6e520b-1e7c-4e74-ad7b-a08d4907493d.png

To:

https://user-images.githubusercontent.com/35271042/178366684-1f77ce44-ef8f-433d-b986-d3fe9b6065eb.png

This is released in VSCode Insiders today.


VSCode 1.75 (Jan. 2023) implements "Commonly Used" list in the first-time-opened Command Palette (issue 169091), with PR 171293

It adds the setting:

workbench.commandPalette.experimental.suggestCommands

Controls whether the command palette should have a list of commonly used commands.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
-3

You can press Shift + Alt + Down arrow key it will repeat the latest command on windows VS code.