this post was submitted on 20 Oct 2023
3 points (100.0% liked)

Emacs

311 readers
2 users here now

A community for the timeless and infinitely powerful editor. Want to see what Emacs is capable of?!

Get Emacs

Rules

  1. Posts should be emacs related
  2. Be kind please
  3. Yes, we already know: Google results for "emacs" and "vi" link to each other. We good.

Emacs Resources

Emacs Tutorials

Useful Emacs configuration files and distributions

Quick pain-saver tip

founded 1 year ago
MODERATORS
 

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

Is there some search and replace I can do in emacs that will replace the time with something like (format-time-string unixtime)?

: 1568128379:0;cp -a ~/.zshrc.pre-oh-my-zsh ~/.zshrc
: 1568128381:0;exit
top 4 comments
sorted by: hot top controversial new old
[–] jsled@alien.top 1 points 1 year ago

C-h a replace-regexp , select the function, and look at its help string.

The solution will end up looking something like:

M-x replace-regexp  : \([0-9]+\):\(.*\)  \,(+ 1 \#1):\2

But with (format-time-string) instead of my (+1 1 \#1); I don't know time formatting functions in emacs elisp off the top of my head, sorry.

(Or use query-replace-regexp if you want interactivity.)

[–] SnooPets20@alien.top 1 points 1 year ago (1 children)
[–] vfclists@alien.top 1 points 1 year ago

And how is this a meaningful answer to the question?

Is there some rule that questions asked here shouldn't be asked on stackexchange too?

[–] vfclists@alien.top 1 points 1 year ago

Here is the solution for my .zsh_history example:

I first created a small 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))
      )

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

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

(match-string 2) is an alternative for the back reference for the second string matched\2