Reputation: 4558
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
Upvotes: 236
Views: 245793
Reputation: 4100
Since Idea IntelliJ IDEA 13.1 there is a possibility to edit multiple lines.
Alt + Shift + Mouse click
Option + Shift + Mouse click
for selection. More about this new improvement in the IntelliJ blog post here. Very useful feature.
Upvotes: 341
Reputation: 693
I hold (Shift + option + command) then click where ever I want the extra cursor to go. Using Webstorm and on a Mac
Upvotes: 5
Reputation: 431
You can set multiple carets from the search and replace tool.
Live the magic
Upvotes: 2
Reputation: 5097
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.
Upvotes: 21
Reputation: 44957
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):
ALT
+ SHIFT
+ INSERT
to enter block-mode
SHIFT + UP
or SHIFT + DOWN
to select multiple lines
END
to jump to the end of each line
now type foo
, it will append it to each line:
Now deselect everything with ESCAPE
and switch back to normal selection mode with ALT + SHIFT + INSERT
.
Upvotes: 48
Reputation: 445
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
Upvotes: 1
Reputation: 604
It's Option + Shift + Click
for Macbook.
All Important Intellij Shortcuts: https://docs.google.com/document/d/1KagEr4hDmTugMJJLsYUgc122zXEnbj4A2vHoe8PtKpo/edit?usp=sharing
Upvotes: 3
Reputation: 47277
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.
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.
Try holding combinations of CTRL, SHIFT, and arrows for improved selection power.
Upvotes: 50
Reputation: 9127
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.
Upvotes: 83
Reputation: 5851
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
Upvotes: 13
Reputation: 2825
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:
(hold the second press of the modifier key, then press the arrow key)
Upvotes: 245
Reputation: 12724
You could also do a vertical code block selection by clicking mouse wheel and dragging:
Upvotes: 16
Reputation: 4960
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.
Upvotes: 8
Reputation: 2193
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"
Upvotes: 0
Reputation: 9220
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.
Upvotes: 2
Reputation: 5875
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.
Upvotes: 6