IntelliJ IDEA Refactoring and Code Generation Shortcuts
Refactor code safely in IntelliJ IDEA with shortcuts for Rename, Extract Method, Generate Code, and Show Quick Fixes across Java, Kotlin, and TypeScript.
Detailed Explanation
IntelliJ Refactoring Shortcuts
IntelliJ IDEA is renowned for its refactoring capabilities. These shortcuts let you restructure code safely with full semantic understanding.
Essential Refactoring
- Rename (
Shift+F6) — rename a symbol everywhere it is used, including across files, in imports, and in string references. Works for variables, methods, classes, files, and packages. - Extract Variable (
⌘+⌥+V/Ctrl+Alt+V) — introduce a local variable from a selected expression - Extract Method (
⌘+⌥+M/Ctrl+Alt+M) — create a new method from a selected block of code - Extract Constant (
⌘+⌥+C/Ctrl+Alt+C) — replace a value with a named constant - Inline (
⌘+⌥+N/Ctrl+Alt+N) — inline a variable, method, or constant back into its usage sites
Code Generation
- Generate (
⌘+N/Alt+Insert) — open the Generate menu to create constructors, getters/setters, equals/hashCode, toString, and override methods - Surround With (
⌘+⌥+T/Ctrl+Alt+T) — wrap selected code in if/else, try/catch, for loop, or other constructs - Live Templates (
⌘+J/Ctrl+J) — insert code snippets likefori(for loop),sout(System.out.println),psvm(main method)
Quick Fixes and Intentions
- Show Intentions (
⌥+Enter/Alt+Enter) — the most important shortcut in IntelliJ. Shows available quick fixes, code suggestions, and automated refactorings at the cursor position. Use this for:- Adding missing imports
- Fixing compilation errors
- Converting between code patterns
- Implementing interface methods
- Optimizing code
Formatting
- Reformat Code (
⌘+⌥+L/Ctrl+Alt+L) — format the current file or selection - Optimize Imports (
⌘+⌥+O/Ctrl+Alt+O) — remove unused imports and sort them
IntelliJ tracks every refactoring operation and can undo complex multi-file changes with a single ⌘+Z / Ctrl+Z.
Use Case
Refactoring shortcuts are used daily by Java and Kotlin developers working in IntelliJ IDEA. The Extract Method shortcut alone can transform a 200-line function into clean, testable code in seconds. Teams that adopt these shortcuts consistently report fewer merge conflicts because refactorings are applied atomically rather than through manual find-and-replace.