6

highlight-regexp is pretty amazing, but when I run it manually by M-x highlight-regexp RET f?o RET 'someface it only applies the highlight to the current buffer. How can I apply a highlight to multiple buffers?

Similarly I would like to automatically highlight tabs in all files with a nasty face as I am allergic to them. Can I put something in my init.el to do that?

  • Are you sure you need to highlight tabs with highlight-regexp or other method would be acceptable? Do you want to highlight them always or temporarily? – Mark Karpov Sep 16 '15 at 15:12
  • Another method would be welcome, too. I would also like to permanently highlight trailing whitespace in every buffer I have open, which I have been doing with highlight-regexp. – Matthew Gilliard Sep 16 '15 at 15:16
  • 1
    For tabs I'd look at whitespace mode, which can can be configured to highlight various kinds of whitespace. You can also configure Emacs to never insert tabs or to replace them with spaces... – glucas Sep 16 '15 at 15:20
  • OK thanks. But I'd still like to know how to highlight-regexp in all buffers at once. – Matthew Gilliard Sep 16 '15 at 15:23
  • 1
    Very closely related: emacs.stackexchange.com/q/3202/115 – Kaushal Modi Sep 16 '15 at 16:26
8
  • Run M-x ibuffer to show all open buffers.
  • Press m to mark buffers of interest (or one of the other more interesting marking commands).
  • Press E to evaluate a lisp form in each marked buffer, for example,

    (highlight-regexp "regexp-custard" 'hi-yellow)
    

You can save the patterns for a particular file with hi-lock-write-interactive-patterns (Default binding: M-s h w).

More generally, if you always want to highlight the same regexps across a range of files you could call highlight-regexp from a mode hook or find-file-hook.

  • Wow! I didn't know about this 'eval' feature in 'ibuffer'! – NVaughan Nov 27 '16 at 0:14
5

You can solve all your problems with help of built-in minor mode called whitespace-mode. This mode can be set up to highlight lines that are too long as well as all sorts of normally “invisible” characters.

Here is basic setup that will highlight lines longer 80 characters and tabs:

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

To enable it you can use one of these methods:

(whitespace-mode 1)        ; enable locally
(global-whitespace-mode 1) ; enable everywhere

To find out more about things the mode can highlight, see documentation of whitespace-style variable: C-h v whitespace-style RET.

  • My bad for asking two questions at once :( This is a great answer for the 2nd one. – Matthew Gilliard Sep 16 '15 at 15:28
3
  • You can use library Highlight (code: highlight.el) to do what you want. Command hlt-highlight-regexp-region-in-buffers highlights matches for a regexp across a set of buffers.

  • You can use library Highlight Chars (code: highlight-chars.el) to highlight any character in a single buffer or all buffers. Command hc-toggle-highlight-tabs, for example, highlights TAB characters using face hc-tab, which you can of course customize.

  • So it looks like it's not possible with the normal highlight-* commands then. Thanks for the recommendation(s). – Matthew Gilliard Sep 16 '15 at 19:01
  • @MatthewGilliard: I didn't say that, and I don't know that. Someone else can speak to that. I use the libraries I cited, and have been doing so since long before vanilla Emacs had any such highlighting commands - before standard library hi-lock.el existed. – Drew Sep 16 '15 at 23:47

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.