1

For clarity: I mean indents as represented by individual spaces, not tab characters.

I'm working with a friend on a javascript project, and while I'm an 80col zealot, he wants to have 4 space indent. It's not the end of the world, but it bothers the heck out of my eyes.

I know about the .dir-locals.el trick, so I'll just stick the tab width in there, but I was wondering if there was a display hook or extension that would print any block identation as 2 spaces? For example, show this block

function greet(name) {
    return `Hello, ${name}`
}

as this one

function greet(name) {
  return `Hello, ${name}`
}

while actually writing 4 spaces into the file on disk. We already use prettier, so I can chuck in anything without having to worry what comes out on the other end, but I'd prefer to have a solution that just changes the display of the buffer in emacs. I don't really want to hassle with changing the real file contents every time I open a file.

2
  • Sorry, fixed for clarity Sep 21, 2019 at 4:50
  • Great, in that case my answer should apply to your use-case. Enjoy!
    – phils
    Sep 21, 2019 at 8:16

2 Answers 2

1

You can (ab)use prettify-symbols-mode for this.

(push '("  " . ?\ ) prettify-symbols-alist)

followed by M-x prettify-symbols-mode

0

If the indentation is spaces rather than tabs, give redshift-indent a try. I failed to follow up on some upstream text-property enhancements which are needed to make this idea work in all situations, and consequently tabs can cause problems1; but if your indentation is spaces, then this will probably still work fine.

See the library Commentary for details, and use the M-x redshift-indent command to try it out:

Invoke `redshift-indent-mode' with a buffer-local cosmological constant.

With no prefix ARG, `redshift-indent-mode' is toggled.
If ARG is positive, `redshift-cosmological-constant' is set to ARG.
If ARG is negative, `redshift-cosmological-constant' is set to 1.0 / (abs ARG).
If ARG is C-u, `redshift-cosmological-constant' is prompted for interactively.

e.g.:
With prefix arg 2, indentation will appear 2 times its normal width.
With prefix arg -2, indentation will appear 0.5 times its normal width.

1 In a local copy (with the same code), I have a more specific note that "The current implementation unavoidably breaks for tabs which are visually narrower than tab-width."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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