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

This entry was posted in Tips'n'Tricks and tagged , , . Bookmark the permalink.

16 Responses to Become multi-armed with CLion’s multiple cursors

  1. Signer says:

    Multiple 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.

  2. Knut says:

    It 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?

  3. Norbert says:

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

      Seems like WordPress dropped the alignment in the sample.
      In general multiple cursors uses the formatting configured in Code Style settings.

      • Norbert says:

        Yes, looks like the formatting was dropped.

        I found that the suggestion is actually excellently described already in YouTrack issue IDEA-123053.

        • Anastasia Kazakova says:

          I see, thanks. Yes, ticket contains quite clear description. Feel free to follow it for the updates.

  4. Yuncong Zhang says:

    This 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. -_-

  5. Serge Lussier says:

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

      Could you maybe share some logs? Which CLion version do you use, which JRE are you using – a bundled one or Oracle one?

  6. Bob Stein says:

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

      CLion uses the same shortcut as IntelliJ IDEA does. Not sure why it’s different in PyCharm. We’ll check with the team.

  7. Tomas says:

    Is there a way to select a bunch of lines and get a caret per line?

    Similar to sublime_text split into lines

Leave a Reply

Your email address will not be published. Required fields are marked *