IntelliJ IDEA way of editing multiple lines

JavaAndroid StudioIntellij IdeaIdeText Editor

Java Problem Overview


I've seen this done in TextMate and I was wondering if there's a way to do it in IDEA.

Say I have the following code:

 leaseLabel = "Lease";
 leaseLabelPlural = "Leases";
 portfolioLabel = "Portfolio";
 portfolioLabelPlural = "Portfolios";
 buildingLabel = "Building";

What is the best way to append '+ "foo"' to every line? Column mode won't work since the lines are not correctly aligned on the right side... unless there is an easy way to right justify the text :P

Java Solutions


Solution 1 - Java

Since Idea IntelliJ IDEA 13.1 there is a possibility to edit multiple lines.

Windows

Alt + Shift + Mouse click

macOS

Option + Shift + Mouse click

for selection. More about this new improvement in the IntelliJ blog post here. Very useful feature.

Solution 2 - Java

I use Column Selection Mode (Cmd+Shift+8 on Mac) which allows to create multiple cursors via Shift+Up or Shift+Down then edit all the lines together.

Starting from IntelliJ IDEA 14 there is also Clone Caret Above / Below:

  • Windows: Ctrl, Ctrl+Up/Down
  • MacOS: Option,Option + Up/Down

(hold the second press of the modifier key, then press the arrow key)

Solution 3 - Java

Another keyboard-only approach. It's possible (since 13.1 version) to use Alt+J / Shift+Alt+J (Ctrl+G for OS X) shortcuts for creating multiple carets. Alt+J selects the next occurrence of the currently selected text and adds another caret.

  1. Select the first semicolon

enter image description here

  1. Then press Alt+J four times

enter image description here

  1. Edit what you want to

enter image description here

  1. Press Esc to return to the first line.

enter image description here

Solution 4 - Java

Place caret at end

Windows: CTRL + CTRL(Hold) + ↑ / ↓

Mac: option + option(Hold) + ↑ / ↓.

To place caret at the end of rows: move caret to top row, clone down to bottom, and click END.

Change Multi-caret Hotkey

To add a custom Keymap, CTRL+SHIFT+A, type keymap and click on the one with Settings as subtext. Search for Clone Caret Above and Clone Caret Below.

I mapped mine to ALT+SHIFT+↑ / ↓ on Windows and +↑ / ↓ on Mac.

Bonus

Try holding combinations of CTRL, SHIFT, and arrows for improved selection power.

Solution 5 - Java

Column mode works just fine: first select all the lines in column mode, then press END: each cursor will jump to the end of respective line.

On Linux (NO MOUSE NEEDED):

  1. ALT + SHIFT + INSERT to enter block-mode

    entered column edit mode

  2. SHIFT + UP or SHIFT + DOWN to select multiple lines

    enter image description here

  3. END to jump to the end of each line

    enter image description here

  4. now type foo, it will append it to each line:

    enter image description here

  5. Now deselect everything with ESCAPE and switch back to normal selection mode with ALT + SHIFT + INSERT.

Solution 6 - Java

Since WebStorm 2020.2 you can use AltShiftG to add carets at the end of each selected line.

> The new action for working with multiple carets (Alt+Shift+G) lets you quickly place carets at the end of each selected line and removes the selection as soon as all carets are added.

What’s new in WebStorm 2020.2

enter image description here

Solution 7 - Java

You could also do a vertical code block selection by clicking mouse wheel and dragging:

enter image description here

Solution 8 - Java

Select Next Occurrence:

      Alt+J on Windows, Ctrl-G on Mac OS X

Unselect Occurrence:

      Alt+Shift+J on Windows, Ctrl-Shift-G on Mac OS X

Select All Occurrences:

      Ctrl+Alt+Shift+J on Windows, Ctrl-Cmd-G on Mac OS X

for more reference: link

Solution 9 - Java

I just use the macros for this sort of thing. I start recording the macro, do it once, then play back the macro on each line I want to modify. You'd be amazed at how fancy you can get with the macro record/playback feature.

Solution 10 - Java

It took me a while to find out, but on a Mac you can double-press Option (press it once, release, press it again, keep it pressed) and use Up/Down keys to create/remove carets as you wish.

You can also hold Shift+Option and click to create/remove carets at specific points.

Solution 11 - Java

ALT + CTRL + SHIFT + CLICK on linux

Solution 12 - Java

Hold ALT and use the mouse for click and drag

Solution 13 - Java

It's Option + Shift + Click for Macbook.

All Important Intellij Shortcuts: https://docs.google.com/document/d/1KagEr4hDmTugMJJLsYUgc122zXEnbj4A2vHoe8PtKpo/edit?usp=sharing

Solution 14 - Java

I hold (Shift + option + command) then click where ever I want the extra cursor to go. Using Webstorm and on a Mac

Solution 15 - Java

In this case you can also just select the piece of code in which you want to do this and perform a replace on it. Replace:

";

with:

" + "foo";

So in case you didn't know: If you have text selected while you perform a replace (Ctrl+R or Cmd+R) it will only apply to the selected piece of text.

Solution 16 - Java

For Mac:

Option + Shift + Click & Drag

Solution 17 - Java

For newer versions use: Alt + Shift + Insert

Solution 18 - Java

You can set multiple carets from the search and replace tool.

  1. Ctrl + R to open Search & Replace tool
  2. Write the character or word identifying the beginning of each recurrence to edit
  3. Clic on 'Select all occurence' in the Search & Replace tool, IntelliJ will put a caret for each occurence

Live the magic

Solution 19 - Java

What I usually use (NetBeans, but I believe it is simple to use in any IDE) is find&replace.

You just find ;\n and replace it with + $foo;\n then you don't apply to ALL lines but you place cursor on the first line and you just hit "replace" button (depends on your IDE I suppose) 7 times to change 7 lines in no time. Easy and simple and it should be done with the most basic and the most advanced IDE you can find.

EDIT: In IntelliJ (don't know if it works in other IDEs too) you can use your regexp search&replace to selection only so you can actually use "replace all"

Solution 20 - Java

For mac users it's : ALT + SHIFT + Click

Solution 21 - Java

All the above answer are great, but if you want multiline edit at the end of lines then you have press "End" key while your multiline selection is active.

For Mac Sequence of command will be:

shift option click # select a set of lines

command right arrow # go to the end of lines

This will do multiline select at the end of the line and you can edit it as required

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhdxView Question on Stackoverflow
Solution 1 - JavashobullView Answer on Stackoverflow
Solution 2 - JavaAlexander BogushovView Answer on Stackoverflow
Solution 3 - JavaDAleView Answer on Stackoverflow
Solution 4 - JavaGiboltView Answer on Stackoverflow
Solution 5 - JavaAndrey TyukinView Answer on Stackoverflow
Solution 6 - JavaYoannView Answer on Stackoverflow
Solution 7 - JavaDimaSanView Answer on Stackoverflow
Solution 8 - JavaSnehal ParmarView Answer on Stackoverflow
Solution 9 - JavaChris KesselView Answer on Stackoverflow
Solution 10 - JavaRafael EyngView Answer on Stackoverflow
Solution 11 - JavametanerdView Answer on Stackoverflow
Solution 12 - JavaDavijrView Answer on Stackoverflow
Solution 13 - JavaSoham MehtaView Answer on Stackoverflow
Solution 14 - JavaJustin MeskanView Answer on Stackoverflow
Solution 15 - JavaTom van ZummerenView Answer on Stackoverflow
Solution 16 - JavaCode4ArtView Answer on Stackoverflow
Solution 17 - JavaMeysam SadeghiView Answer on Stackoverflow
Solution 18 - JavaDotistaView Answer on Stackoverflow
Solution 19 - JavaSrneczekView Answer on Stackoverflow
Solution 20 - JavaK-MarouaneView Answer on Stackoverflow
Solution 21 - Javasanjaykumar81View Answer on Stackoverflow