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:
Press F3 in a buffer showing a log file to start recording the macro.
Execute commands for highlighting lines and phrases as you normally would.
When you're done, press F4 to stop recording.
To persist the macro:
Name the macro by doing
M-x name-last-kbd-macro
RET highlight-log-file
RET
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
Optional: Bind the macro to a key via
(global-set-key (kbd "C-c h") 'highlight-log-file)
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)