Shortcut to rerun last command

69 views (last 30 days)
Simone Gaiarin
Simone Gaiarin on 2 Sep 2015
Is it there a way to setup a shortcut to rerun the last command typed in the command window?
So a shortcut that does the equivalent of the following set of operations but without the need to focus the command window: - Type up arrow key in command window to retrieve the last command - Hit enter to run hit

Answers (4)

Cam Salzberger
Cam Salzberger on 4 Sep 2015
Edited: Cam Salzberger on 4 Sep 2015
Hello Simone,
I understand that you would like to be able to rerun the last command entered in the command window without needing to focus on the command window. Unfortunately, there don't appear to be any keyboard shortcut options that will do that, or even ones to bring the focus to the command window. You could double-click the command in the Command History (or highlight multiple commands and hit Enter), but I imagine that's not quite what you want either.
You can create a custom shortcut button which can appear in the Quick Access Bar. Follow the instructions on this page to create the button, and use this code in the shortcut:
historypath = com.mathworks.mlservices.MLCommandHistoryServices.getSessionHistory;
lastexpression = historypath(end);
Alternatively, if you would like to rerun the last batch of commands (if you enter multiple lines, but run them all at once, they're a batch), you might have to get a bit trickier. You could create a button shortcut that uses xmlread to open the history file. The history file for me is located at:
The folder should be the same as your prefdir. You could then parse out all the commands at the end that have the same batch number, and run them with eval.
I hope this helps!
Cam Salzberger
Cam Salzberger on 9 Oct 2015
I just found out that you can apparently use Ctrl+0 (zero, not "O") to focus on the command window. This will grab the focus from anywhere else in the main MATLAB GUI, or the editor, though not from an undocked figure window. But it does mean that sometimes you could do Ctrl+0 -> up arrow -> Enter to rerun the last command.

Sign in to comment.

Guillaume on 8 Sep 2015
An option is to go outside of matlab and use a macro recording program to would reproduce all the steps you've mentioned when you type a key combination of your choice.
There's probably one out there clever enough to be able to figure out the position of matlab's window wherever it is, so it is always going to click inside it.

Conglian Pan
Conglian Pan on 5 Jan 2018
on my MacBook, I simply use the up arrow key and a list of history command would appear. The bottom one is the last command, and you just hit enter to rerun it. If you want to go few commands back, it is also very simply, just keep using up arrow key to select and then hit enter key.
Alireza yarmohammadi
Alireza yarmohammadi on 28 Mar 2021
hy i think ctrl+TAB would be a good one!!!!

Sign in to comment.

Alireza yarmohammadi
Alireza yarmohammadi on 28 Mar 2021
try ctrl+tab


Find more on Desktop in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by