3

Is there a way to use auto-indentation in Emacs without creating indentation on empty lines?

For an example from C, if I have

if (true)
{
   printf("word");
}

and then insert a newline after the opening brace

if (true)
{
>>>
   printf("word");
}

then a new line is created and if auto-indention is enabled then extra space is also added, here represented by the symbol >.

However, when I commit this code using a VCS, I DO NOT want the extra space from the empty line to be included, but I DO want the newline.

Is there an auto-indentation system for Emacs which will only add extra space to the buffer if I type a character after creating a newline? Or else is there a different scheme that could accomplish my goal of avoiding adding spaces on empty lines?

6

Perhaps an even better idea is to use ws-butler, a mode that unobtrusively trims the relevant trailing whitespace whenever the buffer is saved (be it from auto-indentation or some other source). Even better, it tracks your changes, and only cleans up the lines you touch, which is handy for editing with other people who aren't so tidy under VC.

Just M-x package-install ws-butler (you do have MELPA or Marmalade, right?)

(ws-butler-global-mode +1)

And away you go...

  • 1
    The other alternative I know of is ws-trim, and I would highly recommend using one or the other. (Some people like to automatically trim all trailing whitespace when saving a file, but that's a massive VC fail.) – phils Sep 14 '15 at 4:14
  • @phils Thanks guys! This is exactly what I originally wanted, but figured no such package existed. Is there a reason to choose one package over the other? – holocronweaver Sep 14 '15 at 5:00
  • They do work differently, so maybe try them both. ws-trim is available on Marmalade, btw. See also the "history" part of the ws-butler readme. – phils Sep 14 '15 at 5:46

Your Answer

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

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