Our project has a large yaml file edited by multiple developers in multiple environments. When I edit the file in emacs, yaml-mode (I assume?) will trim any whitespace lines:

           Item: foo
           Count: 10
     - Item: Name

The hyphens above represent the legal spaces that yaml-mode deletes on save.

I have no strong opinions on the "correct" whitespace here but since my editor is in the minority in the team, I'd like it to be consistent and leave those spaces.

How can I ask or hack yaml mode to do this?

  • 1
    Look into before-save-hooks, not sure it's yaml-mode. More likely your other global settings.
    – wvxvw
    Jul 23, 2018 at 10:55
  • Try it without your init.el or .emacs. Maybe you have global-whitespace-cleanup-mode activated?
    – Maxim Kim
    Jul 23, 2018 at 12:03
  • @wvxvw ah yeah it's prelude-cleanup-maybe in my before-save-hooks. I can disable that with prelude-clean-whitespace-on-save, once I figure out how to do a mode-local variable...
    – tenpn
    Jul 23, 2018 at 14:07

1 Answer 1


With help from @wvxvw, the problem wasn't yaml-mode, but my before-save-hooks. That listed prelude-cleanup-maybe as the only entry. Disabling prelude-clean-whitespace-on-save fixed the problem. With help from this question, the code below makes that change every time I open a yaml file:

;; whitespace cleanup can conflict with other people's IDEs, so don't do it
(add-hook 'yaml-mode-hook
          (lambda ()
            (make-variable-buffer-local 'prelude-clean-whitespace-on-save)
            (setq prelude-clean-whitespace-on-save nil)))

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.