I want to convert the timestamps in a .zsh_history file to their readable equivalents.

Does query-replace-regex or some other function provide the ability to replace the match with the value of a function like something like (format-time-string (match 1))?

: 1568128379:0;cp -a ~/.zshrc.pre-oh-my-zsh ~/.zshrc
: 1568128381:0;exit


See my solution to this particular problem - https://emacs.stackexchange.com/a/79254/15477

2 Answers 2


The answer is Yes. C-h f query-replace-regexp says:

In interactive calls, the replacement text can contain ‘\,’ followed by a Lisp expression. Each replacement evaluates that expression to compute the replacement string. Inside of that expression, ‘\&’ is a string denoting the whole match as a string, ‘\N’ for a partial match, ‘\#&’ and ‘\#N’ for the whole or a partial match converted to a number with ‘string-to-number’, and ‘\#’ itself for the number of replacements done so far (starting with zero).


Here is the solution for my .zsh_history example:

The search string: \(: \)\([0-9]\{10\}\)\(:0;\)

The replacement string: \,(concat (format "%6d " (line-number-at-pos)) (rgx-get-time-string (match-string 2)) " "))

A helper function for the unix time string. More of the functionality in the replacement string can be included if preferred.

    (defun rgx-get-time-string (unixtimestr)
     (format-time-string "%Y-%m-%d %H:%M" (string-to-number unixtimestr))

(match-string 2) is an alternate expression for the back reference for the second string \2.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.