EditorMacro - assign a macro to a keyboard key-stroke in the Matlab Editor and Command Window

EditorMacro assigns a macro or action to requested key-binding in the Matlab Editor & Command Window
Updated 31 Jan 2011

View License

[bindingsList, actionsList] = EditorMacro(keystroke, macro, macroType)
[bindingsList, actionsList] = EditorMacro(bindingsList)

EditorMacro assigns the specified MACRO to the requested keyboard KEYSTROKE, within the context of the Matlab Editor and Command Window.

KEYSTROKE is a string representation of the keyboard combination. Special modifiers (Alt, Ctrl or Control, Shift, Meta, AltGraph) are recognized and should be separated with a space, dash (-), plus (+) or comma (,). If KEYSTROKE was already defined, then it will be updated (overridden).

MACRO should be in one of Matlab's standard callback formats: 'string', @FunctionHandle or {@FunctionHandle,arg1,...}, or any of several hundred built-in editor action-names - read MACROTYPE below for a full description. To remove a KEYSTROKE-MACRO definition, simply enter an empty MACRO ([], {} or '').

MACROTYPE is an optional input argument specifying the type of action that MACRO is expected to do:

- 'text' (=default value) indicates that if the MACRO is a:
1. 'string': this string will be inserted as-is into the current
editor caret position (or replace selected editor text).
Multi-line strings can be set using embedded \n's.
2. @FunctionHandle - the specified function will be invoked
and is expected to return a string which will then be inserted
into the editor document as expained above.
3. {@FunctionHandle,arg1,...} - like #2, but the function will
be called with the specified args.

- 'run' indicates that MACRO should be invoked as a Matlab
command, just like any regular Matlab callback. The
accepted MACRO formats and function input args are
exactly like for 'text' above, except that no output
string is expected and no text insertion/replacement
will be done, useful for non-textual actions.

In addition, this MACROTYPE accepts all available
(built-in) editor action names. Valid action names can
be listed by requesting the ACTIONSLIST output arg.

BINDINGSLIST = EditorMacro returns the list of currently-defined KEYSTROKE bindings as a 4-columned cell array:{keystroke,macro,type,class}. The class information indicates a built-in action ('editor menu action', 'editor native action', 'cmdwin native action' or 'cmdwin menu action') or a user-defined action ('text' or 'user-defined macro').

BINDINGSLIST = EditorMacro(KEYSTROKE) returns the bindings list for the specified KEYSTROKE as a 4-columned cell array: {keystroke, macro, type, class}.

BINDINGSLIST = EditorMacro(KEYSTROKE,MACRO) returns the bindings list after defining a specific KEYSTROKE-MACRO binding.

EditorMacro(BINDINGSLIST) can be used to set a bunch of key bindings using a single command. BINDINGSLIST is the cell array returned from a previous invocation of EditorMacro, or by manual construction.

[BINDINGSLIST, ACTIONSLIST] = EditorMacro(...) returns in ACTIONSLIST a 3-columned cell array of all available built-in actions and currently-associated key-biding(s): {actionName, keyBinding(s), class}.

Usage Examples:
bindingsList = EditorMacro; % get list of current key-bindings
bindingsList = EditorMacro('ctrl r'); % get list of bindings for <Ctrl>-R
[bindings,actions] = EditorMacro; % get list of available built-in action-names
EditorMacro('Ctrl Shift C', '%%% Main comment %%%\n% \n% \n% \n');
EditorMacro('Alt-x', 'try\n % Main code here\ncatch\n % Exception handling here\nend');
EditorMacro('Ctrl-Alt C', @myCallbackFunction); % myCallbackFunction returns a string to insert
EditorMacro('Alt control t', @(a,b)datestr(now), 'text'); % insert current timestamp
EditorMacro('Shift-Control d', {@computeDiameter,3.14159}, 'run');
EditorMacro('Alt L', 'to-lower-case', 'run') % Built-in action: convert text to lowercase
EditorMacro('ctrl D','open-selection','run') % Override default Command-Window action (=delete) to behave as in the Editor (=open selected file)

A few known limitations (=TODO for future versions) are listed within the file's help section.

Bugs and suggestions:
EditorMacro was tested on Matlab 6.0 (R12) through 7.7(R2008b).

Note: Unfortunately, my Matlab 6 computer crashed since the first version so I can no longer test EditorMacro on Matlab 6, so if you find problems please email me directly.

Please send bugs to Yair Altman (altmany at gmail dot com)

This code heavily relies on undocumented and unsupported
Matlab functionality. It works on Matlab 6 & 7+, but use at
your own risk!

A technical description of the implementation can be found at: http://UndocumentedMatlab.com/blog/EditorMacro/

Cite As

Yair Altman (2024). EditorMacro - assign a macro to a keyboard key-stroke in the Matlab Editor and Command Window (https://www.mathworks.com/matlabcentral/fileexchange/24615-editormacro-assign-a-macro-to-a-keyboard-key-stroke-in-the-matlab-editor-and-command-window), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R12
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes

Fixes for Matlab 6 through 7.12 (R2011a)

Many fixes; Automatically detect macro functions that do not accept the expected two input args; Support for native/menu actions (idea by Perttu Ranta-aho); Support for command-window actions; Use EDT for text replacement; Added screenshot