regexemacsemacs-prelude

Emacs Prelude `whitespace-empty-at-eob-regexp` allow single empty line


On saving a file, whitespace-cleanup does a nice job of cleaning up whitespace:

https://emacsredux.com/blog/2013/05/16/whitespace-cleanup/

However, I would like to be able to leave a blank line at the end of buffer, either globally or add a hook for different modes.

I think if I edit this whitespace-empty-at-eob-regexp I should be able to do it?

whitespace-empty-at-eob-regexp’s value is "^\\([ ]+\\)\\'"

So I thought I could just add a \n but this does not achieve what I want:

"^\\([ ]+\\n\)\\'"


Solution

  • The value of whitespace-empty-at-eob-regexp is "^\\([ \t\n]+\\)\\'" actually.

    I you want to leave a blank line at the end of buffer. Try:

    (setq whitespace-empty-at-eob-regexp "^[ \t\n]\\([ \t\n]+\\)\\'")
    
    ;; Tips: you can use xr and rx to play with regex more relaxed in emacs.
    ;; (xr "^[ \t\n]\\([ \t\n]+\\)\\'")
    ;; (seq bol
    ;;      (any "\t\n ")
    ;;      (group
    ;;       (one-or-more
    ;;        (any "\t\n ")))
    ;;      eos)