I noticed that my Emacs didn’t maintain the current line’s indentation when editing code blocks in Markdown (
markdown-mode). I indent the first line with 4 spaces like any sane person would. When I hit enter to continue in the next line, thanks to the
markdown-mode defaults, I am presented with a new line that’s properly indented visually. Only when committing to git did I notice that Emacs inserted tabs instead of spaces. Gasp!
Turns out you’re supposed to turn this off for
text-mode (a parent mode of Markdown). See the great Emacs indentation tutorial for a good reference.
Using hooks, here’s what I am now using to indent by 4 spaces instead of adhering to tab stops:
(add-hook 'text-mode-hook '(lambda () (setq indent-tabs-mode nil) (setq tab-width 4)))
This will also affect the “press tab to indent the current line” behavior that’s active even when your in the middle of text. I needed to get used to this at first, because I expected hitting tab to insert a tabulator at the current cursor position. By default, this
indent-line-function is configured to indent the current line up to the previous line’s indentation, at least for a lot of cases. In some modes, it is even more aware of the context and does clever things. You can change this to be dump and always indent by 4 spaces, too. Just add this to the hook’s lambda list of expressions:
(setq indent-line-function (quote insert-tab))