1

I'm looking for a way to precisely set the cursor position given a virtual column.

Is there only :exe "normal! ".vcol."|"?

NB: I need virtual columns as they're the only thing that I'm able to compute correctly in a context where there are multi-byte characters that occupy only one column, and tab characters that spawns over several columns.

Regarding the context, I should be more explicit. If I'm looking for a function, it's partly out of curiosity, partly to avoid translating a tuple of coordinates (that I already have thanks to getpos(a_mark)) into something else, partly for style consistency: I'm trying to fix/enhance a plugin where I try to use functions. This implies I cannot expect something as the virtual editing mode to be in a state or in another: plugins need to work whatever the end-user choose for her/his vim options. Yet, it's true I could change it momentarily and restore it in a finally statement.

Also, I'm not in the general case where I may end up in the middle of a tabulation. Any position, I want to jump to, is accessible without 'virtualedit' being set. The (computed) position matches a placeholder in an expanded and re-indented code snippet.

8
  • I don't often use virtual columns so it might be a dumb comment but can't you do what you want with :h setpos() and use the off entry of the list taken as parameter?
    – statox
    Oct 12, 2018 at 14:21
  • According to the documentation, "off" relates to virtualedit mode, which is not my situation. Oct 12, 2018 at 14:27
  • Ok I thought virtualedit was mandatory to use virtual columns, my bad :)
    – statox
    Oct 12, 2018 at 14:30
  • 1
    No. It's unrelated. In \t/*«»*/ if () {, getcursorpos() returns [0, 2, 14, 0, 19] when the cursor is on the (. The byte index is 14, but the actual (virtual...) column is 19 -- with &ts==8 Oct 12, 2018 at 14:33
  • 1
    I should have stated my context more explicitly. I'm trying to fix a plugin that may be used by anybody with any possible setting. This means I cannot expect options to be a in certain state. The easiest solution for me would have been "tabs users cannot use my plugin"... Here however, I must admit I could set virtualedit momentarily. I've to think about that. Oct 13, 2018 at 7:58

1 Answer 1

1

Using the Tab example, let's say my line number 10 starts with a tab character (denoted by > and - for each of the three virtual columns that follow):

10: >---foo

This will place the cursor in (virtual) column 3:

set virtualedit=all
call cursor([10, 1, 2])

The list is [lnum, col, off]. lnum is line number. col is non virtual column and off is offset beyond col (this is the secret sauce to get to a virtual column).

Result:

10: >---foo
      ^
      |_____ cursor here

Note that without the virtualedit setting the cursor would be placed in column 1.


My first submission used setpos() instead until I realized that cursor() is more straightforward...

call setpos(".", [0, 10, 1, 2])

"." means "set cursor position" (as opposed to a mark). The list is [bufnum, lnum, col, off]. A bufnum value of 0 indicates current buffer. The rest is identical to the parameters for cursor().

5
  • 1
    While it's not exactly what I'm looking for, it gives me a strong hint why virtual columns are ignored by these functions when we are not in virtual editing mode. Thanks. Oct 23, 2018 at 12:41
  • I think that what @LucHermitte is trying to do is: given the input 5, place the cursor on the f He’s not trying to find a way to place the cursor on “column” 3. (That’s what he means by “The places that I want to jump to exist”)
    – Rich
    Oct 23, 2018 at 19:53
  • @Rich, it's indeed the case. B Layer wrote his (her?) answer before I provide more details on my exact use case. Oct 23, 2018 at 20:04
  • @LucHermitte Ah, I didn’t notice the times (and dates!) on things.
    – Rich
    Oct 23, 2018 at 20:17
  • Thanks @LucHermitte, I'm glad it was helpful if somewhat off the mark. I appreciate the accept/vote.
    – B Layer
    Oct 24, 2018 at 2:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.