5

Is there a Vim regex to delete all leading blank lines in the file?


text
more text

some more text

Should become:

text
more text

some more text

4 Answers 4

7

The following should do the job:

:%s/\%^\_\s*\n//

Here is an explanation:

  • \%^ match the beginning of the file
  • \_\s match any space character including new line
  • * repeat the match
  • \n match the last new line

I include the \n to not match the leading space on the first non white-space line (line that that is only made of white-spaces)

Remark: If by blank line you mean empty line the regex is more simple:

:%s/\%^\n*//
4
  • This only removes a blank first line not subsequent blank lines. @mas answer works if there are no spaces in the line, not possible to tell from the question. If there are then :g/^\s\+/d will work on every line
    – Steve
    Commented Sep 30, 2022 at 1:16
  • Not in the test I made. The \_\s item match new line and the * make it work for multiple lines Commented Sep 30, 2022 at 1:44
  • 1
    I tested it and it works. It does remove subsequent blank lines.
    – Amarakon
    Commented Sep 30, 2022 at 2:34
  • Thanks for the feedback :-) Commented Sep 30, 2022 at 2:37
5

Probably the most concise way to do this is with a range and the :delete command:

:1,/\S/-1d
             # Explanation
             # -----------
         d   # Delete all the lines
:1,          # in the range that starts at the beginning of the file
       -1    # and ends one line before
   /\S/      # the first line containing a non-whitespace character

If you don't want to delete whitespace-only lines it's even shorter:

:1,/./-1d

        d   # Delete all the lines
:1,         # in the range that starts at the beginning of the file
      -1    # and ends one line before
   /./      # the first line containing anything
2

This is the method I use:

:g/^$/d
1
  • 4
    That will remove all blank lines though, not just the ones at the start of the file. The way I read the question, it should only remove blank lines at the start of the file. Commented Sep 30, 2022 at 1:27
1

On the off chance that one or more of the lines isn't exactly blank, but contains whitespace, this variant on @mas answer works:

:g/^\s*$/d

You asked about leading blank lines. That implies to me that you don't want to delete any lines that come either between text-containing lines or after them. If that's the case, you want to put a range between the : and the g. In your sample's case, where it appears to me there are two blank lines, it'd look like:

:1,2g/^\s*$/d
4
  • If you already know how many blank lines there are, there's no need to use :g at all: :1,2d. Although note that in the sample, there's actually only one blank line, so :1d.
    – Rich
    Commented Sep 30, 2022 at 10:31
  • @Rich I agree. Deleting blank lines need be no more complicated than deleting non-blank lines.
    – MDeBusk
    Commented Sep 30, 2022 at 13:18
  • I'm not sure if I made my point clearly. You wrote: "If that's the case, you want to put a range between the : and the g". But if you add a range specifying where the blank lines are, then you don't need the g or the regular expression at all.
    – Rich
    Commented Sep 30, 2022 at 13:22
  • @Rich Dang, you're right. I even removed the g when I was testing it, but forgot to do so when I typed the answer.
    – MDeBusk
    Commented Sep 30, 2022 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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