A few days ago I needed to look up some indenting functions in Emacs, and I found something I either didn’t know or didn’t remember. (I already knew about <TAB>
(indent-for-tab-command
, which tries to DWIM and – at least in JS mode – can be pretty smart at it) and the very useful C-M-\
(indent-region
, which does exactly what it says on the tin). However, I forgot about C-x <TAB>
(indent-rigidly
). Given a prefix argument, it adds that many spaces of indentation to every line in the region. It is even smart enough to use tabs whenever suitable (i.e., if indent-tabs-mode
is non-nil and the prefix argument equals at least tab-width
), and it can delete indentation when given a negative argument.
Even better, you can run it without any prefix argument, and then it enters a (kind of) interactive mode. (It is actually quite interesting, since it is not a minor mode per se – rather, it uses set-transient-map
, which is a bit like Hydra, but with fewer bells and whistles and built-in into Emacs.) In this “mode” you can press the right/left arrow keys to increase or decrease indentation of the region by one space, or the same arrows with shift
to do it by one tab.
There is also another, similar command called indent-code-rigidly
. It lacks the interactive “mode” of indent-rigidly
, but has another cool feature: it does not touch indentation in lines whose beginning falls in a string. This is quite nice if you happen to code in a language that allows for multi-line strings.
Interestingly, while its docstring claims that it also doesn’t affect multiline comments, this particular feature didn’t work for me – and a cursory scan of the source code confirmed that this is indeed the case. (I filed a bug report about it, although frankly, I’m not sure if I wanted this command not to affect multiline comments. That is, if I used multiline comments at all, which I hardly ever do.)
Anyway, I thought I could share this knowledge – it seems that these commands might not be known to everyone, and while they don’t look like something you might use every day, they can definitely be handy once in a while.