From 9797b773f6cd8798b8d467c9ccd92e0a44af32f0 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Wed, 27 Oct 2021 08:30:10 -0700 Subject: eshell: open file at point Add a function to open the file at point. In an eshell session, I can then move the cursor to a file or a directory, and call `fcuny/eshell-open-file-at-point`: for a file, a new buffer is open, if it's a directory, we're changing the working directory to the given path. Bind the function to "C-o" in eshell. --- emacs/custom/fcuny-eshell.el | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'emacs/custom') diff --git a/emacs/custom/fcuny-eshell.el b/emacs/custom/fcuny-eshell.el index 53c3f69..2f53988 100644 --- a/emacs/custom/fcuny-eshell.el +++ b/emacs/custom/fcuny-eshell.el @@ -51,6 +51,25 @@ (magit-status (pop args) nil) (eshell/echo)) ;; The echo command suppresses output +(defun fcuny/eshell--open-or-cd (path) + "Cd to PATH if path is a directory ((file-name-directory path) => t), otherwise open PATH via `find-file'." + (interactive) + (if (file-directory-p path) + (progn + (goto-char (point-max)) + (insert (concat "cd " path)) + (eshell-send-input)) + (find-file path))) + +(defun fcuny/eshell-open-file-at-point () + (interactive) + (let ((filename (symbol-name (symbol-at-point)))) + (cond + ((file-readable-p filename) + (fcuny/eshell--open-or-cd filename)) + ((file-readable-p (expand-file-name filename)) + (fcuny/eshell--open-or-cd (expand-file-name filename)))))) + (defun fcuny/shorten-path (path &optional max-len) "Return a potentially trimmed-down version of the directory PATH, replacing parent directories with their initial characters to try to get the character @@ -168,7 +187,9 @@ append to it, while separating multiple outputs with :commands (eshell eshell-command) :bind (("C-c e h" . fcuny/eshell-here) ("C-c e e" . fcuny/eshell-export) - ("C-c r" . counsel-esh-history)) + ("C-c r" . counsel-esh-history) + :map eshell-mode-map + ("C-o" . fcuny/eshell-open-file-at-point)) :custom (eshell-scroll-to-bottom-on-input 'all) (eshell-error-if-no-glob t) -- cgit 1.4.1