Tab Completion in the MATLAB R2010a Editor12

Posted by Michael Katz,

In continuing our coverage of the new MATLAB R2010a desktop features, this week I’m highlighting the updates to Tab Completion. For those new to this feature, anytime I refer to “Tab” I mean the TAB key on a keyboard, and not the open file tabs in the Editor. Using Tab Completion in the Command Window or Editor is easy: type the first few letters of a word and press the Tab key to bring up a list of all stuff MATLAB recognizes that starts with the typed letters. If only one match is found, the rest of the word is automatically completed, if more than 1 match is found, a window is shown to make a selection. If no matches are found, the window will display a message to that effect.

Go ahead and try it out; you may have to turn it on in the preferences: File -> Preferences -> Keyboard

Before R2010a, Tab Completion listed the same set of words in both the Editor and Command Window: functions on the path, variables in the workspace, fields/methods/properties of objects in the workspace, and inputs for a small subset of functions such as file names and figure properties. Because the Editor’s completion mechanism was based off the workspace, a common workflow was: run a file or part of a file with “eval selection”, cell mode, or using a breakpoint to put local variables into the workspace in order to get Tab completion of those variables. Another common workflow to insert variable names was to use copy & paste within the Editor, or type out the variable names, both of which can introduce bugs.

Well…. not anymore! In this release, the Editor has added smarts to recognize variable names and subfunctions within the scope of where you’re editing. The editor analyzes the syntax and structure of your file to figure out what might be a variable or subfunction and where it can be used.

There are a few limitations to this feature. Because the completion list comes from analyzing the file’s syntax, the code must by reasonably syntactically correct. It doesn’t have to be perfect, but there are forms of bad files that can’t be analyzed. Another limitation comes from MATLAB’s dynamic data-types. Because tab completion uses static analysis, it doesn’t try to guess at the data type for a variable, so things after a “.” such as properties and fields won’t be completed unless that variable is also defined in the workspace. For similar reasons, variables created through eval, assignin, load, etc. won’t be added to the list of completions.

I know longtime reader Daniel was fired up about this new feature. What do you think? Will you now be more likely to use Tab Completion in your workflow?

Sven replied on : 1 of 12

I like this addition. I was one of those that ran a quick chunk of code before I edited it, just to get the variables in the workspace.

One further feature that I would request is for the function hints to play nicely with variable tab completion.

Consider the following:
>> xblah = 5;
>> xblah2 = 34;
>> mean(

At this point, a handy little yellow help thing tells you the two syntaxes of “mean”. If you type “xblah2,”, the handy little hint highlights “dim”, showing you that you’re now at the second input variable to mean. If, however, you had a long variable name that you couldn’t remember, so you typed “xbl” + TAB, and selected “xblah2” from the list, that handy little yellow help thing has been scared away, never to reappear again (unless you delete and retype the opening parentheses to the mean function).

Anyway, that’s my little request for a future version: a slightly more robust “handy little yellow help thing” that can give function hints to you even if you don’t follow the strict order of type functionName -> type parentheses -> type variable name -> type comma -> type variable name etc. It would be great if that little guy could work simply from the contents of the current command and location of the cursor, even if you didn’t follow the above order in getting there.

Cheers,
Sven.

Mike replied on : 2 of 12

Sven,
We do have a goal of better integrating all our editing helper tools, but until that happens you can try Ctrl+F1 to bring the function hints back up after it’s been dismissed by the tab completion.

Jim replied on : 3 of 12

Rather than return an error message when a completion is not found, why not simply insert a tab?

Almost every time I get the error message, I really simply wanted a tab. I end up turning this feature off because I use tabs for code alignment or to align parts of my comments. With this turned on, I have to put a space followed by a tab to achieve this.

Mike replied on : 4 of 12

@Jim,

That’s a great question. For us it was a design trade-off. Based on our observations, when a user types a sequence of characters and then presses TAB, the overwhelming majority of the time they’re looking for completions. If no completions are found, it’s possible that there are no completions in which case if we put the tab key in, that probably wasn’t what they wanted. If the reason for no completions is because they’ve missed typed, they have to backspace the tab as well to correct the sequence. Additionally, they may think that the completion list was not searched or somehow Tab completion is no longer working.

This behavior introduced the need to insert a space to get a tab character inserted, as you have discovered. I wasn’t here when the feature was first designed, but my guess is that we chose TAB to be consistent with command-line shells, and I don’t think it’s as big of problem in the Command Window. We’re not at a point yet where you can set an arbitrary shortcut for the completion action, but that is one workaround we’re considering.

per isakson replied on : 6 of 12

I want the old fashion functionality of the tab-key back! I also want the completion functionality. As a compromise, wouldn’t it be possible to interpret a second press of the tab-key as a cry for space up to the next tab-stop?

Charlie replied on : 7 of 12

Tab completion is very helpful. I have noticed in more recent matlab versions strings with file names are able to use tab completion. This is very helpful when typing in long paths for files.

In functions that I write, tab completion doesn’t work on strings containing paths. To get around this, I generally use a command like ls, then use tab completion to get the correct file path, then change the ls command back to what I actually wanted. I can’t belive this method of mine is very efficient. Could paths be recognised to allow tab completion, or is there a way that users can set up there functions to allow tab completion of strings using tabs?

Thanks for the helpful developments thus far.

Mike replied on : 8 of 12

@Charlie,

Right now Tab Completion is not user-extendable. To work around this, I use the “!” trick. Basically I type ! + function name, so

!myfun


and then tab complete from there. Then I use “Home” to go back to the beginning of the line and delete the “!” to run the command.

It’s not very elegant, but it works.

Yair Altman replied on : 9 of 12

@Charlie – here’s an undocumented and unsupported way to do what you need. Note that being undocumented and unsupported it may possibly fail in some future Matlab release, but it still works:

Type the following in your Matlab Desktop Command Prompt:
edit(‘tc.xml’)

This is Matlab’s definition of the tab-completions which is used in both the desktop (Command Prompt) and the Editor (tc stands for “Tab Completion”).

Before you make any changes to this file, keep a backup. Now simply add your favorite function name to the bottom of this tc.xml file. The syntax is pretty self-explanatory, and there’s an extensive article about it here:
http://UndocumentedMatlab.com/blog/setting-desktop-tab-completions

For example, one of my favorite utilities on the file Exchange is OfficeDoc, which reads/writes/formats Microsoft Office documents (XLS, DOC, PPT) – http://www.mathworks.com/matlabcentral/fileexchange/15192-officedoc

So, in my tc.xml file I simply added the following:

<binding name=”officedoc”>
<arg argn=”1″ ctype=”FILE”/>
</binding>

In this example, the first input arg to OfficeDoc will tab-complete the available files, and any subsequent args will not tab-complete.

For functions like ls that only accept a single file/folder argument, the syntax is even simpler:

After editing, save tc.xml and restart Matlab. So simple!

Unfortunately, tc.xml is stored in %matlabroot%/toolbox/local/ rather than in the user’s prefdir() folder. This means that when you install a new Matlab release, any changes you’ve made are not transferred to the new installation and you need to redo them – perhaps this is an enhancement idea for the next release, Mike?

Yair Altman
http://UndocumentedMatlab.com

Yair Altman replied on : 10 of 12

For functions like ls that only accept a single file/folder argument, the syntax is even simpler:

Yair Altman replied on : 11 of 12

One more try:

For functions like ls that only accept a single file/folder argument, the syntax is even simpler:

<binding name=”ls” ctype=”FILE DIR”/>

Jed replied on : 12 of 12

Thanks Yair! Unfortunately, I don’t have write permission to that directory.

Mike, it would be great if we could get this functionality supported by Matlab. Better yet, why doesn’t Matlab just try to tab complete any string as a filename? (FreeMat does that, and I love it!)