How to display indentation guides in Emacs?

Thotep picture Thotep · Oct 19, 2009 · Viewed 15.1k times · Source

I'm trying to switch to Emacs as my primary source-code editor. I really miss one thing (common in even much simpler editors) - indentation guides (unobtrusive vertical lines which show the indentation level). Is Emacs able to display them?

Answer

antonj picture antonj · Dec 16, 2010

I've made a function highlight-indentation for this purpose, code is on github.

When invoking highlight-indentation without a prefix argument the current indentation level is naively guessed from major mode (python, ruby and languages based on cc-mode). Only works for space indentations. Customize highlight-indent-face to change appearance of indentation lines.

Examples (ruby, python): Ruby, Python example

I also frequently use this snippet that folds all code on an indentation level greater than the current line. It's a great way of getting a quick overview of the outline.

(defun aj-toggle-fold ()
  "Toggle fold all lines larger than indentation on current line"
  (interactive)
  (let ((col 1))
    (save-excursion
      (back-to-indentation)
      (setq col (+ 1 (current-column)))
      (set-selective-display
       (if selective-display nil (or col 1))))))
(global-set-key [(M C i)] 'aj-toggle-fold)