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?
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?
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.
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...