# Configurable keyboard shortcuts have arrived25

I’d like to welcome guest blogger Christina Roberts from the MATLAB Editor team. Christina was the lead developer on the configurable keyboard shortcuts feature.

R2009b has a new feature that I am particularly excited about: user-configurable keyboard shortcuts for the MATLAB desktop. This is a project that has been in the works for several years, and I am thrilled that it is now officially released.

I will be splitting up my discussion of this feature into two posts. The first will give a high-level overview of the R2009b functionality, and the second will delve into the somewhat troubled history of keyboard shortcuts in MATLAB. I hope that you will learn some valuable tips from both posts and gain a better understanding of how to leverage customizable keyboard shortcuts.

First, let me explain the layout of the keyboard shortcut preference panel. You can open it from the File Menu: File -> Preferences -> Keyboard -> Shortcuts

The Active settings dropdown list at the top of the preference panel allows you to choose between different keyboard shortcut sets—those that ship with the product, and customized sets that you may obtain from colleagues or MATLAB Central. The shortcut set selected by default matches common keyboard shortcuts on the platform on which you are running. For Windows and Macintosh, these are based on Windows and Macintosh platform standards, respectively. On UNIX, we decided to base our default keyboard shortcuts on the popular Emacs editor; however, if you are more comfortable with Windows-based keyboard shortcuts, that option is also available on UNIX.

The Windows and Emacs sets are both available on non-Macintosh platforms, but the Macintosh Default Set is only available on Macintosh due to the usage of the Command key. In addition to our new default sets, we also ship a set representing the default keyboard shortcuts in R2009a, specific to the platform on which you are running.

To import a customized keyboard shortcut set, choose Browse… from the Active settings combo box and navigate to the location of the set’s XML file. If you would like to share your own customized set, use the Save As… button to create an XML-representation of the set.

The top table of the preference panel lists all the actions that are configurable. You can search the contents of this table using the search field above it. For example, let’s look at the actions relating to pasting an item from the clipboard:

You can see that there are three paste-related actions. The generic Paste action has the keyboard shortcuts Ctrl+V and Shift+Insert. These same keyboard shortcuts are also assigned to Paste to Workspace in the Workspace Browser, as indicated by the informational icon next to the actions as well as their tooltips.

The middle table is where you can change assigned keyboard shortcuts, remove existing assignments, and add new ones. Let’s assume that you want to change Paste’s Ctrl+V shortcut to Ctrl+Shift+P. To do this, click in the table cell displaying Ctrl+V and press your new keyboard shortcut. The text representing the shortcut that you just pressed will then be displayed. Note that you can also insert multi-stroke keyboard shortcuts, popular in Emacs, by selecting Limit to 2 keystrokes from keyboard shortcut editor’s context menu.

If you press the Apply or OK buttons, Ctrl+V will be replaced by Ctrl+Shift+P for all tools which support the Paste action. To see what tools these are, click in the table cell to the right of the new assignment. It is then possible to deselect some of these tools, if you do not wish for Ctrl+Shift+P to be assigned to Paste in all desktop tools.

Now let’s also assign Ctrl+V back to Paste in the Editor only. To do this, press the button under the middle table. Then press Ctrl+V in the shortcut field, and finally deselect all the tools for that shortcut except the MATLAB Editor.

Finally, the bottom table in the panel shows keyboard shortcut conflicts with the action currently being edited. In the example above, informational icons indicate that there are keyboard shortcuts assigned to Paste which are also assigned to Paste to Workspace. However, since the Workspace Browser does not support the default Paste action, this is not an actual conflict. To create a real conflict, try replacing the Ctrl+Shift+P assignment with Ctrl+P.

When you make that change, an error icon appears, indicating that Ctrl+P is assigned to two or more actions within the same desktop tool. This means that you cannot use Ctrl+P for both Paste and Print, but you can instead choose to unassign Ctrl+P from Print by selecting the first row in the bottom table and pressing the Unassign button.

If you now press the OK or Apply button, your modifications to the Windows Default Set will be retained in subsequent sessions of MATLAB. You can go back to the original Windows Default Set by selecting Restore defaults, or you could write this file out as a custom set by selecting Save As….

That is a quick overview of how the preference panel works. In a future post, I will discuss the history of keyboard shortcuts in MATLAB and explain how we made some of our design choices.

-by Christina Roberts, The MathWorks

### Note

Aaron Batista replied on : 1 of 25

I’m delighted this exists now. Perhaps even sufficient justification to upgrade from r2009a…

What I want at the moment is to make ctrl-o insert a newline with emacs keybindings on, just like it should :)

Is there a keyboard shortcut for newline in r2009a? And, no, “return” isn’t it because I don’t want point to move. Thanks!

Christina Roberts replied on : 2 of 25

Hi Aaron,

Unfortunately there is no existing action that does the Emacs Ctrl+O behavior. The keyboard shortcut manager just allows you to change keyboard shortcuts for actions that have already been defined.

We do know that users would like to be able to define macros so that they can assign keyboard shortcuts to arbitrary actions. I will add this request to the existing records.

Thanks for the comment!

Yair Altman replied on : 3 of 25

User-defined macros can be defined for the Editor and the Command-Window using the EditorMacro utility on the File Exchange:
http://www.mathworks.com/matlabcentral/fileexchange/24615-editormacro-assign-a-macro-to-a-keyboard-key-stroke-in-the-matlab-editor-and-command-window

Technical details and examples can be found here:
http://undocumentedmatlab.com/?s=EditorMacro

Your specific request can easily be achieved using EditorMacro:

EditorMacro('ctrl-o', @(ed,evd)ed.insert(ed.getLineEndFromPos(ed.getCaretPosition),sprintf('\n')), 'run');


Warning: EditorMacro uses highly undocumented and unsupported Matlab functionality. So while it appears to work very well with Matlab releases all the way back to Matlab 6, it may indeed fail in a future release. Also, if something fails to work, you will not be able to ask MathWorks for support.

Ori replied on : 4 of 25

The current folder shortcut used to be alt-y. Now it is alt-o. However, while in the editor window, alt-o opens the Tools menu. Please correct me if I’m wrong but neither the current directory nor the Tools menu seem to have configurable keyboard shortcut. Right now I have to press ctrl-0, alt-o, “<“choose a directory”>” ctrl-shift-0 in order to go to the command window, then the current folder field and back to the editor. Any simpler way to circuvent this conflict?

Christina Roberts replied on : 5 of 25

Hi Ori,

The Alt key accelerators that you are referring to are known as mnemonics, and they actually aren’t part of the keyboard shortcut infrastructure. They are limited to being a letter that appears in the actual text of the item.

It appears that this conflict was introduced when “Current Directory” was renamed as “Current Folder”. I will file a bug report with the owner of that component and see if there is a way for us to resolve the conflict between (perhaps adding an actual keyboard shortcut to move focus into the Current Folder widget!).

Thanks for letting me know about this. If I discover a workaround in the current release, I will let you know.

-Christina

Sridhar Mahadevan replied on : 6 of 25

As a long time user of MATLAB on the Mac (and Windows), I for one am not thrilled with the changes to the keyboard shortcuts. I can’t figure out how to enable Emacs keys in the MATLAB editor (the previous versions had a nice nifty shortcut that enabled that, which has sadly been taken out of the new MATLAB 2010a version).

Also on the Mac, I don’t see all the options that you illustrate above for the Windows keyboard shortcuts. There is no Emacs shortcut selection choice on the Mac.

– Sridhar

Mike replied on : 7 of 25

@Sridhar,

You can get to the emacs set using the Active Settings menu:

Sridhar Mahadevan replied on : 8 of 25

Hi Mike,

I’m sorry but this is not helping me.

I am running MATLAB R2010a on a 64-bit Mac (several Macs, actually, including two desktops and a laptop). I don’t see four options shown above in my Keboard shortcuts. I see only two options: Macintosh Default Set and R2009a Macintosh Default Set. Did you have to load some other preferences file?

I do not see the Emacs Default Set option on any of the machines I’m using MATLAB on.

– Sridhar

Sridhar Mahadevan replied on : 10 of 25

Hi Mike,

Great, thanks for the pointer. Loading the Pre2009bUnixDefaults file does the trick! It would be nice to correct this bug in future releases, so that the Emacs key bindings are available as a selectable option. On the four machines I’ve tried it on, the Mac 2010a release does not provide an Emacs default key binding by default.

– Sridhar

Kevin replied on : 11 of 25

I am attempting to put the next tab/previous tab shortcuts back to ctrl+tab and ctrl+shift+tab, as they were in previous editions of Matlab. However, the shortcut editor does not seem to recognize the key input of ctrl+shift+tab, so I have no way to assign it to the “previous tab” action. Is there a way around this?

Mike replied on : 12 of 25

@Kevin,

There is no easy way to to do that in the current release. You could manually edit the keybindings xml file, just save a backup copy first.

Ryan replied on : 13 of 25

Hi Mike,
I’m trying to use the keyboard shortcut to switch between tabs in the editor window. I’m using a macbook keyboard and don’t recognize (and can’t find help with) the symbols displayed for this keyboard shortcut.
Navigating to File/Preferences/Keyboard/Shortcuts and then to the ‘previous tab’ or ‘next tab’ action names, one finds these symbols: The caret symbol (^) and an up-arrow that I am unfamiliar with. What keys on the macbook keyboard correspond to these symbols? Is there some resource documenting what these symbols are called?
Thanks,
Ryan

R Kan replied on : 15 of 25

Hi
when I installed Matlab 2010b, whenever I start I get the Warning:
Name is nonexistent or not a directory: C:\Program Files\MATLAB\toolbox\nnet\nnanaylze.
(few more neuralnetwork related directory in this way).
In previous versions, I did not get any problem installing.
I searched in the forums. I could not find any English forums that talked about this. Where to direct the question. Could you suggest.
thanks

Mike replied on : 16 of 25

Did you have neural network toolbox installed in a previous version of MATLAB? It might have carried over some of your preferences that look for it. Try the command “rehash toolbox” to see if that clears it up.

R Kan replied on : 17 of 25

Hi thanks.
I had to set the path with ‘default’. Things got straightened. stupid me. simple thing

Chad Gilbert replied on : 18 of 25

Hi Christina, Mike,

I sometime use MALAB on school computers, where logging off resets MATLAB’s settings. My solution is to carry around a USB stick with a setup.m script on it and run that script when I log on. Is there any code I can add to that script so taht I can assign my custom keybindings automatically?

Thanks for your great work on the MATLAB GUI.

Chad Gilbert replied on : 19 of 25

com.mathworks.services.Prefs.setStringPref(‘CurrentKeyBindingSet’, );

changes my matlab.prf file the way I want. But then I can’t convince MATLAB to acknowledge the change! Even worse, restarting MATLAB changes the matlab.prf file back to its default state.

Mike replied on : 20 of 25

Correct, that command does not change the setting, it is a side effect of changing the keybindings from the gui, rather than the cause. There is no supported way to do this from the Command line, but I will create an enhancement for the key bindings team.

Jessica replied on : 21 of 25

Mike, I’m using Matlab R2011a installed on a linux machine. I connect to the machine remotely using ssh -X on my Mac. If I could choose for Matlab to use Mac key bindings, my life would be really easy! But I only have the option for Windows and Emacs. Is there a downloadable mac set I can use on a Windows or Linux machine, when connecting remotely? I couldn’t find this on the file exchange. Thanks!

John King replied on : 23 of 25

@JohnKingV

I have read your article about customizing key-strokes to perform that actions supplied. I am working on a project to standardize the header and footer information that goes into a ‘.m’ file. I have written a function that creates a template ‘.m’ file that uses pre-formatted prompts to gather information about the function being written.

This would be a handy function to call using the (CTRL + N) functionality.

Is it possible to call a function from a keyboard shortcut?

Jens Huber replied on : 24 of 25

I know this is an old topic, but I hope I can still get an answer. I switch constantly between my desktop Mac Matlab 2011 and a Matlab 2009b on a unix cluster. However I access the cluster via an NX-client (Remote Desktop) from my Mac -> therefore I have the command key on my keyboard when using the unix version. I’m used to the Macintosh keyboard shortcuts (“Macintosh Default Set”), and want to use them on the unix version as well.

Is there any way to do this?
Where can I find the .xml file for the Macintosh Default set? Can I just select this in the unix version and this will lead to all the command+enter, command+c, etc. commands working like on the mac version?

Thanks!

Jens Huber replied on : 25 of 25

Oh I forgot Mike, the hint you gave to Jessica doesn’t help, because these are “Pre2009bMacDefaults”, which seem to be different than the current “Macintosh Default Set”. E.g. evaluate cell in the current set is cmd + Enter and in the Pre2009b set it’s ctrl + Enter.

Thanks