Search code examples
pycharmeditorkeyboard-shortcutsmultilinecaret

PyCharm: Adding carets above or below the current caret using keyboard is not working


In PyCharm, as I have to edit multiple lines at once, I've been trying to add carets below the current caret using a keyboard. As per this page:

Press Ctrl twice, and then without releasing it, press up or down arrow keys.

However, when I do that, the text just gets scrolled. The cursor/caret is not moving at all.

The afore-mentioned page, also says:

To add carets exactly above or below the current caret position using virtual spaces, press Alt+Shift+Up / Alt+Shift+Down.

I even tried that but in this case, the cursor moves to somewhat random places in the file.

Note that I can insert carets above or below the current caret using a mouse (Alt+Shift + mouse-click + dragging the mouse). However, I want to do that using the keyboard only... just like in VSCode, it's quite straightforward:

  • to add Cursor Above: Shift + Alt + UpArrow

  • to add Cursor Below: Shift + Alt + DownArrow


Solution

  • Depending on the keymap you use, the actions can have a default keyboard shortcut configured or you may have to configure it yourself.

    You can configure these actions by going to File > Settings > Keymap > Editor Actions. In the IDE, the actions to add a caret exactly one line above or below the current caret position are called Clone Caret Above and Clone Caret Below respectively.

    In the screenshot, the default Windows keymap is used. As can be seen, the actions don't have default values and the keyboard shortcuts mentioned in the documentation are used for other actions. However, if you configure them, they work as expected.

    screenshot of settings