0

I have the following command for \ that jumps to the last character unless the cursor is under the last character, in which case it jumps to the start of the line.

nnoremap <silent> <expr> \ ((col(".") >= col("$")-1) ? "^" : "$")

It works great most of the time unless the final character is a multibyte char, e.g. the 1-line file multibyte.txt (given below)

aaaaaaaaaa的的的的的的的

\ moves to the end of the line, but repeatedly pressing it keeps it there.

Is it possible to detect whether the cursor is at the last byte in a line, regardless of the number of bytes that make up the character?

Greg Nisbet
  • 1,839
  • 14
  • 27

1 Answers1

2

The function col() returns the byte offset of the cursor in the current line. The returned value is the offset of the first byte of a multi-byte character.

You could use virtcol() instead. It returns the screen column of the character.

nnoremap <silent> <expr> \ ((virtcol(".") >= virtcol("$")-1) ? "^" : "$")

In a short test it also works, when the last char is double-width.

Ralf
  • 9,197
  • 1
  • 11
  • 30