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

up vote 1 down vote accepted

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.

  • so to get the column of the cursor, the commands would be col(".") and virtcol(".") – wxyz Dec 11 at 4:22
  • Additionally, the commands getpos(".") and getcurpos() return a list of position values, with the 3rd element being column – wxyz Dec 15 at 6:24
  • Would you mind modifying your answer to be col(".") and virtcol(".")? Then I will accept it. – wxyz Dec 15 at 22:47
  • Of course. Done. – Luc Hermitte Dec 15 at 22:55

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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