Skip to content

Instantly share code, notes, and snippets.

@lispyclouds
Created October 19, 2020 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lispyclouds/d21235724145efe3c97d92a699b1cdce to your computer and use it in GitHub Desktop.
Save lispyclouds/d21235724145efe3c97d92a699b1cdce to your computer and use it in GitHub Desktop.
Removes dupes from ZSH history, preserving timestamps. Compatible with babashka
(require '[clojure.java.io :as io]
'[clojure.string :as s])
(defn line->cmd
[line]
^String
(some->> (s/index-of line \;)
inc
(subs line)))
(with-open [rdr (io/reader (str (System/getProperty "user.home") "/.zsh_history"))]
(let [lines (loop [[line & lines] (line-seq rdr)
uniq-cmds (transient #{})
uniq-lines (transient [])]
#_(println (format "Remaining: %d Unique: %d"
(count lines)
(count uniq-cmds)))
(if (nil? line)
(persistent! uniq-lines)
(let [cmd (line->cmd line)]
(if (uniq-cmds cmd)
(recur lines uniq-cmds uniq-lines)
(recur lines (conj! uniq-cmds cmd) (conj! uniq-lines line))))))]
(doseq [line lines]
(println line))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment