7

At work, we have a policy which states that we have to use 4 spaces for indentation, and in my opinion it makes the code harder to read.

Is it possible to indent with 4 spaces, but display it as 2 spaces?

  • 1
    I'd say this has little or nothing to do with spacemacs. Good question though. I'd be interested in a solution. – PythonNut Sep 9 '15 at 17:45
  • 1
    See also emacs.stackexchange.com/questions/13274/… – wasamasa Sep 9 '15 at 17:50
  • 1
    Sort of a "poor man's tabs" approach? I can imagine this being not too hard to apply to all leading spaces, but more complicated if you want to distinguish indentation from alignment. – phils Sep 10 '15 at 0:28
  • 1
    I'm guessing wildly here, but can you tabify at four-space tab stops, then display tabs using two-space stops, and finally untabify at four-space stops? (But this idea will work badly for continuation lines.) – Norman Ramsey Sep 12 '15 at 16:58
  • This should probably be a comment, not an answer. – PythonNut Sep 12 '15 at 18:22
1

There is really only one answer that can work for everyone all of the time, at least if you want both the ability to view indentation increments at different widths and to be able to align continuation lines consistently with the indentation level of the line they are continued from: Use Smart Tabs.

For an explanation of all the issues with indentation and alignment of continuation lines see Tabs Are Evil. (Tabs aren't really evil of course, it's just a problem when they are abused for multiple purposes -- when used correctly they are the enabling tool for viewing indentation at different widths.)

The most important advantage of using tabs for indentation and spaces for alignment is that one can also view indentation increments with different widths outside of the editor. For example by simply setting tab stops every desired number of spaces in one's terminal (emulator), even a raw view (e.g. cat file) of a tab-indented and space-aligned file will look as desired no matter what the terminal's tab width is set to. This technique of Smart Tabs should even work for those other lesser editors, even if only by politely asking their users to be a little more careful about when they press and when they press so that they can manually implement Smart Tabs.

0

Give https://www.emacswiki.org/emacs/redshift-indent.el a try.

See C-h f redshift-indent for usage.

Don't use this version if you use tabs for indentation (although unless you're mixing spaces and tabs, you didn't have a problem to begin with, as for the tabs-only approach you can simply adjust tab-width to suit).

If you're indenting with spaces exclusively, I believe this will be fine.

I wasn't planning to release it until I'd followed up on a text property bug report (which is waiting for me to provide more details of the scenarios in which things don't work), but you might find it works for you regardless. I decided to pop it on the Wiki as-is on account of seeing another question View code with different indentation than saved to disk -- before I realised that the other question wasn't actually a duplicate after all... whereupon I figured I should just post an answer to the proper question...

  • 2
    Please don't host any new projects on Emacswiki. – wasamasa Mar 17 '16 at 6:13
  • 1
    Well my intent is for this to be on GNU ELPA eventually. For now it's on the Wiki. Feel free to try the code, rather than just comment on the hosting. – phils Mar 17 '16 at 10:13

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.