In vim, how can I count the number of characters on each line?
how
to
count
number
of characters?
For example, if I open a file with above text, I want to see counts like below
3
2
5
6
20
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityIn vim, how can I count the number of characters on each line?
how
to
count
number
of characters?
For example, if I open a file with above text, I want to see counts like below
3
2
5
6
20
One way to do it:
:for i in range(1, line('$')) | echo i . ': ' . strlen(getline(i)) | endfor
This version shows the line number before the character count.
Another way:
:%!awk '{print length($0)}'
UPDATE: Even shorter (every keystroke counts!):
:%!awk '{print length}'
since I just now noticed that length defaults to entire line.
:%!tee >(awk '{print length($0)}')
. This keeps the text.
– new123456
Mar 9 '12 at 1:09
:%!awk '{print length " " $0}
, then do :sort n
– Cory Klein
Jan 9 '13 at 17:51
The question was about vim, but just for comparison, here is how you can do that in Vedit:
Repeat(ALL) {
EOL
Num_Type(Cur_Col-1)
Line(1, ERRBREAK)
}
or as one-liner:
Repeat(ALL){ EOL NT(CN-1) L(1, ERRBREAK) }