0

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

UPDATE:

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

vfclists
  • 1,403
  • 1
  • 12
  • 28

2 Answers2

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).

NickD
  • 29,717
  • 3
  • 27
  • 44
2

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.

match-string

phils
  • 50,977
  • 3
  • 79
  • 122
vfclists
  • 1,403
  • 1
  • 12
  • 28