How can I make emacs highlight lines that go over 80 chars?
I know there are solutions to making emacs show the 80 line column, but I don't want that so开发者_运维问答rt of visual disturbance. I'd just like to make it highlight a line if it's over 80 characters.
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.
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.)
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).
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
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.
This is not exactly what you wanted, but I had a similar problem and found this question/answer. All I wanted was to insert a visual guide, when I needed it. This is what I finally ended up with:
(defun insert-80 ()
"Insert an 80-character-wide guide at beginning of line."
(interactive)
(beginning-of-line)
(insert "0 1 2 3 4 5 6 7 |")
(newline)
(insert "01234567890123456789012345678901234567890123456789012345678901234567890123456789|")
(newline)
)
Super simple, but sometimes very effective.
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.
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.
精彩评论