I'm trying to add an indicator in my statusline for the total length of the line (not just the cursor column position, which can be shown with %c
). How do I do this?
Asked
Active
Viewed 2,633 times
7

Ben Berman
- 359
- 5
- 14
1 Answers
8
To get the contents of a line as a string, use getline(<line number>)
.
To get the contents of the current line as a string, you can use getline(".")
.
To get the length of a string, you can use strlen(<string>)
.
Putting it all together, we get strlen(getline("."))
. To add it to your statusline, simply:
statusline += "%{strwidth(getline('.'))}"
or for vim-airline (what I use)
" can be any section; this is for section z (right hand side)
let g:airline_section_z = "%{strlen(getline('.'))}"

nionios
- 75
- 6

Ben Berman
- 359
- 5
- 14
-
2Beware, strlen count bytes, not the actual number of characters. You'll to use functions like strwidth to obtain the actual length of the line. – Luc Hermitte Mar 18 '16 at 18:21
-
Just note that `strdisplaywidth` seems to be align with `%v`. – Haoshu May 30 '21 at 06:00