vim

Vim replace multiple empty lines by one empty line


I was looking for a way to replace multiple empty lines by one empty line and encountered with one solution given below:

:g/^$/,/./-j

I understand the following:

g/   replace each occurrences
^$   start to end is an empty, basically empty line
,    replace empty line by comma
.    maybe repeat last command
-j   minus is go up and j is go down

But, I do not understand how period and minus j works in the above code. Vim is a pretty powerful tool and I hope understanding its syntax help further.

Where can we find the documentation of minus j?

How does period and minus j work here?


Solution

  • g    Run the command globally, for the entire file
    /^$/ Start executing at an empty line…
    ,    …and continue executing to…
    /./  …the first non-empty line (a line that contains
         regexp '.', i.e. any character)
    -j   go up and join all selected lines
    

    That is, the command joins all empty lines from an empty line to the line before the next non-empty.