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)


\ 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?


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.

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.