Ova

What is the shortcut for copy line in IntelliJ?

Published in IntelliJ Shortcuts 3 mins read

The shortcut to duplicate a line in IntelliJ, which is often what users mean by "copy line" for immediate replication, varies by operating system:

  • macOS: Press ⌘D
  • Windows/Linux: Press Ctrl+D

This powerful shortcut instantly creates an exact copy of the current line (if no text is selected) or a selected block of code, placing the duplicate directly below the original.


Mastering Line Duplication in IntelliJ IDEA

IntelliJ IDEA, a leading integrated development environment (IDE), offers a wealth of keyboard shortcuts designed to boost developer productivity. Among the most frequently used is the "Duplicate Line or Block" shortcut, which streamlines the process of replicating code.

The "Duplicate Line" Shortcut Explained

While a standard copy (⌘C/Ctrl+C) and paste (⌘V/Ctrl+V) operation can also copy a line, the "Duplicate Line" shortcut (⌘D/Ctrl+D) is specifically tailored for quickly inserting an identical line or block of code right below the current position. This eliminates the need for two separate actions, making your coding workflow significantly faster.

Shortcut Reference Table

Action macOS Shortcut Windows/Linux Shortcut Description
Duplicate Line ⌘D Ctrl+D Duplicates the current line or selected block and places it directly below.

How It Works

  • No Selection: If your cursor is on a line and no text is selected, pressing ⌘D (macOS) or Ctrl+D (Windows/Linux) will duplicate that entire line, inserting the copy directly beneath it.
  • With Selection: If you have a block of text or multiple lines selected, the shortcut will duplicate the entire selected block, placing the copy immediately after the original selection.

Practical Applications and Productivity Boosts

Utilizing the duplicate line shortcut can significantly enhance your coding efficiency in various scenarios:

  • Creating Similar Variables or Method Calls:
    String firstName = "John";
    String lastName = "Doe"; // Duplicate 'firstName' line, then edit
  • Replicating Configuration Lines: In configuration files (e.g., XML, YAML, properties files), it's common to have repetitive structures. Duplicating a line and making minor adjustments is much quicker than typing it out again.
  • Generating HTML/XML Elements: Quickly create multiple <li> items or <div> blocks.
    <div class="item">Item 1</div>
    <div class="item">Item 2</div> <!-- Duplicate and edit -->
  • Copying and Modifying Method Signatures: When overloading methods or creating similar helper functions, this shortcut is invaluable.

Beyond Duplication: Related Line Manipulation Shortcuts

While duplication is key, IntelliJ offers other powerful line-level shortcuts that complement your workflow:

  • Cut Line:
    • macOS: ⌘X
    • Windows/Linux: Ctrl+X
      (Cuts the current line or selected block to the clipboard.)
  • Delete Line:
    • macOS: ⌘⌫ (Command + Delete)
    • Windows/Linux: Ctrl+Y
      (Deletes the current line or selected block without affecting the clipboard.)
  • Move Line Up/Down:
    • macOS: ⇧⌘↑ / ⇧⌘↓ (Shift + Command + Up/Down Arrow)
    • Windows/Linux: Shift+Alt+Up / Shift+Alt+Down
      (Moves the current line or selected block up or down without cutting/pasting.)

Incorporating these shortcuts into your daily routine can dramatically reduce reliance on the mouse and improve your coding speed and focus. For a comprehensive list of shortcuts, refer to the official IntelliJ IDEA default keyboard shortcuts documentation.