1

How can I get the length of the current row in vim script?

2

To get the current line you can use :h getline() giving it the parameter '.'. This will return a string, all you have to do next is to get the length of this string, to do so you have several options: :h string-functions

strlen()        length of a string in bytes
strchars()      length of a string in characters
strwidth()      size of string when displayed
strdisplaywidth()   size of string when displayed, deals with tabs

So your code could be:

let size = strwidth(getline('.'))
| improve this answer | |
  • Using col('$') is also possible... – filbranden Sep 2 '19 at 19:20
  • 1
    @filbranden If I remember correctly col('$') will report N+1 for a line containing N characters since it reports the position of the cursor when it is after the last character of the line. Depending on what you want to do it could be problematic, and as OP didn't specified what they want to do I decided not to include it in my answer. But you are right it is another possibility. – statox Sep 3 '19 at 8:57

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.