Become multi-armed with CLion’s multiple cursors
Sometimes effectiveness is how quickly you can type. For example, having code appear simultaneously in multiple places can come in handy. In this blog post, we are going to explain how CLion can help you do this with its multiple cursors feature. We’ll also identify some scenarios when it’s better to go with refactorings or code generation instead.
Multiple cursors: Essentials
The idea is simple – you put the caret in several different places in a file and start editing all of them at once. This works for all the languages supported in CLion, including C, C++, CMake scripting, JavaScript, XML, HTML, Python, Swift and more. Sounds cool, doesn’t it?
Some useful shortcuts:
- To add/remove a caret, press
Alt+Shift
and select cursor locations with the mouse. When you want to go back to single-cursor mode, just pressEsc
.
Another way it to pressCtrl
(Lin/Win) or⌥
(macOS) twice then hold down and move up/down, that could be useful for items that are aligned. - Another way to get multiple cursors in a file is to add repeating occurrences. Press
Alt+J
(Lin/Win) or^G
(macOS) to add the next occurrence of the current word to the selection; pressShift+Alt+J
(Lin/Win) or⇧^G
(macOS) to remove one.
First option works in the situations like in the following example:
And second one can be handy in the case like below:
If you wish to select all occurrences at once, press Shift+Ctrl+Alt+J
(Lin/Win) or ^⌘G
(macOS).
Multiple cursors and smart actions
Lots of editing actions work nicely with multiple cursors:
- Move caret to line end/start:
Home / End
(Lin/Win) or⌘ -> / <-
(macOS) - Move caret to line end/start with selection:
Shift + Home / End
(Lin/Win) or⇧⌘ -> / <-
(macOS) - Move caret to next/previous word:
Ctrl + -> / <-
(Lin/Win) or⌥ -> / <-
(macOS) - Move caret to next/previous word with selection:
Ctrl + Shift+ -> / <-
(Lin/Win) or⌥⇧ -> / <-
(macOS) - Complete Statement:
Ctrl+Shift+Enter
(Lin/Win) or⇧⌘⏎
(macOS) - And more.
Yet there are more exciting actions that work with multiple cursors as well:
- Completion
- Code commenting
- Live templates
The context for the action is detected by the most recently set cursor, and the result is applied to all the cursors at once. Example:
When not to use multiple cursors
While the feature looks superhandy and cool, there are scenarios when it’s better to avoid it and use other IDE features instead.
Scenario 1:
If you have a class and would like to get getters and setters for its class members, don’t type them - generate them!
Generate menu (Alt+Insert
(Lin/Win) or ⌘N
(macOS)) is even more powerful and allows you to get constructors/destructors, equality/relational/stream output operators, implement/override functions and generate definitions.
Scenario 2:
Use Rename refactoring to change variables, parameters, functions, macros, and other names. This will help you to:
- Update all the occurrences and not miss any, since CLion will do it for you automatically.
- Not break accidentally a usage placed in another scope - CLion refactorings are context-aware and ensure all changes are safe automatically.
Scenario 3:
If you find yourself changing the same value occurrences in several places, maybe it’s worth a constant, a variable, a parameter, a typedef or a define? Use extract refactorings to update the code - CLion will check all the occurrences for you:
Extract Function is another useful refactoring, which can save you from redundant multiple changes in similar code blocks.
Scenario 4:
If you decide to add an extra parameter to a function, you can depute function call updates to CLion - it will substitute the default value for the new parameter to the calls:
Then you can manually update the places where you’d like to use non-default values.
The good thing about the refactorings in comparison with multiple cursors is that they work on the whole project and not just recent file.
Try multiple cursors
Give multiple cursors a try in CLion, get a feel for situations where they’re useful for you and when they’re not, and soon they will become a great new addition to your C and C++ development arsenal!
Cheers,
Your CLion Team
Signer says:
August 1, 2016Multiple cursors are nice, but because of https://youtrack.jetbrains.com/issue/IDEA-135620 I often have to switch into Sublime to edit some repetitive config files.
Anastasia Kazakova says:
August 1, 2016Thanks for letting us know the feedback! We’ll check that.
Knut says:
August 2, 2016It looks fairly straight forward to create a function in CLion to “Auto generate a print enum”. Can this be done via a user-defined macro?
Anastasia Kazakova says:
August 2, 2016This is possibly a good case for Code generation menu. We’ll consider it. Thanks.
Norbert says:
August 2, 2016I love the multiple cursors feature and use it on a daily basis. One thing that would still tremendously improve the feature for me would be to ‘align’ on multiple cursors.
For example, assume code similar to your example:
case WeekDay::Monday: return “Monday”;
case WeekDay::Tuesday: return “Tuesday”;
case WeekDay::Wednesday: return “Wednesday”;
I’d like to be able to set the multiple cursors at the beginning of the return statements, and then have a key-combination to ‘align’, which then aligns the text at the cursor position giving me something like:
case WeekDay::Monday: return “Monday”;
case WeekDay::Tuesday: return “Tuesday”;
case WeekDay::Wednesday: return “Wednesday”;
SublimeText supports this via Alignment plugin, and I often switch to that to perform such tasks.
Anastasia Kazakova says:
August 2, 2016Seems like WordPress dropped the alignment in the sample.
In general multiple cursors uses the formatting configured in Code Style settings.
Norbert says:
August 8, 2016Yes, looks like the formatting was dropped.
I found that the suggestion is actually excellently described already in YouTrack issue IDEA-123053.
Anastasia Kazakova says:
August 8, 2016I see, thanks. Yes, ticket contains quite clear description. Feel free to follow it for the updates.
Yuncong Zhang says:
May 24, 2017This looks great, but I found it’s not compatible with vim emulation at some case.
(Which I can accomplish by using Sublime)
case 1: Conflicts:
control + G key binding conflicts.
Arrow keys make multi cursors become one cursor.
case2: Some Advanced Usage.
After adding some cursors, I want to move all the cursors at the head of each line.
So, I can press `Esc` to enter vim normal-mode,
then press Shift+I to go to the head of the line.
But it does’t work here, the cursors become one if I press Esc.
Maybe works should be done in plugin vim emulation. -_-
Anastasia Kazakova says:
May 25, 2017There are issues with multiple carets and VIM-emulation mode: https://youtrack.jetbrains.com/issue/VIM-780. Pls, follow the update and upvote.
Serge Lussier says:
September 5, 2017I can make CLion/(java infrastructure) crashing hard using multiple-cursors. And I can say it has to do with auto-completion/cpp-assistant being rushed like hell, making CLion to auto-suicide itself…:)
Anastasia Kazakova says:
September 5, 2017Could you maybe share some logs? Which CLion version do you use, which JRE are you using – a bundled one or Oracle one?
Bob Stein says:
August 29, 2018CLion: Shift+Alt+click – for multiple cursors
PyCharm: Alt+click – for multiple cursors
Why?
Neither work in the other. Both are version 2018.2.
Anastasia Kazakova says:
August 30, 2018CLion uses the same shortcut as IntelliJ IDEA does. Not sure why it’s different in PyCharm. We’ll check with the team.
Tomas says:
September 17, 2019Is there a way to select a bunch of lines and get a caret per line?
Similar to sublime_text split into lines
Anastasia Kazakova says:
September 17, 2019While there is no such a way, you can find the ticket requesting it here: https://youtrack.jetbrains.com/issue/IDEA-122181
And a 3rd party plugin solving it: https://plugins.jetbrains.com/plugin/8213-extra-actions