From 8fd98de30046174a6f0c2960a5c7b32252809912 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Fri, 28 May 2021 20:47:58 -0500 Subject: Add eshell last argument https://old.reddit.com/r/emacs/comments/nlefvx/weekly_tips_and_tricks/gzpng5o/ --- init.el | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/init.el b/init.el index 4c3bcdb..8d18dd9 100644 --- a/init.el +++ b/init.el @@ -314,7 +314,45 @@ (define-key eshell-mode-map (kbd "C-d") #'eshell-quit-or-delete-char) (when (boundp 'simple-modeline--mode-line) - (setq mode-line-format '(:eval simple-modeline--mode-line)))))) + (setq mode-line-format '(:eval simple-modeline--mode-line))))) + + (with-eval-after-load 'eshell + ;; Record arguments + (defvar my-eshell-arg-history nil) + (defvar my-eshell-arg-history-index nil) + (add-to-list 'savehist-additional-variables 'my-eshell-arg-history) + + (defun my-eshell-record-args (&rest _) + "Record unique arguments onto the front of `my-eshell-arg-history'." + (setq my-eshell-arg-history + (cl-loop with history = my-eshell-arg-history + for arg in (reverse eshell-last-arguments) + do (setq history (cons arg (remove arg history))) + finally return history))) + + (defun my-eshell-insert-prev-arg () + "Insert an argument from `my-eshell-arg-history' at point." + (interactive) + (if (eq last-command 'my-eshell-insert-prev-arg) + (progn + (let ((pos (point))) + (eshell-backward-argument 1) + (delete-region (point) pos)) + (if-let ((text (nth my-eshell-arg-history-index + my-eshell-arg-history))) + (progn + (insert text) + (cl-incf my-eshell-arg-history-index)) + (insert (cl-first my-eshell-arg-history)) + (setq my-eshell-arg-history-index 1))) + (insert (cl-first my-eshell-arg-history)) + (setq my-eshell-arg-history-index 1))) + + (add-hook 'eshell-mode-hook + (lambda () + (add-hook 'eshell-post-command-hook + #'my-eshell-record-args nil t) + (local-set-key (kbd "M-.") #'my-eshell-insert-prev-arg))))) (setup eww (:option eww-search-prefix "https://duckduckgo.com/html?q=" -- cgit 1.4.1-21-gabe81