What is the IntelliJ shortcut to create a local variable?

Keyboard ShortcutsIntellij Idea

Keyboard Shortcuts Problem Overview


In Eclipse if you have a method:

String MyObject.getValue();

When using this you can go:

MyObject.getValue(); 

If you cursor is on the line and you hit CTRL + 1 you get a context menu to 'assign a new local variable', resulting in the following:

String value = MyObject.getValue(); 

Can you do this as easily in IntelliJ? I've search the net but without success.

Keyboard Shortcuts Solutions


Solution 1 - Keyboard Shortcuts

Yep! This is the Introduce Variable refactoring. By default, select some text, and then hit Ctrl + Alt + V (for Mac: ++V). If the expression is incomplete or invalid, IntelliJ will still make a good guess about what you meant and try to fix it for you.

Solution 2 - Keyboard Shortcuts

IntelliJ 13.1 introduced Postfix completion.

With Postfix Completion you can introduce a local variable by typing:

MyObject.getValue().var

and pressing ctrl + space or enter.

It even works inside other statements. For example:

foo.someMethod(myObject.getValue().var);

Solution 3 - Keyboard Shortcuts

for mac users: alt + enter at the position where you want to generate your variable

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJARCView Question on Stackoverflow
Solution 1 - Keyboard ShortcutsJohn FeminellaView Answer on Stackoverflow
Solution 2 - Keyboard ShortcutsmichaView Answer on Stackoverflow
Solution 3 - Keyboard ShortcutsOlivier RoyoView Answer on Stackoverflow