MATLAB Keyboard Shortcuts
MATLAB's shortcut set is shaped by its dual nature as both a scripted programming environment and an interactive command-line-style computing tool — the Command Window supports its own history-recall and execution shortcuts distinct from the Editor's more traditional code-editing bindings, and moving fluidly between writing a script in the Editor and testing pieces of it interactively in the Command Window is a core part of typical MATLAB workflow. Because iterative development in MATLAB means running a script or a highlighted chunk of code over and over, that one run-selection binding ends up doing more work per session than almost anything else in the shortcut table. MATLAB's own Preferences > Keyboard panel lets a user inspect or reassign the full active keymap, which is worth checking directly if a binding listed here doesn't fire, since some institutional or lab-managed installations ship with a customized default keymap rather than MathWorks' stock one.
Code Execution
| Action | Windows | Mac | Description |
|---|---|---|---|
| Run entire script | F5 | F5 | Runs the entire currently open script file from top to bottom, the standard way to execute a complete MATLAB program or function file. |
| Run current Section | Ctrl+Enter | Cmd+Return | Runs just the current code Section (a block delimited by %% comment markers) rather than the entire file, letting you iterate on one specific part of a longer script without re-running everything before it. |
| Run selected code (Evaluate Selection) | F9 | F9 | Executes just the currently selected lines of code in the Command Window's workspace context, useful for testing a specific expression or line without running the surrounding script. |
| Stop running script | Ctrl+C (Command Window focused) | Cmd+C or Ctrl+C | Cuts off whatever script or loop is currently running in MATLAB's console, the move to reach for when code has clearly run past a reasonable time or is stuck spinning indefinitely. |
| Step into function call while debugging | F11 | F11 | With execution paused at a breakpoint, this descends into the body of whatever function the current line calls instead of skipping past it, letting you watch that function's internal logic play out rather than only seeing its inputs and outputs from the outside. |
Editor Navigation
| Action | Windows | Mac | Description |
|---|---|---|---|
| Comment/uncomment selected lines | Ctrl+R (comment), Ctrl+T (uncomment) | Cmd+R, Cmd+T | Adds or removes a leading % comment character on selected lines, using separate distinct keys for commenting versus uncommenting rather than a single toggle key. |
| Go to specific line number | Ctrl+G | Cmd+G | Opens a dialog for jumping directly to a specific line number within the current file, useful for navigating to an error location reported in the Command Window's error output. |
| Find and Replace | Ctrl+F | Cmd+F | Opens Find and Replace within the current Editor file, standard text search functionality. |
| Smart Indent selected code | Ctrl+I | Cmd+I | Automatically re-indents the selected code according to MATLAB's code style conventions, useful for cleaning up inconsistently formatted or pasted-in code. |
| Toggle breakpoint on current line | F12 (Editor) | F12 | Sets or clears a debugging breakpoint on the line the cursor currently sits on, pausing script execution there the next time that line runs so you can inspect variable values in the workspace at that exact point. |
Command Window
| Action | Windows | Mac | Description |
|---|---|---|---|
| Recall previous Command Window entry | Up Arrow (Command Window focused) | Up Arrow | Cycles backward through previously executed commands in the Command Window, letting you re-run or edit a recent command without retyping it entirely from scratch. |
| Clear Command Window | Type clc and Enter (no default key binding) | Type clc and Enter | Clears all visible text from the Command Window for a cleaner view, executed by typing the clc command rather than a keyboard shortcut, since MATLAB treats it as a regular function call like any other command. |
| Clear all workspace variables | Type clear and Enter (no default key binding) | Type clear and Enter | Removes all variables currently stored in the workspace, typed as the clear command rather than bound to a keyboard shortcut, commonly run at the start of a script to ensure a clean state unaffected by leftover variables from previous runs. |
Frequently Asked Questions
What's the difference between running a whole script (F5) and running just a Section (Ctrl+Enter)?
F5 runs the entire file from start to finish every time, which can be slow or wasteful if you're only iterating on one small part deep within a long script. Sections, delimited by %% comment markers in the code, let you break a script into logical chunks and run just the section your cursor is currently in via Ctrl+Enter (Cmd+Return), a considerably faster iteration loop when developing or debugging one specific part of a larger analysis without needing to re-run everything before it each time.
Why are Clear Command Window and Clear Workspace typed commands instead of keyboard shortcuts?
MATLAB treats clc (clear command window) and clear (clear workspace variables) as regular built-in functions rather than reserved editor-level keyboard shortcuts, consistent with MATLAB's broader design as fundamentally a command-driven interactive environment where typing a function name and pressing Enter is the native way most actions happen, rather than an editor-first application where keyboard shortcuts are the primary interaction model.
Does Stop Execution (Ctrl+C) always work immediately?
Generally yes for typical MATLAB code including most loops and standard function calls, but it can be delayed or fail to interrupt certain long-running compiled MEX functions, some deeply nested calls into external libraries, or code executing on the GPU, since the interrupt signal needs an opportunity to be checked by the running code, which doesn't happen instantaneously inside every possible operation MATLAB can execute.
Is there a shortcut to quickly comment or uncomment a block of selected code?
Ctrl+R comments out the currently selected lines by prefixing each with %, and Ctrl+T uncomments them by stripping that prefix back off, which is faster than manually typing % at the start of every line when temporarily disabling a chunk of a script during debugging or experimentation.
How do I run only a specific section of a script rather than the whole file?
MATLAB's Live Editor and Editor both support code sections, delimited by a double-percent comment (%%), and placing the cursor inside one and pressing Ctrl+Enter runs just that section rather than the entire script from the top — genuinely useful when iterating on one part of a longer analysis without re-running expensive earlier steps like loading a large dataset every single time. Section boundaries also show as subtle horizontal divider lines in the editor, making it easy to see at a glance where one logical block ends and the next begins, which matters since a section relying on a variable defined in an earlier section you skipped will simply error out rather than warn you about the missing dependency beforehand.
How does debugging in MATLAB actually work once a breakpoint is hit?
Execution pauses at the breakpointed line and the Command Window switches into a debug prompt (shown with a K>> prefix) where you can inspect or even modify workspace variable values interactively before continuing, stepping over the next line, or stepping into a called function to trace deeper — a genuinely interactive debugging experience rather than just a static call-stack viewer.