Using fill-column-indicator for Characters Per Line limit
I prefer limiting characters per line for my code, and I'm not alone.
I'm getting natural limit by always using Emacs in the 2 column mode (C-x 3
).
If you want to get a proper visual advice, you can use fill-column
.
Normally fill-column
is consulted when Emacs wants to reformat text in a paragraph, for example if one press M-q
or (fill-paragraph)
.
But we can also use it to solve our task. Notice the light-grey vertical line:
And here is the code, which should be self explanatory.
(defun my-toggle-fill-column-indicator ()
"Toggle displaying of fill column indicator."
(interactive)
(if display-fill-column-indicator
(setq display-fill-column-indicator nil)
(setq display-fill-column-indicator t)
(setq display-fill-column-indicator-character ?\N{U+2506})))
You can of course M-x customize-variable fill-column
or setq
it.
Or just run M-x set-fill-column
to interactively change it on the fly.
If you wish you can enable it for any programming mode:
(add-hook 'prog-mode-hook #'my-toggle-fill-column-indicator)
And finally here some options for styling indicator itself:
;; ┊
(setq-default display-fill-column-indicator-character ?\N{U+2506})
;; ┆
(setq-default display-fill-column-indicator-character ?\N{U+250A})
;; ╎
(setq-default display-fill-column-indicator-character ?\N{U+254E})
;; face customization:
(customize-face 'fill-column-indicator)
;; or put something like this in your theme
'(fill-column-indicator ((t (:foreground "#4e4e4e"))))
NOTE: display-fill-column-indicator
is available since Emacs 27.1
You may use fill-column-indicator package, though I haven't tried it myself.