Ova

How do I select the next instance in IntelliJ?

Published in IntelliJ Shortcuts 3 mins read

To select the next instance or occurrence of a word or text range in IntelliJ IDEA, repeatedly press Alt + J (Windows/Linux) or Option + J (macOS).

Efficiently Selecting Multiple Instances

IntelliJ IDEA provides powerful features for working with multiple instances (occurrences) of text, significantly speeding up refactoring and bulk editing tasks. This capability is often referred to as "Add Selection for Next Occurrence" or "Select Multiple Occurrences."

Step-by-Step: Selecting the Next Occurrence

This method allows you to progressively add matching text occurrences to your selection, giving you fine-grained control over which instances you want to edit simultaneously.

  1. Initial Selection: First, place your cursor on the word you want to select, or highlight a specific text range.
  2. Select Next: Press Alt + J (Windows/Linux) or Option + J (macOS). IntelliJ will select the current word/range and then extend the selection to the next case-sensitively matching occurrence in your document.
  3. Continue Selecting: Keep pressing Alt + J (or Option + J) to successively select additional matching instances. Each press adds the next occurrence to your multi-selection.

Once multiple instances are selected, you can type, delete, or apply other editing actions, and these changes will be applied simultaneously to all selected occurrences.

Selecting All Occurrences at Once

If you need to select all matching instances of a word or text range in the current file, there's an even faster shortcut.

  1. Initial Selection: Place your cursor on the word or highlight the text range you wish to find.
  2. Select All: Press Ctrl + Alt + Shift + J (Windows/Linux) or Cmd + Ctrl + G (macOS). IntelliJ will immediately select all case-sensitively matching occurrences within the document.

Deselecting Instances

Sometimes you might select too many instances or need to remove a specific one from your multi-selection.

  • Deselect Last: Press Shift + Alt + J (Windows/Linux) or Shift + Option + J (macOS) to deselect the last added occurrence.
  • Deselect All: Press the Escape key to deselect all instances and return your cursor to the primary selection (or simply place your cursor somewhere else).

Key Benefits and Practical Use Cases

Utilizing multi-selection in IntelliJ IDEA greatly enhances productivity for various coding scenarios:

  • Refactoring: Quickly rename local variables, method parameters, or string literals across their scope without initiating a full refactoring dialog.
  • Bulk Editing: Change multiple similar lines, update several identical values, or modify repeated code patterns simultaneously.
  • Consistency: Ensure consistent naming conventions or code styles are applied across multiple sections of your code.
  • Efficiency: Reduce repetitive manual edits, saving significant time and reducing the chance of human error.

Shortcut Summary

Here's a quick reference for the most common multi-selection shortcuts in IntelliJ IDEA:

Action Windows/Linux Shortcut macOS Shortcut Description
Select Next Occurrence Alt + J Option + J Adds the next matching instance to selection.
Select All Occurrences Ctrl + Alt + Shift + J Cmd + Ctrl + G Selects all matching instances in the file.
Deselect Last Occurrence Shift + Alt + J Shift + Option + J Removes the last added instance from selection.
Deselect All Occurrences Escape Escape Clears all multiple selections.

Further Exploration

For more advanced find and replace operations, including regular expressions, scope limitations (e.g., only in selected text, or in a specific directory), and interactive replacement, explore IntelliJ IDEA's comprehensive "Find and Replace" dialog, accessible via Ctrl + F (Find) or Ctrl + R (Replace).

For additional details on multi-selection and other editing features, refer to the official IntelliJ IDEA documentation on Multiselection.