IntelliJ IDEA way of editing multiple lines
Asked Answered
M

21

217

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

Misdemeanant answered 11/8, 2009 at 20:29 Comment(5)
This is something that I need to do so infrequently that even if there was a special mode in my editor, I'd never remember how to use it. I'd just use the clipboard if it was only for a few lines (like above). If it's for hundreds of lines, that's when I'd move over to the find/replace gadget.Jamieson
In column mode, create 5 cursors on the very left. Then use Ctrl-RightArrow to get over to the right. Multiple cursors, macros and search-replace are all awesome ways to get this done! I can't decide which one I like best.Jeopardize
Its Option + Shift + Click for Macbook. All Intellij Shortcuts: docs.google.com/document/d/…Wolver
Maybe this answer also helps: https://mcmap.net/q/128288/-how-can-i-expand-multi-line-selection-to-full-lines-in-phpstorm-intellij-ideaDibri
official docs: jetbrains.com/help/idea/multicursor.htmlBackus
A
319

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.

Ale answered 20/3, 2014 at 7:44 Comment(10)
Thanks for the link. I accidentally hit upon this feature but somehow I couldn't figure out which key combination I accidentally pressed :)Ssm
As of Feb 2015 Alt-Shift-Insert : Column Selection Mode OR simply Alt-Select. very powerful and easy to use. I'm not sure why people keep telling the OP to use "search and replace" when he's clearly asking about simultaneous line editing - the fact he asks that suggests he's smart enough to be aware of search and replace.Pd
For Mac, its Alt + Click.Curzon
And for now, just mouse middle button + drag.Christyna
Its Option + Shift + Click for Macbook. All Intellij Shortcuts: docs.google.com/document/d/…Wolver
doesnt work for, Is this still the combo on new versionsCaptious
Keyboard-only alternative: Press Ctrl twice, and then without releasing it, press the Up or Down arrow keys. jetbrains.com/webstorm/guide/tips/multi-cursor (works for me in PyCharm + Windows)Charlton
Still works as of Aug 2022!Dakota
@BernardoCosta For years I have been wondering what I am doing by mistake every now and then to activate that, and whenever I want to do it for real I don't know how to do it. Thank you. Finally I know what I'm doing and can do it when I want to.Zwiebel
official documentation: jetbrains.com/help/idea/multicursor.htmlTetrahedron
A
218

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)

Aleuromancy answered 9/1, 2015 at 14:25 Comment(10)
I had accidently pressed Cmd+Shift+8 and could not for the life of me figure out why the heck everything was "multi line" selectable. Thanks for the command shortcut!Unguiculate
Double <kbd>Ctrl</kbd> doesn't work for me on IntelliJ Ultimate 2016.1. I have to use double <kbc>Option</kbd>, hold the second press, then press the arrow keys.Vidrine
@DaveKennedy You're absolutely right, Jet Brains has changed default shortcut.Aleuromancy
double ctrl solution is best as it allows to keep my hands on the keyboardOutrelief
To change the key mapping to actually match any of your other editors that you use, go into Settings -> Keymap, and in the right search box search for "Clone Caret". Right click the action and pick "Add Keyboard Shortcut". If it saves it causes a conflict, click "OK" anyways. Then it'll add another prompt asking if you want to "Remove" the other assignment (probably what you want), or "Leave" it (intentionally triggering two things with one shortcut seems weird...) or "Cancel" (why did you click OK previously then?)Estimation
@AlexanderBogushov thanks this helped me. It might be worth mentioning, just for completeness, that you also have to turn-off Column Selection Mode, by selecting the same keys again, Cmd+Shift+8 (on Mac)Philosophize
Okey, Cmd+Shift+8 = ALT + SHIFT + INSERTGales
For years I have been wondering what I am doing by mistake every now and then to activate that, and whenever I want to do it for real I don't know how to do it. Thank you. Finally I know what I'm doing and can do it when I want to.Zwiebel
Official Documentation: jetbrains.com/help/idea/multicursor.htmlTetrahedron
The Windows version also works for LinuxCalutron
T
80

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

Tarriance answered 16/6, 2017 at 9:32 Comment(0)
L
48

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.

Libnah answered 17/9, 2017 at 17:26 Comment(5)
Thanks for the 'END' comment, great for common assignments into variables with different lengths.Weddle
This should be the answer as it's the most complete and to-the-point response. +1.Enteric
another way is to hold "alt" and keep pressing "j", it will keep adding lines to the multi-line select.Peeper
There is another tip like 'END', which is 'HOME', it puts the cursors at the beginning of the text on each line, if you press it more than once, it alternates between beginning of line and beginning of text (after leading whitespaces)Phalanger
Thank you! option + option (Hold) + ↑ / ↓ worked for me on a Mac.Shalne
P
42

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.

Profane answered 17/8, 2019 at 10:55 Comment(4)
delighted with this... thanks @AndreyTyukinFaunie
This is the way it works on Linux for me (Ubuntu 2021.04). But its sad, because it used to work with Ctrl+Ctrl before, something must have changed, Wayland may be interfering with that action.Botha
finally, the definitive answer!Brigadier
"no mouse needed" sounds like an advantage. but in fact, the alt+shift+mouse shortcut is hijacked by the UI which wants to move the window. - a very nice UI feature actually which I miss in windows, but this time it badly conflicts with intellij, like the code format hotkey too..Samovar
S
21

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

Smithsonite answered 26/8, 2020 at 16:6 Comment(2)
I've been looking for this for a long time. Thank you! Is there a hotkey to do the reverse as well?Ridotto
Works in IntelliJWellbalanced
C
13

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

enter image description here

Corinthians answered 2/9, 2017 at 20:48 Comment(0)
S
11

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

Se answered 13/8, 2018 at 11:15 Comment(1)
The point with (left) Alt + J is you have to select code first.Apomixis
L
7

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.

Lindesnes answered 11/12, 2015 at 20:51 Comment(1)
This is great but is there a way to do this and select all the way down to the end of the file? Otherwise I have to scroll through the entire thing using arrow and we have some massive files.Definiens
R
7

ALT + CTRL + SHIFT + CLICK on linux

Rhizocarpous answered 30/1, 2019 at 13:3 Comment(0)
T
6

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.

Tanah answered 11/8, 2009 at 21:40 Comment(5)
Throw away macros can be very handy for do these sorts of changes. How you descibe it is exactly what I would do if I was making these changes inside the Zeus editor.Dunleavy
If the lines are all grouped together like that, then record the macro to also move the cursor to the next line, then you just have to start running the macro on the first line and repeat however many times you want.Biophysics
How do you mean you can "repeat however many times you want"? Is there a way to automatically repeat a macro?Adolfoadolph
Edit->Macros->Playback Last Macro. I've got that mapped to F12, but I have a non-standard key binding. I do the macro once and start whacking F12 over and over.Tanah
macros is good,but if i use ideavim on intellij, the macros cannot resolve shortcut of vimCalling
B
4

Hold ALT and use the mouse for click and drag

Brunhilda answered 3/6, 2019 at 19:8 Comment(0)
C
4

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

Captious answered 16/12, 2020 at 20:49 Comment(0)
W
3

It's Option + Shift + Click for Macbook.

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

Wolver answered 4/10, 2019 at 11:30 Comment(1)
Link in answer is dead - "410 Gone".Holms
T
2

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.

Tutankhamen answered 11/9, 2009 at 15:11 Comment(0)
Q
2

For newer versions use: Alt + Shift + Insert

Quadrille answered 25/6, 2020 at 14:10 Comment(1)
another way is to hold "alt" and keep pressing "j", it will keep adding lines to the multi-line select.Peeper
L
2

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

Lorri answered 27/8, 2020 at 12:47 Comment(0)
N
1

For Mac:

Option + Shift + Click & Drag
Negrito answered 19/10, 2019 at 6:24 Comment(0)
E
1

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

Ensure answered 6/7, 2020 at 6:27 Comment(0)
E
0

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"

Emersen answered 10/6, 2013 at 10:54 Comment(2)
Ofc there are cases where you can't apply this method, when lines are totaly different or something, but I think theres not much you can do about this situations even with advanced methods.Emersen
So why -1? I answered the question he asked. -1 the question if you think its stupid, not the correct answer. Accepted answer with macro is in my opinion slower method to be honest.Emersen
P
0

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

Pythagorean answered 5/7, 2018 at 14:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.