How can I match – in, for example, a group of files from a Dired buffer – all files that have one or more blank lines at the end of the file?

The specific need that prompts this question is to remove those trailing blank lines, but with a manual prompt; so I want to search and replace. So I'm looking for a match as a separate step, maybe with an Emacs regexp pattern.

1 Answer 1


In Dired, % g marks the files whose contents match a regexp.

You should be able to just use % g with this regexp: ^[[:space:]]*\'

That matches any amount of whitespace (including none) at the beginning of the last line of the buffer.

(\' matches the empty string at the end of the buffer.)

  • That is very close; it matches any file with any whitespace at the end of the file, including a non-blank line with its line break (which is whitespace, of course). So it effectively matches any text file with any line of text. Can this be refined to distinguish line-break versus other whitespace?
    – bignose
    Apr 15, 2017 at 7:47
  • Not sure what you are asking. You said one or more blank lines. And I had you confirm that by "blank line" you mean zero or more whitespace chars. If you want something different, please specify exactly what it is. You should be able to adjust the regexp accordingly - e.g., if you want one or more whitespace chars, or if you want more than one newline char at the end. You can also require that the last line end in whitespace (possibly empty) by adding $ before the \'.
    – Drew
    Apr 15, 2017 at 16:59

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.