# Using the Mathematica front-end efficiently for editing notebooks

## Background:

I have used Vim for 15+ years. There is a certain "one"-ness with the editor one achieves. Emacs users also experience this. Notepad users do not.

My current interaction with the Mathematica notebook is at the Notepad level. This is very frustrating because:

• the Mathematica engine is very powerful

• I'm so slow that I'm thinking faster than I can type or move, which is weird

## Goal:

I want to become much more efficient at interacting with Mathematica. Hitting a productive level in Vim took me 2 books + a month of internalizing key bindings and consciously developing habits. I am willing to invest similar efforts for Mathematica. However, I'd like to invest the effort into the right habits.

Here's a bunch of simple things I can't do (not exhaustive list)

• % in Vim to move around (), {} [] pairs
• dw to delete a word
• / to search + jump around the file
• hjkl movement keys
• and much more I'm not even aware of

## Questions:

1. How do I become more efficient at keyboard interaction with Mathematica?
2. Is using the notebook (I'm using Mathematica 8) the right interface, or should I be setting up some up some type of Mathematica-REPL + editing in Vim + sending it over to the Mathematica-REPL over tcp / unix socket? [Like SLIME for Lisp].
3. How do Mathematica wizards operate? My goal is NOT to make Mathematica feel like Vim. The goal is to learn how to master / communicate efficiently with Mathematica
4. Any other tips for becoming more efficient at interacting with Mathematica
-

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 [enter Part 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 (EscEsc 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).

-
I believe Ctrl + K and Ctrl + Shift + K should be mentioned. mathematica.stackexchange.com/questions/4574/command-completion –  Ajasja Jul 14 '12 at 14:57
@Ajasja Ah, yes! I forgot to mention them –  The Toad Jul 14 '12 at 14:58
is there a shortcut to switch from cell content editing mode to cell bracket navigation mode? i think this could be a useful addition. –  Thies Heidecke Jul 14 '12 at 16:03
@RM How do most people write large mathematica programs? Notebook? Workbench? Vi + Emacs? –  user1602 Jul 14 '12 at 19:02
(+1) Regarding emacs - note that many emacs shortcuts actually work out of the box in the notebook, e.g., the following Control key combinations: p/n (up down), d (forward-delete), a/e (beginning/end of line)... –  Jens Jul 15 '12 at 1:18

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.

-
for OS X there's also BetterTouchTool which is free and lets you define arbitrary shortcuts for keyboard, mouse and trackpad actions. eg, shift+cmd+m to switch to mathematica. –  acl Apr 18 '13 at 10:56
@acl Looks good, and free too! –  cormullion Apr 18 '13 at 10:59

It is unfortunate that so few of the front-end internals are exposed through the front-end's API. Hopefully this will change in the near future as part of the front-end's evolution into a worthy competitor to LaTeX for producing scientific documents.

Unfortunately the Wolfram team has a lot of work to do to match the quality of LaTeX's output, or even that of Word 2013 for that matter, given the progress Sargent's team over at Microsoft has made toward introducing high-quality math typesetting (that incorporates for example modern math font technology like OpenType) into the Office product line. (I wonder to what extent Wolfram could leverage the Microsoft RichEdit controls in future versions of Mma (for Windows) in order to avoid the need to re-create this technology or if their decision to switch to Qt precludes them from doing so at this point ... assuming there are not other reasons that would make it impractical for the front-end to use RichEdit controls. The RichEdit controls of course offer a rich API that would facilitate developing VIM-like navigation shortcuts.)

In any event, your question concerns ease of navigation in the current Mma front-end, an issue that has occupied my attention as well. I've found a tool called Autohotkey to be quite useful for creating a "mock version" of vim that can be useful not only for Mma but also other under-endowed editors like the Notepad that you mention.

If this type of workaround interests you, here's a script to get you started: modal VIM script in Autohotkey

-

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).

-
It is indeed useful to look closer to that file. :) You may want to attach this Q&A link to your answer. Personally, I find very useful a shortcut deleting all output cells via "DeleteGeneratedCells" token. –  Kuba Jul 19 '13 at 23:24
@Kuba I actually reinvestigated that file to see if there were any useful tokens that could make me get your bounty in the question of the syntax errors :). I have made some progress there, and maybe I should be satisfied, but I was going for full automation :). I hope I can show you soon, but I think I will go to sleep now. Goodnight :). –  Jacob Akkerboom Jul 19 '13 at 23:29
I'm looking forward to see it :) Don't miss the bounty deadline :) Goodnight. –  Kuba Jul 19 '13 at 23:31