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
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(".")
andvirtcol(".")
– TrevorDec 11, 2018 at 4:22 -
Additionally, the commands
getpos(".")
andgetcurpos()
return a list of position values, with the 3rd element being column– TrevorDec 15, 2018 at 6:24 -
Would you mind modifying your answer to be
col(".")
andvirtcol(".")
? Then I will accept it.– TrevorDec 15, 2018 at 22:47 -
-
But how to get the column offset to the left border of the current window? Mar 26 at 7:26