index rss mastodon twitter github linkedin email
Álvaro Ramírez
sponsor

Álvaro Ramírez

20 December 2015 Search bash history with Emacs helm

Following up from changing CWD with helm projectile, here's a way to search your bash history with helm:

helm-bash-history.gif

(defun ar/helm-helm (title candidates on-select-function)
  "Helm with TITLE CANDIDATES and ON-SELECT-FUNCTION."
  (helm :sources `((name . ,title)
                   (candidates . ,candidates)
                   (action . ,on-select-function))
        :buffer "*helm-exec*"
        :candidate-number-limit 10000))

(defun ar/shell-send-command (command)
  "Send COMMAND to shell mode."
  (assert (string-equal mode-name "Shell") nil "Not in Shell mode")
  (goto-char (point-max))
  (comint-kill-input)
  (insert command)
  (comint-send-input))

(defun ar/helm-shell-search-history ()
  "Narrow down bash history with helm."
  (interactive)
  (assert (string-equal mode-name "Shell") nil "Not in Shell mode")
  (ar/helm-helm "bash history"
                (with-temp-buffer
                  (insert-file-contents "~/.bash_history")
                  (reverse
                   (delete-dups
                    (split-string (buffer-string) "\n"))))
                #'ar/shell-send-command))

Bonus: Replace existing M-r binding to use ar/helm-shell-search-history.

(bind-key "M-r" #'ar/helm-shell-search-history shell-mode-map)