1

How to count lines in current paragraph?

echo line("'}") - line("'{")

is near, but not correct, and not considering corner cases.

How to fix? or use another approach?

== Edit ==

I wanna count inner lines of current paragraph using vimscript, i.e., count given by yip.

since both marks '{ and '} both point to empty line or line that only containing blank (space+Tab) if posssible.

The corner case is thus when above condition not match, that is, '{ is first line and/or '} is last line of the graph.

== Solution ==

see my answer below, -- a vimscript function that could be embeded inside of statusline.

  • 2
    What are some corner cases that you're facing? Like if the paragraph is a single line? (Then a minimum of 1 will fix it) Do you need to count wrapped lines? Or, if not used in a Script, how about set relativenumber? – Sunny Pun Nov 21 '17 at 15:19
4

You can vip to select the current paragraph.

If you press gCTRL + G you'll get

Selected 3 of 5 Lines; 8 of 12 Words; 101 of 291 Chars; 101 of 297 Bytes
0

Sorry for not emphasize, as the tag indicates, I want a vimscript solution.

Never mind, I found a solution, and wrap it in a function.

func! LineCountCurrentParagraph()
    return line("'}") - line("'{") + ((match(getline("'{"),'^\s*$') == -1) + (match(getline("'}"),'^\s*$') == -1) -1)
endfunc

which is then used inside of my statusline settings.

set statusline+=%{LineCountCurrentParagraph()}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.