shabunc
shabunc

Reputation: 24761

Vim - count lines in selected range

I want to count lines in 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?

Upvotes: 107

Views: 35804

Answers (3)

Luc Hermitte
Luc Hermitte

Reputation: 32966

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)

Upvotes: 30

Laurence Gonsalves
Laurence Gonsalves

Reputation: 143274

'<,'>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.

Upvotes: 14

sehe
sehe

Reputation: 393467

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: :heTabTab...)

Upvotes: 184

Related Questions