Code Completion
The code completion works in two steps:
- Invoking the code completion to get a list of possible items to insert. This usually happens automatically while you are typing but, in addition, there are several shortcuts that have special meaning.
- Selecting and inserting and item from the code completion
1. Invoking Code Completion
Oftentimes, you won’t have to invoke code completion manually as JetBrains IDEs provide auto completion while you are typing if you are using the default settings. The IDE’s behavior for code completion can be adjusted if you go to File | Settings | Editor | General | Code Completion. With *Show suggestions as you type** enabled, the IDE will automatically pop up a completion window while you are typing, suggesting functions that match what you have currently typed.
However, it is always possible to manually invoke code completion to get the popup that shows possible functions, symbols or expressions which can then be inserted at the current cursor position.
Ctrl
+Space
will pop up the basic code completion window.Ctrl
+Shift
+Space
will show a smart completion window which only works in specific places, e.g inside the brackets of a built-in function, it will show the options of that function, or insideMessage[...]
it shows messages you have defined in your file.Ctrl
+Alt
+Space
will show a code completion window that contains every possible symbol you have used in your code.Ctrl
+Alt
+T
will show possible expressions that can surround the current selection of code. That is helpful if you like to surround an expression withModule
, braces,Do
loops, and other things.- The above surround completion will show a list of live templates, which are often used code constructs. This list can be accessed with .
2. Inserting a completion
The completion window has basically two states: 1. No entry is focused and 2. an entry is focused. The list will look like this
As soon as you use your arrow keys to move down the list, or you invoke the completion explicitly with one of the shortcuts above, one entry will be highlighted. Here is the same image as above after I pressed down arrow
There is not much difference between these two, except with a focused entry, you can use [
, (
, ;
, Space
, and
several other keys and the completion is inserted. If no entry is focused, insertion of the top suggestion is possible
in 3 different ways:
- pressing
Enter
simply inserts the selected entry - pressing
Tab
will insert the entry, deleting the text that is right to the cursor. This is handy if you have mistyped something. Let’s say you have typedAbsoluteFileName
but what you wanted wasAbsoluteTiming
. Just go with the cursor right after theAbsolute
and start typingT
and pressTab
. You see that the rest ofFileName
is replaced. - pressing
Ctrl
+Shift
+Enter
is called SmartEnter and it will insert the entry as well, but in addition, it will insert[]
for you if it is a built-in function. Try typingAb
and press it. The behavior if only[]
is inserted or for instance the complete call pattern can be adjusted in Settings | Languages | Mathematica.
You can always call the completion pop up with Ctrl
+Space
, even in the middle of a word. As explained above, use
Tab
to replace the rest of the word with the current selection.
Remember, as soon as you start navigating inside the completion window and you have focused an entry, you can always
look up its documentation with Ctrl
+Q
(Cmd
+J
on OS X).