Working with Multiple Selection in PhpStorm 8 EAP

Earlier this week, we released the PhpStorm 8 EAP with a number of new features and improvements. We hear what you say on Twitter and on our issue tracker and that’s why we’re including your number one request: Multiple Selection. Let’s see how we can work with them!

Multiple Selection

With Multiple Selection, we can work with multiple cursors in one file simultaneously. Let’s start with some basics: press and hold Alt and use the mouse to select the locations where a cursor is desired.

Please note that default hotkey for multiple selection (multiple carets) add/remove caret has been changed from PhpStorm 8 EAP 138.256 build to Alt+Click (used to be Alt+Shift+Click before). It can be always changed in Settings | Keymap | Add or Remove Caret.

Cursor locations can also be added using Select Next Occurrence(Alt+J on Windows, Ctrl-G on Mac OS X): we can add the next occurrence of the current word to the selection and start working from there. Also we can Unselect Occurrence (Alt+Shift+J on Windows, Ctrl-Shift-G on Mac OS X). Select All Occurrences (Ctrl-Cmd-G on Mac OS X is already available, Windows hotkey Ctrl+Alt+Shift+J will be available from the next EAP – use double shift | Select All Occurrences action for now) will select all occurrences of current word/symbol in multiple caret mode. Hitting Escape lets us work with one cursor again.

Here’s a video of these concepts in action in an HTML file.

Multiple selection works with all languages supported by PhpStorm such as PHP, HTML, JavaScript, CSS and more. Here’s a (slightly insane) example of converting a Markdown file into PHP code using nothing but multiple selection:

Autocompletion (Ctrl+Space) and live templates are also available with multiple selection. Copy/paste also works: copying will store selected text from all cursors on the clipboard, pasting will add all clipboard text to every cursor location.

Here’s another video where we demonstrate Column Selection Mode (Alt and drag the mouse to select a region of text) as well as Emmet support inside multiple selection:

Refactoring

While we agree multiple selection is super awesome, there are situations where refactorings are more appropriate. With great power comes great responsibility so it’s worth knowing when to use that power! Consider the following piece of code:

How can we rename the $config variable to $configuration here? It’s really tempting to use multiple selection but that would break all usages of the $config variable. It’s better to use a Rename refacoring in this case (Shift+F6).

Another example would be replacing strings in one document: using Select Next Occurrence (Alt+J on Windows, Ctrl-G on Mac OS X) we can easily select these strings and update them. But having the same string multiple times in a document is an antipattern… Instead, try using the Extract Variable in such case.

The shortcut alt+shift is not well chosen as it is the default shortcut to switch between keyboards under Windows. As my default keyboard is not US, my keyboard is changed everytime I press Alt+Shift.

I can disable Windows shortcut but it is not very simple and some people may need to use the original shortcut.

Can’t you use “Alt+click” (or better “Ctrl+click”) instead of alt+shift+click to add other locations?
It would be easier to press and Ctrl+click is the shortcut used by any other software (Word, Excel, drawing software, Sublime Text etc.).
I wouldn’t mind personaly if ctrl+click was used for multiple selection instead of “Navigate>declaration”.

Andriy Bazanov

ATM shortcut is not configurable, at all (you cannot change it via Keymap). When it became possible to change — you can then assign another shortcut to whatever you want / what suits you best. But no one will change default Ctrl+Click (in Default keymap) to something else — it has been used for “Navigate | Declaration” for years (for PhpStorm it is since very first version).

Mikhail Vink

Configuration of hotkey in Settings | Keymap will be possible in the nearest future, so you’ll be able to change it to any other combination.

However, as Andriy already mentioned in the previous comment, changing default hotkey for multiple selection to Ctrl+Click is not an option in any case from JetBrains side – it stands for Navigate | Declaration and will stay so.

Fabio

Thanks. As long as it is possible to change it for “Alt+Click” or any other simpler combination, I think everyone will be pleased.

Kiean

Are there plans to put this into IntelliJ?

Maarten Balliauw

I would expect it to show up in IntelliJ IDEA in one of the future releases.

JP Veilleux

Isn’t supposed to be the other way around with IntelliJ IDEA based sub-products?

I’m sure I’m missing something here but I thought that features trickled down from IntelliJ to the more focused editions ie: WebStorm, RubyMine, PHPStorm etc.

Mikhail Vink

That really depends on release cycles and individual priorities of products, so current case of having multiple selection in WebStorm/PhpStorm IDEs first is absolutely valid case.

http://xatzis.com George Hadjivarnava

Alt +drag to select a rectangle of text is the best thing. Very easy to replace text in lists and move things around!

aomm

what’s the name of this plugin on the right of the IDE on video?
like sublime’s super preview。
how to enable it?

Do you intend to implement the ability to insert sequences like the text-pastry plugin for sublime text?
It is a very useful complement to multicursors. For the moment, I’m using Emmet to emulate this behavior.

When escaping multiple carets, it would be nice to be able to return to the first instance, not the last selected. Could this be an option?

Mikhail Vink

Now it should work in this way – please try latest EAP.

http://flowcom.se Tim Samuelsson

This was the only thing i missed from sublime text. I love using an IDE!!

http://supremetechs.com Randy

When I’m using Sublime I frequently add multiple selections with ctrl + shift + up/down arrow. Is this possible with PHPStorm? I don’t like the Alt + click to set the cursors for the multiple selection. My clicking is sometimes inaccurate by a character and messes up my entire group of selections. I would much rather have this available as a shortcut.

Liubov Melnikova

As it stated in the article:

Cursor locations can also be added using Select Next Occurrence (Alt+J on Windows, Ctrl-G on Mac OS X): we can add the next occurrence of the current word to the selection and start working from there. Also we can Unselect Occurrence (Alt+Shift+J on Windows, Ctrl-Shift-G on Mac OS X). Select All Occurrences (Ctrl-Cmd-G on Mac OS X is already available, Windows hotkey Ctrl+Alt+Shift+J ) will select all occurrences of current word/symbol in multiple caret mode. Hitting Escape lets us work with one cursor again.

James Osterhout

I second this – using the keyboard to control multiple cursors would be idea.

James Osterhout

This is exactly what I was looking for: Preferences > Keymap then add keyboard shortcuts for Clone Caret Above and Clone Caret Below.

Will it be possible to have an option to allow multiple selections using dragging?

In other words, click and drag mouse to select something, then hold alt and click and drag something else, etc. Instead of just clicking, I’d like to highlight multiple items. Currently it just deselects the previous highlight.

In Sublime, I very often used the keyboard to mark columns of subsequent lines – not based on any word occurrences. Is this possible without using the mouse?

Liubov Melnikova

I am sorry, what do you mean by that? Screenshot can be helpful.

http://osedea.com Angus

This doesn’t seem to work with IdeaVim. For example, I’d like to select multiple occurrences of a word while in command mode, and then use Vim keybindings to edit the text at each cursor. Currently, it will only edit the text at the last cursor. Any fixes for this? This works very well in Sublime and is a feature that I use very often!

Call me an old classic, but I don’t think I want to use this feature. Is there a way to disable it? In working with PHPStorm 8 I keep getting multiple cursors and selections popping up out of nowhere during my usual keystroke patterns and I have to try to get rid of it in a flurry of annoyed ESC’ing and clicking.