Using Smart Line Joins

Posted on by Egor Malyshev

Here’s a handy feature for those times when you want to clean up your code a little. When you want to condense two or more lines into one, you would normally use Home or End and then Delete or Backspace. But not all Java statements can be so easily combined, and joining lines manually can become a tedious time-waster. Thankfully, IntelliJ IDEA adds a little extra intelligence by providing the Smart Line Join editor command.
For example, when a String concatenation expression spans multiple lines, it can be a pain to delete the quotes and plus signs, but Smart Line Join will handle this with just one shortcut.

Joining string literals
With the cursor on the first line, press Ctrl-Shift-J and the String literals on both lines are joined together, removing the redundant concatenation:

String literal after joining
Likewise, when a declaration and initialization of the same variable are split onto two lines, they are quickly joined into one line.

Joining declaration and initialization
Press Ctrl-Shift-J and the initialization is embedded into the declaration:

Initializtion and declaration joined
The Smart Line Join feature is also able to handle other not-so-simple joins, like joining several line-comments (//) joined into a single line-comment, etc.

This article was originally posted in JavaLobby Tips by Rob Harwood
Technorati tags: , , ,
Comments below can no longer be edited.

4 Responses to Using Smart Line Joins

  1. Piotr says:

    April 1, 2020

    Is there any way to have operator at the beginning of the line?

    “`
    String up = “up”
    + ” and down”
    + ” and down”
    + ” and down”
    + ” let the bodies hit the flooor!”
    “`

    • Andrew F says:

      April 23, 2020

      Piotr, go to *Code Style* -> *Java*. Look for “Binary expressions” and check “Operation sign on next line”.

  2. Greg Solberg says:

    April 24, 2020

    It would be nice if the Smart Line Join feature + Rust plugin also handled rust’s inner line comment //! and outer line comments ///.

  3. Basil Bourque says:

    May 31, 2020

    FYI, besides the keyboard shortcut, there is a menu item for this feature:

    `Edit` > `Join Lines`

Subscribe

Subscribe for updates