32

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.

6
  • 1
    Doesn't look like there is one. Here's the default keybidings for windows (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. Commented Aug 23, 2018 at 7:29
  • @Damien_The_Unbeliever That's unfortunate. It would have been useful. Commented Aug 23, 2018 at 7: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. Commented Aug 29, 2018 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. Commented Aug 30, 2018 at 9:32
  • 1
    @Julien See stackoverflow.com/questions/55336497/… No need for an extension. Commented Oct 13, 2019 at 17:49

6 Answers 6

22

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

Take a look:

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

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
Doesn't work if the command has an argument you need to provide.
You can press F1 then Enter
8

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"
}, 

Comments

1

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

Comments

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.

Comments

0

Click your VsCode terminal to activate it, then use the shortcut:

Ctlr + Alt + R

The most recent command is select by default, press enter.

enter image description here

Comments

-6

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.