Using the Mathematica front-end efficiently for editing notebooks
The first step would be to not think of Mathematica's notebook editor as a full-fledged editor, but rather as an interactive interface with the kernel that has some editing capabilities, perhaps at par with notepad. If you don't, you'll always be disappointed.
The Mathematica editor:
As with all editors, you'll have to grok the editor before you can be productive in it (for various definitions of productive). Some shortcuts that will help you in writing code faster or in moving around faster are:
- Enter matching
[]
,()
,{}
with CommandOption], CommandOption), CommandOption} respectively. Use the modifications in this answer for shortcuts to [enterPart
brackets:〚〛
. With these, matching〚〛
are entered with CtrlCommand]. Using Ctrl. to select groups of expressions. Starting at any point in the expression, repeatedly pressing this will select successive groups in the expression. This might be something that you'll end up using a lot, and it also helps you visualize the precedence, etc.
As an example, try placing your cursor at
foo
in the following dummy example and repeatedly press the shortcut. You should see something like below:f[a_, b_, c_] := a ~foo~ b + c // bar
Use CtrlShift→ and CtrlShift← to extend the selection token-wise right or left.
Use CommandK to autocomplete symbols (or show a list of possible symbols) and CommandShiftK to autocomplete with placeholders for the possible arguments. For example, you'll get the following for
Plot
and you can press Tab to move through them.See this answer to change the shortcut key to something else.
Use Command/ to comment/uncomment a selection (that you selected either manually or with Ctrl.)
- Avoid using subscripts, no matter how high the temptation to make your expressions look more "mathy". Apart from slowing you down, you'll run into several issues if you're not careful, and even then, all bets are off.
- Ctrl↑ and Ctrl↓ scrolls up/down in steps of 3 lines, but the cursor remains in the same position. This is helpful if you want to quickly look at a previous cell and continue typing (thanks to Halirutan for the tip). However, these keys are by default mapped to other exposé actions on a Mac, so this won't work.
- Shift↑ and Shift↓ when inside a cell, selects the cell contents to the left or to the right respectively, till it wraps up exactly a line above or below your current line. Shift↑ and Shift↓ when outside a cell, selects the cell up or down. Further pressing up/down selects the previous/next cell and holding Shift while you do that will select multiple cells. You can also select a cell by clicking on its cell-bracket on the right.
- I don't know of a simple way to exit cell editing mode to cell selection mode (using only the keyboard) other than by repeatedly pressing Shift↑ (or down) till you reach the beginning/end of the cell, followed by up or down (this makes the cursor horizontal, which is cell selection/creation mode). The opposite is easy — you can go from a selected cell to editing it by simply pressing ←.
- Jens mentions that a few emacs shortcuts work out of the box. For example, CtrlA/E for moving to the beginning/end of line, CtrlP/N to move up/down a line (this also crosses from cell editing to cell selection mode) and CtrlD to delete forward.
- You can divide cells at the cursor with CommandShiftD and merge multiple cells with CommandShiftM
- Learn the various styling shortcuts. See what the shortcuts Command1—9 (1 through 9) do.
This list could go on, so I'll stop here unless I think of something that is very essential to add to the list.
If you also use the notebooks for taking notes, typesetting math, etc., you might also want to learn the various input aliases and other shortcuts, some of which are on this page. The simplest way to find an existing shortcut for a function is to go to the function's documentation page. You can also create your own input aliases (Esc—Esc syntax) following my answer here.
Also see this answer, especially the link to a PDF at the bottom, which shows how fast one can be if they master the formatting and typesetting shortcuts (those notes were taken in realtime).
The Workbench editor:
Compared to the Mathematica notebook, the Workbench editor is rather inert. There is no connection to a kernel and you cannot evaluate your expressions as you go. The familiar input aliases which were quite handy in your notebook interface cannot be entered here. So you will have to resort to a more verbose form of coding.
The advantage is that the Workbench is based on the Eclipse IDE, and so you can use plugins that were designed for Eclipse with the Workbench. I was recently introduced to the viPlugin for the Workbench by Rolf Mertig, and it turns the Workbench into a modal editor (I haven't used it).
See the file KeyEventTranslations.tr located at
FileNames["KeyEventTranslations.tr",
FileNameJoin[{$InstallationDirectory, "SystemFiles", "FrontEnd",
"TextResources"}], 2] // First
This file can be used to set up hotkeys as you please. There are basically not restrictions here, in that you can also set a key to cause an evaluation, using KernelEvaluate (example here, by Rolf Mertig). This file is also a great source for FrontEndTokens
that you may want to bind to (different) keys. A word of caution: it is probably wise to backup KeyEventTranslations.tr.
The FrontEndTokens
of particular interest here are the tokens under
(* Typesetting motion commands *)
Of which I especially like "MovePreviousExpression"
and "MoveNextExpression"
.
These can help you jump over matching brackets. Using them you can also jump from + to + and from comma to comma in a way that respects the structure of the expressions, which I think can be useful. It all seems quite nice, though I would have made it a little differently myself :P (i.e. I would have made it easier to jump between arguments of the same function).
For the record, here are some platform-specific suggestions for enhancing keyboard use.
If you're using Mac OS X, there are many ways you can customize your working environment, and if you spend all day at the keyboard you'll probably already be doing a lot of customizations. Although it's a good idea to master the techniques of individual applications, it's also sensible to find some system-wide tricks - you can then use these wherever you find yourself entering text.
For example, there's a strange little System Utility called KeyRemap4Macbook that lets you control the way the keyboard controls your Mac (desptie the name it works on any Mac, not just MacBooks). It also provides a Ubiquitous Vim Bindings for Normal Mode which gives you some Vim navigation shortcuts in any text field in any application, including Mathematica. It's nothing like The Real Vim, though - you can't do 'count+command' or most of the 10000 other things that those crazy Vimmers love...
Alternatively, you could look at QuickCursor on the Mac App Store. This is an inexpensive little app that lets you edit the contents of a text area in your favourite text editor, outside the current application. For example, instead of writing your text inside a text-area in a web browser, you can, after pressing a shortcut, edit the text in, say, MacVim or BBEdit. When you've finished in MacVim, Save and Close the window, and the edited text re-appears in the browser's text area.
QuickCursor works OK-ish with Mathematica, although you have to select the text you want to edit first. It would work even better if you could customize Mathematica so that Select All selected the current cell's text content, rather than every cell in the document. But that's another question for another day.
Finally, the text-expansion utilities are always useful - as you probably know. Both KeyBoard Maestro and TextExpander let you combine keyboard shortcut with scripts, so that typing specific triggers can do anything from inserting an empty Table construction to running Perl or Python scripts to insert on-the-fly calculations.