5

In vimscript, how can I get the column that the cursor is currently on? I would like to save this value in a variable.

1 Answer 1

8

You've got col('.') and virtcol('.').

  • The first one is to be used most of the time. It doesn't return the actual column number, but the byte index the cursor is on in the current line.
  • The second one returns the actual column number

The tricky part is that sometimes a tab (1 byte) expand up to &ts columns, and a multibyte character have more than 1 byte that expand over only one column.

You'll need to read their documentation to know what you exactly need depending on what you do with that number after.

6
  • so to get the column of the cursor, the commands would be col(".") and virtcol(".")
    – Trevor
    Dec 11, 2018 at 4:22
  • Additionally, the commands getpos(".") and getcurpos() return a list of position values, with the 3rd element being column
    – Trevor
    Dec 15, 2018 at 6:24
  • Would you mind modifying your answer to be col(".") and virtcol(".")? Then I will accept it.
    – Trevor
    Dec 15, 2018 at 22:47
  • Of course. Done. Dec 15, 2018 at 22:55
  • But how to get the column offset to the left border of the current window? Mar 26 at 7:26

Your Answer

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

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