Vim - count lines in selected range
I want to count lines i开发者_开发百科n a range, not matter what range, but let it be, say, a visual block.
What is the shortest way to do it. All that comes to my mind is something like: '<,'>s/.//n
but I don't believe it is the shortest way.
So, can somebody give me a hint?
In visual mode, press gC-g
Typical output:
Selected 7 of 22 Lines; 8 of 32 Words; 201 of 491 Chars; 201 of 497 Bytes-- VISUAL LINE --
Source: :he count-items
(discoverable as: :he
TabTab...)
Set the option showcmd
(:h 'sc'
), and you will never ever need to type anything to know how many lines are selected -- at first, as I forget that I've set this option, I didn't understand the point of your question. ^^'
Otherwise, if you want to obtain that number programmatically, it's simply:
:echo line("'>") - line("'<") + 1
From within a range-function, it can also be obtained by a:lastline-a:firstline+1
. (:h function-range-example
)
'<,'>s///n
is one character shorter. :-)
If I just want to know the number of lines in a visual selection I usually just yank it (hit y
). It'll say "5 lines yanked" or "block of 5 lines yanked" depending on the type of selection.
精彩评论