DEV Community 👩‍💻👨‍💻

Sérgio Araújo
Sérgio Araújo

Posted on • Updated on

Count how many paragraphs in vim

The best way to count paragraphs in my opinion is counting all blank lines that appears after each paragraph. so, let's remove consecuteve blank lines

:g/^\_$\n\_^$/d
Enter fullscreen mode Exit fullscreen mode

The above code finds an empty line start followed by another empty line start and deletes it.

If the last line is not empty let's add a blank line after the last paragraph - (using the put command, adding a black hole register)

:$put_
Enter fullscreen mode Exit fullscreen mode

We can test if the current line is empty

if getline('.') == ''
    echo "empty line"
endif
Enter fullscreen mode Exit fullscreen mode

Hence we can automate the adding of a blank line at the last line of the file based on that condition:

" if the last line '$' is not '!=' empty ''
if getline('$') != ''
    :$put _
endif
Enter fullscreen mode Exit fullscreen mode

Now lets count how many blank lines.

:%s,^$,,gn
Enter fullscreen mode Exit fullscreen mode

The secret to the above command is that it does not do anything, because of the flag "n", it just shows you how many substitutions it would had done if the "n" flag were not be used.

The regular expression...

^$
Enter fullscreen mode Exit fullscreen mode

...corresponds to empty lines, therefore we are actually counting how many blank lines we have "after each paragraph".

Of course, if you want you can define a function with all of this

function! CountParagraphs()
    echom "Number of paragraphs: "
    silent! g/^\_$\n\_^$/d
    if getline('$') != ''
        $put _
    endif
    %s,^$,,gn
endfun
Enter fullscreen mode Exit fullscreen mode

We have used "silent!" on the global command so that you can see just the paragraphs count

In order to test this function beforeg deciding to put it on your vimrc, or just savin the code in another place you can just run:

:@+
Enter fullscreen mode Exit fullscreen mode

Then you can call the function by its name:

:call CountParagraphs()
Enter fullscreen mode Exit fullscreen mode

When I was writing this post I made some typos and I have corrected what I saw was wrong, but my English is far from perfect, so, feel free to comment and suggest a better solution in vim or an alternative solution using other tools.

Top comments (4)

Collapse
coreyja profile image
Corey Alexander

This is fun! I love finding new fun things to do in VIM! Thanks for the post!

Think you could help break down that first global command regular expression for me? I'm not the best at regular expressions yet and would love to understand how that first one works a bit better!

Collapse
Sloan, the sloth mascot
Comment deleted
Collapse
coreyja profile image
Corey Alexander

Ohh Awesome! That makes sense! The \_$ and \_^ are new to me, that's really cool!

I'll definitely remember those! Thanks again man!

Collapse
voyeg3r profile image
Sérgio Araújo Author

The full explanation from stackoverflow:
_$ means the end-of-line that can be used inside a pattern ($ can only be used at the end of the pattern.) Same is with _^ that means the start of a line and can be used anywhere inside the pattern. So, the pattern matches an empty line, a newline, and again an empty line. See: vimdoc.sourceforge.net/htmldoc/pat...

Find what you were looking for? Sign up so you can:

 
🌚 Enable dark mode
🔠 Change your default font
📚 Adjust your experience level to see more relevant content