[Command] How to implement "Add Cursor Above/Below"?

Hi everyone,

First of all, thank you for the amazing library that is CodeMirror 6!

I’m trying to replicate an editing command that is standard in editors like VSCode: the ability to add a new cursor on the line directly above or below the current one, while preserving the column position.

I came across a 2020 issue about this (Implement more commands · Issue #192 · codemirror/dev · GitHub), where Marijn acknowledged the feature’s usefulness.

Following that, I tried to implement this behavior myself. While a basic version is achievable, I quickly ran into numerous edge cases—for instance, how to correctly place the cursor on lines shorter than the current column, or how to behave near folded ranges and at document boundaries. It became clear that a truly robust implementation is not trivial.

This experience is why I believe a native solution would be ideal and leads me to my questions:

  1. Has there been any progress on a native implementation of this command? Is it on the library’s roadmap?
  2. If not, has anyone in the community found an elegant and robust way to implement this, perhaps through a custom extension that handles these subtleties?

I would be very grateful for any insight.

Thanks for your time and consideration.

Does this patch look like it implements the commands as you’d want them to work?

1 Like

Hi Marijn,

Wow, this is fantastic! Thank you so much for implementing this so quickly. I know a feature like this can be quite tricky with all the potential edge cases, so I really appreciate you tackling it. It works perfectly for the most common use cases.

While testing, I came across some edge cases. One of them appears when moving the selection up repeatedly across lines of different lengths.

To reproduce:

  1. Start with text like this:
short line
another short line
short line
a much, much longer line here
another short line
  1. Place the cursor at the middle of longer word.
  2. Press the “Add Cursor Above” keybinding three times.

Actual Behavior: It adds an second cursor on the second line.

Hope this detailed feedback is helpful for refining the patch!

I’d be happy to help out further, whether that’s by testing more edge cases or even trying to contribute a PR for this fix if you can point me in the right direction. Just let me know.

On that note, and purely out of excitement to start using it, do you have a rough idea of when this might land in an official release? No rush, of course.

Thanks again for the incredible support.

I’m not seeing this happen. Which browser are you testing with?