78

As a programmer, I want to see a ruler at a specific column (usually 80), both so I see when I cross that column, but also to see how close I am getting to it so I can reformat my code early.

The options I have found so far are all not achieving this goal:

  • whitespace-mode, column-enforce-mode, and column-marker only highlight individual rows after the text in the row has already passed the fill-column. I'd like to see when I am getting close to the column, not just when I cross it.
  • fill-column-indicator would be a good solution, except it breaks auto-complete-mode, company-mode, avy, and more. These are issues that seem hard to fix, each requiring an individual workaround -- e.g., see the company-mode issue and the auto-complete-mode issue, the latter over two years old).

Are there any better alternatives?

  • 8
    This is not an answer to your question per se, but a practical solution is to set the width of your window/frame to 80 characters. – Eric Brown Sep 24 '14 at 9:48
  • Thanks for the suggestion. I think I would prefer having the optional width available when necessary, but it's certainly an option. :-) – Jorgen Schäfer Sep 24 '14 at 10:52
  • 5
    I would add to @EricBrown's suggestion, don't change the actual window width, but rather set the window's right margin so the editing space is 80 characters. (set-window-margins nil 0 (max (- (window-width) 80) 0)) So if you have a 120 character wide window, it'll shrink the space you have to actually display code to 80 characters. This way it won't mess up your window configuration and you can toggle it on an off if you'd like. If you have fringes, there will actually be a line drawn at 80 columns. – Jordon Biondo Sep 24 '14 at 19:06
  • 1
    Here's a gist of the idea: gist.github.com/jordonbiondo/aa6d68b680abdb1a5f70 and here it is in action: i.imgur.com/dUx4bNz.gif – Jordon Biondo Sep 24 '14 at 19:35
  • @jordon-biondo - this misses update hooks for window resize, see: bitbucket.org/snippets/ideasman42/zexMG5 – ideasman42 Feb 7 at 10:37
38

fill-column-indicator is the most mature solution, and if you find overlay-based code with which it conflicts, you can add code to suspend fci-mode while the conflicting code is active. For example, the following code makes it work with auto-complete:

  (defun sanityinc/fci-enabled-p () (symbol-value 'fci-mode))

  (defvar sanityinc/fci-mode-suppressed nil)
  (make-variable-buffer-local 'sanityinc/fci-mode-suppressed)

  (defadvice popup-create (before suppress-fci-mode activate)
    "Suspend fci-mode while popups are visible"
    (let ((fci-enabled (sanityinc/fci-enabled-p)))
      (when fci-enabled
        (setq sanityinc/fci-mode-suppressed fci-enabled)
        (turn-off-fci-mode))))

  (defadvice popup-delete (after restore-fci-mode activate)
    "Restore fci-mode when all popups have closed"
    (when (and sanityinc/fci-mode-suppressed
               (null popup-instances))
      (setq sanityinc/fci-mode-suppressed nil)
      (turn-on-fci-mode)))
  • 5
    Or see github.com/company-mode/company-mode/issues/… for a similar solution for company-mode. – Dmitry Sep 29 '14 at 12:57
  • Hi, thanks for sharing the idea. How do I get this to work? I have the code in my .emacs, but I suppose I have to add some hooks for it to work? – PierreE Oct 9 '17 at 20:45
  • Yes, the example above advises popup-create and popup-delete -- depending on your use case, you might need to advise different functions. – sanityinc Oct 13 '17 at 23:20
  • 1
    Unacceptably slow. – Alexander Shukaev Jan 29 at 16:29
21

Here is one option which is more robust, it breaks almost nothing (occasionally company-mode being a noteworthy exception), but is not as convenient as fill-column-indicator.

Use header-line-format to mark the 80th column on the header.
Something like the following should suffice:

(setq-default header-line-format 
              (list " " (make-string 79 ?-) "|"))

You should change the number of spaces in that first string depending on the size of your left fringe. But other than that, this should work reasonably well. It's not as convenient as a ruler in the actual buffer, but it helps.

You can also set it to apply only on programming buffers.

(defun prog-mode-header-line ()
  "Setup the `header-line-format' on for buffers."
  (setq header-line-format 
        (list " " (make-string 79 ?-) "|")))

(add-hook 'prog-mode-hook #'prog-mode-header-line)

Result:
You should get something like the following (the first line is not actually in the buffer, but in the header).

-------------------------------------------------------------------------------|
;; This is what your buffer should look like all the way up to column number 80.
(setq some-dummy-variable we-are-all-friends)
  • 1
    Note that in Emacs 24.3 this will break Company Mode, too, at least in some edge cases. Emacs 24.3 fails to correctly the the header line into account when computing the height of a window. Consequently Company fails to draw proper popups in cases where the height is important, i.e. at the very bottom of the buffer. – lunaryorn Sep 24 '14 at 10:18
  • 1
    This is a really interesting idea. Too bad I can't mark two answers as correct, the "patch fci" answer has some drawbacks and depending on circumstances, this can be a better choice. Thank you! – Jorgen Schäfer Sep 25 '14 at 20:07
  • 8
    There's also M-x ruler-mode. – sanityinc Nov 2 '14 at 14:32
  • This doesn't draw starting after number's (when line numbers displayed) – ideasman42 Oct 14 '16 at 10:11
15

After much suffering because of various bugs fill-column-indicator introduces, I eliminated it from my config for good.

What I currently use is built-in Emacs functionality to highlight lines that are too long. This even looks better, I wouldn't enable fill-column-indicator now even if it were bug-free.

For a start you may grab my setup:

(setq-default
 whitespace-line-column 80
 whitespace-style       '(face lines-tail))

Then enable it where you want. I use it only in programming context:

(add-hook 'prog-mode-hook #'whitespace-mode)
7

This EmacsWiki page has lots of information about different ways to mark a particular column or otherwise let you know when you go past it.

The one I use is Mode Line Position.

But others include showing a vertical line at the column (Column Marker, Fill-Column Indicator) and using whitespace mode to highlight text that goes past the column.

(If you need the line to be far to the right of all text in all rows, you can always turn on picture-mode, but that is probably useful here only as a temporary workaround.)

See also Find Long Lines for ways to find long lines on demand.

6

Not exactually what you want, but ruler like @Malabarba♦ will waster space, here is better solution:

There is a built-in package in emacs-goodies-el(recommend to install it in terminal) called highlight-beyond-fill-column.el, add this to your .emacs or init.el:

(setq-default fill-column 80)
(add-hook 'prog-mode-hook 'highlight-beyond-fill-column)
(custom-set-faces '(highlight-beyond-fill-column-face
                    ((t (:foreground "red" )))))

The text beyond fill-column which is 80 in the snippet will be highlighted with the color of red. You can set the face as you like.

5

Are there any better alternatives?

Emacs 27 will support a fill column indicator natively by way of the buffer-local minor mode display-fill-column-indicator-mode and its global counterpart global-display-fill-column-indicator-mode.

Here it is in action:

enter image description here

Quoth (emacs) Displaying Boundaries:

14.15 Displaying Boundaries
===========================

Emacs can add an indicator to display a fill column position.  The fill
column indicator is a useful functionality specially in prog-mode to
indicate the position of an specific column.

   You can set the buffer-local variables
‘display-fill-column-indicator’ and
‘display-fill-column-indicator-character’ to activate the indicator and
controls how the indicator looks.

   Alternatively you can type ‘M-x display-fill-column-indicator-mode’
or ‘M-x global-display-fill-column-indicator-mode’ which enables the
indicator locally and globally respectively and also chooses the
character to use if none is set already.  It is possible to use the
first one to activate the indicator in a hook or the second one to
enable it globally.

   There are 2 buffer local variables and 1 face to customize this mode:

‘display-fill-column-indicator-column’
     Specifies the column number where the indicator should be set.  It
     can take positive numerical values for the column or the special
     value ‘t’ which means that the variable ‘fill-column’ will be used.

     Any other value disables the indicator.  The default value is ‘t’.

‘display-fill-column-indicator-char’
     Specifies the character used for the indicator.  This character can
     be any valid char including unicode ones if the actual font
     supports them.

     When the mode is enabled through the functions
     ‘display-fill-column-indicator-mode’ or
     ‘global-display-fill-column-indicator-mode’, the initialization
     functions check if this variable is ‘non-nil’, otherwise the
     initialization tries to set it to U+2502 or ‘|’.

‘fill-column-indicator’
     Specifies the face used to display the indicator.  It inherits its
     default values from shadow but without background color.  To change
     the indicator color you need to set only the foreground color of
     this face.

   On graphical displays, Emacs can indicate the buffer boundaries in
the fringes.  If you enable this feature, the first line and the last
line are marked with angle images in the fringes.  This can be combined
with up and down arrow images which say whether it is possible to scroll
the window.

   The buffer-local variable ‘indicate-buffer-boundaries’ controls how
the buffer boundaries and window scrolling is indicated in the fringes.
If the value is ‘left’ or ‘right’, both angle and arrow bitmaps are
displayed in the left or right fringe, respectively.

   If value is an alist (*note (elisp)Association Lists), each element
‘(INDICATOR . POSITION)’ specifies the position of one of the
indicators.  The INDICATOR must be one of ‘top’, ‘bottom’, ‘up’, ‘down’,
or ‘t’ which specifies the default position for the indicators not
present in the alist.  The POSITION is one of ‘left’, ‘right’, or ‘nil’
which specifies not to show this indicator.

   For example, ‘((top . left) (t . right))’ places the top angle bitmap
in left fringe, the bottom angle bitmap in right fringe, and both arrow
bitmaps in right fringe.  To show just the angle bitmaps in the left
fringe, but no arrow bitmaps, use ‘((top . left) (bottom . left))’.
1

Since fill-column-indicator is quite heavy, this solution shows a character to the right of the current line.

So when you're typing you can see the line limit before you exceed it.

This defines the minor-mode hl-line-margin-mode:

;; Global, ensures one active margin for the active buffer.
(defvar hl-line-margin--overlay nil)

(defun hl-line-margin--overlay-clear ()
  "Clear the overlays."
  (when hl-line-margin--overlay
    (delete-overlay hl-line-margin--overlay)
    (setq hl-line-margin--overlay nil)))

(defun hl-line-margin--overlay ()
  "Create the line highlighting overlay."
  ;; Remove in the event of a changed buffer,
  ;; ensures we update for a modified fill-column.
  (when (and hl-line-margin--overlay
             (not (eq (current-buffer)
                      (overlay-buffer hl-line-margin--overlay))))
    (hl-line-margin--overlay-clear))
  (unless hl-line-margin--overlay
    (setq hl-line-margin--overlay (make-overlay 0 0))
    (let ((space `((space :align-to ,fill-column)
                   (space :width 0))))
      (overlay-put hl-line-margin--overlay 'after-string
                   (concat (propertize " " 'display space 'cursor t)
                           (propertize " " 'face '(:inverse-video t))))))
  (let ((eol (line-end-position)))
    (unless (eql eol (overlay-start hl-line-margin--overlay))
      (move-overlay hl-line-margin--overlay eol eol))))

(defun hl-line-margin-mode-enable ()
  "Turn on `hl-line-margin-mode' for the current buffer."
  (add-hook 'post-command-hook #'hl-line-margin--overlay nil t))

(defun hl-line-margin-mode-disable ()
  "Turn off `hl-line-margin-mode' for the current buffer."
  (hl-line-margin--overlay-clear)
  (remove-hook 'post-command-hook #'hl-line-margin--overlay t))

;;;###autoload
(define-minor-mode hl-line-margin-mode
  "Show a character at the fill column of the current line."
  :lighter ""
  (cond (hl-line-margin-mode
         (jit-lock-unregister #'hl-line-margin-mode-enable)
         (hl-line-margin-mode-enable))
        (t
         (jit-lock-unregister #'hl-line-margin-mode-disable)
         (hl-line-margin-mode-disable))))

If you use evil-mode and want to limit this to insert mode you can add the following hooks:

(add-hook 'evil-insert-state-entry-hook #'hl-line-margin-mode-enable)
(add-hook 'evil-insert-state-exit-hook #'hl-line-margin-mode-disable)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.