Tips & Tricks

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 press Esc.
    Another way it to press Ctrl (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; press Shift+Alt+J (Lin/Win) or ⇧^G (macOS) to remove one.

First option works in the situations like in the following example:
multiple_cursor_set

And second one can be handy in the case like below:
multiple_cursor_selection

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:
multiple_cursor_templates_compl

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_setters_getters

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_constant

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:
change_signature
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

image description