1

I would like to test in an operator-pending mapping whether the cursor is on the last position of the line. I currently have this, but it does not work:

onoremap <expr> i$ col('.') == col('$') ? ':…<CR>' : ':…<CR>'

How can I test whether the cursor is on the last position of the line?

Related to this:

  • Where can I find the documentation of these tests?
  • How can I quickly test such a condition from the command line?

1 Answer 1

2

Try this instead:

col('.') >= col('$') - 1

col('$') returns the number of the bytes in the cursor line plus one. Unless you have 'virtualedit' set, the cursor will never be in that position. (I'm using >= instead of == in case you do have 'virtualedit' set.)

I'm not 100% sure what you mean by these tests, but you can find the documentation for col() at :help col().

One way to test such a condition on the command line is to evaluate it as an expression and print out the result echo:

:echo col('.') >= col('$') - 1
6
  • I am actually looking for a good tutorial about conditional tests in mappings. May 6, 2020 at 9:48
  • What I do not understand is how the cursor could be beyond the end of the line. Could you explain that? May 6, 2020 at 9:50
  • 2
    @SergeStroobandt See :help 'virtualedit'
    – Rich
    May 6, 2020 at 9:50
  • 1
    @SergeStroobandt I'm not sure if any such tutorial exists. There are so many possibilities for things you could test! Having said that, they're all listed in :help functions, so having a skim through that might be a start.
    – Rich
    May 6, 2020 at 9:52
  • 2
    @SergeStroobandt Yes, :echo (see the end of my answer). In other contexts, you might want to use :let or :call from the command line.
    – Rich
    May 6, 2020 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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