Vim - count lines in selected range

Vim

Vim Problem Overview


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? Thanks in advance.

Vim Solutions


Solution 1 - Vim

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

Solution 2 - Vim

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)

Solution 3 - Vim

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionshabuncView Question on Stackoverflow
Solution 1 - VimseheView Answer on Stackoverflow
Solution 2 - VimLuc HermitteView Answer on Stackoverflow
Solution 3 - VimLaurence GonsalvesView Answer on Stackoverflow