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
Some useful shortcuts:
- To add/remove a caret, press
Alt+Shiftand select cursor locations with the mouse. When you want to go back to single-cursor mode, just press
Another way it to press
⌥(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
^G(macOS) to add the next occurrence of the current word to the selection; press
⇧^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
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:
- And more.
Yet there are more exciting actions that work with multiple cursors as well:
- 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.
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.
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.
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.
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!
Your CLion Team
Subscribe to Blog updates
Thanks, we've got you!
Introducing the New CLion Conan Plugin
We are thrilled to announce the launch of the new version of the Conan CLion Plugin, now compatible with the 2.X versions of Conan.
How To Address 7 Major C++ Pain Points with CLion
The top major C++ pain points have remained unchanged in recent years. How can a modern IDE help address these pain points?
How To Debug C++ Code Efficiently: Disassemble On Demand
CLion 2023.1 brought the ability to explore assembly code even when the source code is available. A new Disassemble action has been added to improve your debugging experience, help you catch issues, and eliminate inefficiencies in the code. When debugging C or C++ code in CLion, you can explore th…