-
Notifications
You must be signed in to change notification settings - Fork 26
Code Completion
IDEA's features for autocompletion can be divided into two steps: (1) Invoking the autocompletion so that you get a list of possible items to insert and (2) actually inserting them. While the first one is obvious, it might not be clear why there should be several ways to insert a chosen completion. I'll explain this in a moment.
By invoking I mean how do you get the window that shows several possible functions, symbols or expressions that can be inserted at the current cursor position. IDEA's behavior I describe here can be adjusted so that it works differently for you. If you like to adjust how code completion works in IDEA, you can go to File -> Settings -> Editor -> General -> Code Completion. I usually use these settings:
With Autopopup code completion enabled, IDEA will automatically pop up a completion window while you are typing, suggesting functions that match what you have currently typed. You can always close this window by pressing Esc
. However, there is a lot more to it (be aware of the different shortcuts on Mac OSX):
-
Ctrl
+Space
will pop up the basic code completion window wherever you like. -
Ctrl
+Shift
+Space
will show a smart completion window which only works in specific places, like inside a function to show only options, or insideMessage[...]
to show possible messages. -
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
Ctrl
+Alt
+J
.
The completion window has basically two states: 1. No entry is focused and 2. an entry is focused. When you did not enable Insert selected variant by typing dot, ... in the settings above, IDEA will not focus any entry for the auto popup code completion window. 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
, or several other keys and the completion is inserted. If no entry is focused, then this won't work.
Most of the times you will insert a completion entry 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.
Here is an example that shows how SmartEnter works:
Here is an example that shows that 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:
And 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
(Ctrl
+j
on OS X).