110

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?

3 Answers 3

187

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

Sign up to request clarification or add additional context in comments.

5 Comments

Great! Thank you for this tip, as soon as 10 minute will pass, I'll mark this as an answer ) But it interesting, nevertheless, is there a function in vim. I'm pretty sure there should be a function.
@shabunc: what do you mean "is there a function in vim"? Do you mean something you can :call? Your question didn't mention that.
function just in sense like other built-in functions (:h functions ;)
@shabunc So, "yes", then. You didn't mention you wanted a function in your question. I don't believe a built-in function exists to count the number of lines in a range, but you could define your own pretty easily with two calls to getpos() (which can accept a mark as input and returns the position, including line number) and a bit of subtraction.
@shabunc: The closest I can find is :perl $curbuf->Count (returns the number of lines)
31

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)

1 Comment

this is the simplest solution IMHO: just add set showcmd to your .vimrc, and the number of selected lines will always be shown at the bottom.
15

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

2 Comments

me also usually yanking for that purpose. And today I suddenly realized that it is a time to lear something new )))
This answer is probably the best; on my terminal, g C-g will only display for a fraction of a second and need at least 5-6 tries to catch a glimpse of the output...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.