Coding⏱️ 2 min read📅 2026-06-03

How to Fix: Intellij IDEA generate for-each/for keyboard shortcut

IntelliJ IDEA provides a keyboard shortcut for generating foreach and also for loop, which is Ctrl + Alt + L

Quick Answer: Use the 'Code' > 'Refactor' > 'Extract Variable' option or press Ctrl + Alt + L to generate both foreach and for loops.

IntelliJ IDEA provides a keyboard shortcut for generating a foreach loop and also a for loop. To access these shortcuts, open the Settings dialog (Ctrl + Shift + Alt + S on Windows/Linux or Cmd + , on Mac) and navigate to Editor > General > Keyboard. In the Keyboard section, search for 'foreach' and 'for' in the shortcuts list. You can then assign a custom shortcut to either of these actions by clicking the '+' button next to each item.

💡 Customizing Your Shortcut

  • For a foreach loop, you can use the shortcut 'Ctrl + Shift + F' (Windows/Linux) or 'Cmd + Shift + F' (Mac).

🔧 Assigning a Custom Shortcut

Method 1: Using the 'Generate' Action

  1. Step 1: Place your cursor in the foreach loop you want to generate.

Method 2: Using a Custom Shortcut

  1. Step 1: Open the Settings dialog and navigate to Editor > General > Keyboard.

✨ Wrapping Up

By following these steps, you should now be able to generate a foreach loop and also a for loop using the keyboard shortcut in IntelliJ IDEA.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions