VisualStudio Extension API: Restore "virtual" caret column RRS feed

  • Question

  • When you move the cursor up and down normally in a text window, the caret "sticks" to the end of the next line whenever the end of that line is to the left of the original caret column. Then, when you move to another line whose end is to the right of the original caret column, the caret goes back to the original column. This implies that as you move the caret up and down some "virtual" caret column is being kept in memory that is restored to the actual caret column whenever the next line you're moving to has its end to the right of this "virtual" caret column.

    I am developing a Visual Studio Extension that registers shortcuts for scrolling and moving the caret. In my VS extension, I would like to mimic this behaviour of the caret column being restored to its original value whenever the next line is long enough to accomodate it. I move the caret using the IVsTextView.SetCaretPos() method. This method requires setting both the line and the column, which probably overwrites the internal "virtual" caret column. So, even if there were a way to acquire the "virtual" caret column, it would probably be of little use, unless I store it in an instance member before setting the new caret position and restore it manually when appropriate (this could get tricky, as my local instance member would have to be re-set after certain events, like when the caret is moved left or right, or the user clicks on the text view).

    Is there a way I can move the caret up and down without overwriting the "virtual" caret column so that the caret will "hug" the ends of the lines and also restore on longer lines? Is there a way to inspect how the built-in "Edit.LineDown" command is implemented to achieve this (I don't know if that code is open source).

    Sunday, November 3, 2019 9:40 AM


  • Hi bernhardgh,

    Thank you for posting here.

    For your requirement, you could use the MoveTo(ITextViewLine, Double) method which is in Microsoft.VisualStudio.Text.Editor namespace to move the caret to specified location. It will move the caret to the best CaretPosition for the specified x-coordinate and text line.

    Hope it could help you.

    Best Regards,


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    • Marked as answer by bernhardgh Monday, November 4, 2019 7:25 PM
    Monday, November 4, 2019 6:24 AM