7

Gentleman,

Simple question about Vim!

How can I check if the cursor is at the end of a line using a function in vimrc?

[]'s

1
  • 1
    Why do you need to know? What are you trying to accomplish? Sometimes it is easier to force end of line, or use a command that doesn't care about end of line, or something else. What is the actual problem you are trying to solve, instead of your assumed solution?
    – Ben
    Commented Mar 2, 2014 at 16:44

1 Answer 1

12

You don't need a function to determine this, but you could wrap the following test in a function if you needed to. That would depend on what you are trying to do and what you need from the function.

The col() function returns the column of its argument. The last column of a row is col("$")-1 and the cursor column is col("."), so

echo col(".") == col("$")-1

will echo1 when the cursor is at the last column and 0 otherwise.

See:

:help col()
2
  • 6
    There's an edge case to consider: on an empty line, col(".") returns 1 and col("$") also returns 1. So you'd probably want to check if col(".") >= col("$") - 1.
    – dirtside
    Commented Sep 28, 2015 at 20:15
  • 1
    This also doesn't work if the character under the cursor is multi-byte, e.g. U+2124 (ℤ) is three bytes, so if it's the last character on a line, col('.') + 3 == col('$')
    – rampion
    Commented Feb 26, 2020 at 14:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .