How can I make emacs highlight lines that go over 80 chars?

后端 未结 8 1520
不思量自难忘°
不思量自难忘° 2020-12-01 02:18

I know there are solutions to making emacs show the 80 line column, but I don\'t want that sort of visual disturbance. I\'d just like to make it highlight a line if it\'s ov

相关标签:
8条回答
  • 2020-12-01 02:24

    Try highlight-80+.el. You can acquire it from here.

    To install it, just add the following to your .emacs:

    (add-to-list 'load-path "/path/to/highlight-80+")
    (require 'highlight-80+)
    

    You can then enable it via:

    M-x highlight-80+-mode

    0 讨论(0)
  • 2020-12-01 02:32

    Here's my config from Emacs Dev Kit:

    ;; whitespace-mode
    ;; free of trailing whitespace and to use 80-column width, standard indentation
    (setq whitespace-style '(trailing lines space-before-tab
                                      indentation space-after-tab)
          whitespace-line-column 80)
    

    Basically you need just the last bit, but I find the other settings quite useful (I hate tabs and trailing whitespaces).

    0 讨论(0)
  • 2020-12-01 02:35

    Another easy option is to run highlight-lines-matching-regexp on the expression .\{81\}.

    Every line with 81 characters or more will be highlighted with the color of your choice.

    0 讨论(0)
  • 2020-12-01 02:37

    Here is some example code which will highlight text that lies beyond column 80 with the current 'warning' face, and a line to enable it for C++ mode.

    ;; Turn on warn highlighting for characters outside of the 'width' char limit
    (defun font-lock-width-keyword (width)
      "Return a font-lock style keyword for a string beyond width WIDTH
       that uses 'font-lock-warning-face'."
      `((,(format "^%s\\(.+\\)" (make-string width ?.))
         (1 font-lock-warning-face t))))
    
    (font-lock-add-keywords 'c++-mode (font-lock-width-keyword 80))
    

    It doesn't highlight the whole line, but I find it is reasonably helpful.

    0 讨论(0)
  • 2020-12-01 02:38

    See whitespace-mode -- it's now part of Emacs, and can do much more than highlighting just long lines. (But of course can be used to do only that.)

    0 讨论(0)
  • 2020-12-01 02:39

    This is similar to other answers here, with the advantage of using fill-column, and it runs after the mode switches. So each mode can have it's own line-width which is properly respected by the highlighting.

    (add-hook 'after-change-major-mode-hook
      (lambda ()
        (when (derived-mode-p 'prog-mode)
          (let ((column-re (format "^[^\n]\\{%d\\}\\(.*\\)$" fill-column)))
            (font-lock-add-keywords nil
              `((,column-re 1 font-lock-warning-face prepend)))))))
    

    You may wan't to remove the prog-mode check if you like this to be enabled for text files too.

    0 讨论(0)
提交回复
热议问题