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?
4 Answers
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):
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)
-
8To automatically load this for JavaScript and Python files, I added the following to my .emacs:
(require 'highlight-indentation) (add-hook 'python-mode-hook 'highlight-indentation-mode) (add-hook 'js2-mode-hook 'highlight-indentation-mode)
– TomJul 29, 2012 at 12:13 -
2Hi, I've added this to my .emacs, but doesn't seem my python files look the same as on screen. I don't see any indention lines as in your example. could you advice? Nov 5, 2012 at 11:25
-
2+1 for the toggle-fold. Note to others, if you enable
highlight-indentation-mode
and don't see anything, it's probably because you're using tabs.– harpoJun 12, 2013 at 1:57 -
2This is great... but is it possible to make it appear just as a line or something more like a the fringe dots?– RossMay 23, 2014 at 16:18
-
2Setting the font of highlight-indentation-face to Arial makes the vertical lines thinner:
(set-face-font 'highlight-indentation-face "Arial")
(set-face-background 'highlight-indentation-face "#E0E0E0")
(set-face-attribute 'highlight-indentation-face nil :height 54)
Nov 7, 2015 at 6:13
There now is a mode called highlight-indent-guides which seems to work quite well.
-
2Very nice mode. More recent and in Melpa. Should be the new accepted answer! Oct 20, 2020 at 13:09
to my knowledge nobody has implemented indentation guides for Emacs so far. The closest thing you can get are visualization of TABs with the whitespace package, see Show tabs with a different character (Emacs).
-
Note you can visualize spaces, too:
M-x customize-variable RET whitespace-space
thenM-x customize-variable RET whitespace-style
and add these two tokens to the Lisp expression:spaces spaces-mark
. Eg mywhitespace-style
currently looks like this:'(tabs trailing lines-tail space-before-tab newline indentation empty space-after-tab tab-mark spaces space-mark)
Sep 11, 2013 at 0:36 -
Suppose you could bend ColumnMarker to your needs but it will highlight a column not givin you a single pixel.
I indent with 8 spaces so i have never thought about it ;P
set listchars=tab:░\
(char before the "\" is a block char that will not show up here i think, can be replaced by "|" and still looks good. after the "\" is a space character)vline.el
: emacswiki.org/emacs/VlineMode or a modification thereof such as Highlight Current Column emacswiki.org/emacs/HighlightCurrentColumn or CrossHair Higlighting emacswiki.org/emacs/CrosshairHighlighting Users may wish to implement their own modification to change colors (foreground or background) when point is on a particular column -- e.g., that matches up with thetab-stop-list
; or odds; evens; post fill-column; or point is on any wrapped line after the first visual line, or at right window edge.