5

I have some log files that I spend a lot of time reading, and I find myself doing the same series of commands to get useful highlighting each time I open a new log. Typically some combination of M-x highlight-phrase and M-x highlight-lines-matching-regexp.

What is the best way to consolidate this down to a single command (macro perhaps?) Or better yet, have the highlighting applied whenever I open a file matching a specific name pattern?

Millhouse
  • 153
  • 4
  • Clarification: do you mean you want to highlight particular phrases in a new log, make your highlighting *persist* between multiple sessions with the same log, or both? The question body suggests the former, but the question header implies you want to do both. – Dan Dec 01 '14 at 15:04
  • Both, whenever I open one of my log files, regardless if I've opened it previously, I'd like to be able to easily apply the same highlighting. – Millhouse Dec 01 '14 at 18:54

3 Answers3

7

Using a keyboard macro is a valid option, as is defining a custom command. The main advantage of using a keyboard macro is that you don't have to know any Elisp to create it. On the other hand, custom commands are easier to modify later on. I'm going to outline both solutions; feel free to pick the one you're most comfortable with.

Solution 1: Keyboard macro

To record the macro:

  1. Press F3 in a buffer showing a log file to start recording the macro.

  2. Execute commands for highlighting lines and phrases as you normally would.

  3. When you're done, press F4 to stop recording.

To persist the macro:

  1. Name the macro by doing

    M-x name-last-kbd-macro RET highlight-log-file RET

  2. Find your init-file, move point to a place where you would like to insert the macro, and do

    M-x insert-kbd-macro RET highlight-log-file RET

  3. Optional: Bind the macro to a key via

    (global-set-key (kbd "C-c h") 'highlight-log-file)
    
  4. Save your init-file.

Solution 2: Custom command

Let's say you want to highlight

  • all occurrences of foo
  • all lines containing the phrase bar

in your log files. To achieve this you can define the following command:

(defun highlight-log-file ()
  (interactive)
  (highlight-phrase "foo")
  (highlight-lines-matching-regexp "bar"))

(global-set-key (kbd "C-c h") 'highlight-log-file)

If you need to highlight additional phrases/lines, just add more calls to highlight-phrase and highlight-lines-matching-regexp to the body of the command.

Note also that you can instruct Emacs to highlight phrases/lines using a specific color by passing a second argument to the highlight-* functions:

(highlight-phrase "bar" 'hi-green)

Automated highlighting

After defining highlight-log-file using one of the methods shown above you can instruct Emacs to automatically highlight relevant phrases/lines in .log files as follows:

(defun apply-highlighting ()
  (when (string= (file-name-extension (buffer-file-name)) "log")
    (highlight-log-file)))

(add-hook 'find-file-hook 'apply-highlighting)
itsjeyd
  • 14,586
  • 3
  • 58
  • 87
1

May I suggest using a hook?

(add-hook 'find-file-hook (lambda () (highlight-regexp "\\<ImportantWord\\>")))

You could also throw an eye to FixmeMode

Nsukami _
  • 6,341
  • 2
  • 22
  • 35
0

There is a possibility to save highlighting regexps, but to the buffer at point so not sure it is useful to put in a log file. Anyway, it is the function hi-lock-write-interactive-patterns, bound to C-x w b (see the Edit menu).

Ehvince
  • 1,091
  • 10
  • 14