I am aware that it is a good idea to have one blank line at the end of each file. I am also aware that extra white space in a file is bad. Currently in my .emacs file I have (add-hook 'before-save-hook 'delete-trailing-whitespace) so that whitespace is removed during saving.

However, despite what the wiki says, this also deletes the final newline from my file.

I tried setting '(require-final-newline (quote visit-save)) but that had no effect.

Is there a way that I can force a newline to be added and all other extra whitespace removed?

  • I always try to steer people away from this idea, because it's so awful as soon as version control is involved. I suggest instead that you use one of the ws-trim or ws-butler libraries to ensure that lines you edit do not have trailing whitespace. You can always call delete-trailing-whitespace manually if you genuinely want to do this to a given file.
    – phils
    Apr 7, 2016 at 7:28
  • Do you have a preference between the two?
    – Startec
    Apr 7, 2016 at 7:29
  • 1
    I use ws-trim due to inertia (it's been around much longer), but I think ws-butler has had some good improvements since it first appeared, and is possibly the better option, so I'd suggest trying that one first.
    – phils
    Apr 7, 2016 at 7:31
  • Uh, could you help me enable butler mode by default?
    – Startec
    Apr 7, 2016 at 7:42
  • 1
    Sure. If you package-install it from MELPA and then put (ws-butler-global-mode 1) in your init file, it looks like it should be enabled for all buffers. If you only want it for programming, start with (add-hook 'prog-mode-hook 'ws-butler-mode) instead, as the majority of programming modes are derived from prog-mode.
    – phils
    Apr 7, 2016 at 21:24


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.