1

Adjust eshell configuration

This commit is contained in:
Jessie Hildebrandt 2023-03-16 11:37:31 -04:00
parent b7e10696df
commit bd44bea8ea

89
init.el
View File

@ -440,8 +440,41 @@ DOCSTRING is an optional form that is discarded upon expansion."
;; eshell ;; eshell
;; ---------------------------------- ;; ;; ---------------------------------- ;;
(defun user/abbrev-path (path) (defun user/open-eshell (&optional force-new-session)
"Return a PATH with all but the last directory name abbreviated." "Focus the last active `eshell' session, or start a new one if none available.
When FORCE-NEW-SESSION is non-nil, a new session will be started even if there
is already an active session to bring into focus."
(interactive)
(let ((eshell-buffer (cl-find-if (lambda (buffer)
(eq (buffer-local-value 'major-mode buffer)
'eshell-mode))
(buffer-list))))
(if eshell-buffer
(if force-new-session
(let ((current-buffer-directory default-directory))
(pop-to-buffer eshell-buffer)
(let ((default-directory current-buffer-directory))
(eshell :new-session)))
(pop-to-buffer eshell-buffer))
(eshell))))
(defun user/auto-toggle-eshell-buffer-tabs ()
"Enable `tab-line-mode' in all `eshell-mode' buffers if multiple are open.
Also automatically disables `tab-line-mode' if all but one `eshell-mode' buffer
have been closed."
(let ((eshell-buffers (cl-remove-if-not (lambda (buffer)
(eq (buffer-local-value 'major-mode buffer)
'eshell-mode))
(buffer-list))))
(mapc (lambda (buffer)
(with-current-buffer buffer
(tab-line-mode (and (length< eshell-buffers 2) -1))))
eshell-buffers)))
(defun user/prettify-eshell-pwd (path)
"Return PATH formatted and with all but the last directory name abbreviated."
(let* ((components (split-string (abbreviate-file-name path) "/")) (let* ((components (split-string (abbreviate-file-name path) "/"))
(str "")) (str ""))
(while (cdr components) (while (cdr components)
@ -456,38 +489,13 @@ DOCSTRING is an optional form that is discarded upon expansion."
(string (elt (car components) 0) ?/))))) (string (elt (car components) 0) ?/)))))
components (cdr components))) components (cdr components)))
(concat (propertize str 'face 'font-lock-comment-face) (concat (propertize str 'face 'font-lock-comment-face)
(propertize (cl-reduce (lambda (a b) (propertize (cl-reduce (lambda (a b) (concat a "/" b))
(concat a "/" b))
components) components)
'face 'font-lock-doc-face)))) 'face 'font-lock-doc-face))))
(defun user/open-eshell (&optional force-new-session)
"Focus the last active `eshell' session, or start a new one if none unavaiable.
When FORCE-NEW-SESSION is non-nil, a new session will be started even if there
is already an active session to switch to or focus."
(interactive)
(let ((eshell-buffer (cl-find-if (lambda (buffer)
(eq (buffer-local-value 'major-mode buffer)
'eshell-mode))
(buffer-list))))
(if eshell-buffer
(if force-new-session
(let ((current-buffer-directory default-directory))
(pop-to-buffer eshell-buffer '((display-buffer-reuse-window display-buffer-same-window)))
(let ((default-directory current-buffer-directory))
(eshell :new-session)))
(pop-to-buffer eshell-buffer '((display-buffer-reuse-window display-buffer-same-window))))
(eshell))))
(editor-feature eshell (editor-feature eshell
"Provides a shell-like interpreter that can process shell or Lisp commands." "Provides a shell-like interpreter that can process shell or Lisp commands."
:custom :custom
(eshell-banner-message '(concat "\n" (if (executable-find "fortune")
(shell-command-to-string "fortune -s computers")
"Hello, commander.")))
(eshell-prompt-function (lambda ()
(concat "\n" (user/abbrev-path (eshell/pwd)) (if (= (user-uid) 0) " # " ""))))
(eshell-prompt-regexp "^[^#❱\n]* [#❱] ") (eshell-prompt-regexp "^[^#❱\n]* [#❱] ")
(eshell-visual-commands '("fish" "bash" (eshell-visual-commands '("fish" "bash"
"ssh" "mosh" "ssh" "mosh"
@ -495,22 +503,29 @@ is already an active session to switch to or focus."
(eshell-destroy-buffer-when-process-dies t "Destroys child buffers after their process returns") (eshell-destroy-buffer-when-process-dies t "Destroys child buffers after their process returns")
(eshell-error-if-no-glob t "Produces an error if a glob pattern fails to match, like zsh") (eshell-error-if-no-glob t "Produces an error if a glob pattern fails to match, like zsh")
(eshell-hist-ignoredups t "Treat multiple repeating history entries as a single entry") (eshell-hist-ignoredups t "Treat multiple repeating history entries as a single entry")
(eshell-banner-message '(if (executable-find "fortune")
(shell-command-to-string "fortune -s computers")
"Hello, commander."))
(eshell-prompt-function (lambda ()
(concat "\n"
(user/prettify-eshell-pwd (eshell/pwd))
(if (= (user-uid) 0)
" # "
""))))
:hook :hook
(eshell-post-command-hook . (lambda () (eshell-post-command-hook . (lambda ()
(rename-buffer (concat eshell-buffer-name (rename-buffer (concat eshell-buffer-name
" " " "
(user/abbrev-path (eshell/pwd))) (user/prettify-eshell-pwd (eshell/pwd)))
:unique))) :unique)))
(eshell-mode-hook . (lambda () (eshell-mode-hook . (lambda ()
(setq-local line-spacing 0.15 (setq-local line-spacing 0.15
global-hl-line-mode nil global-hl-line-mode nil
tab-line-tabs-function #'tab-line-tabs-mode-buffers) tab-line-tabs-function #'tab-line-tabs-mode-buffers)))
(tab-line-mode))) (buffer-list-update-hook . user/auto-toggle-eshell-buffer-tabs)
:bind :bind*
("C-c RET" . user/open-eshell) ("C-c RET" . user/open-eshell)
("C-c C-<return>" . (lambda () ("C-c C-<return>" . (lambda () (interactive) (user/open-eshell :force-new-session))))
(interactive)
(user/open-eshell :force-new-session))))
;; `eshell-mode-map' is not available until the `esh-mode' module is loaded, so ;; `eshell-mode-map' is not available until the `esh-mode' module is loaded, so
;; the local keymap bindings are set up here ;; the local keymap bindings are set up here
@ -519,7 +534,6 @@ is already an active session to switch to or focus."
"Handles input from the user and provides `eshell-mode' for eshell." "Handles input from the user and provides `eshell-mode' for eshell."
:bind :bind
(:map eshell-mode-map (:map eshell-mode-map
("C-c RET" . nil)
("C-<return>" . eshell-copy-old-input) ("C-<return>" . eshell-copy-old-input)
("C-c j" . tab-line-switch-to-prev-tab) ("C-c j" . tab-line-switch-to-prev-tab)
("C-c l" . tab-line-switch-to-next-tab))) ("C-c l" . tab-line-switch-to-next-tab)))
@ -530,8 +544,7 @@ is already an active session to switch to or focus."
(editor-feature em-alias (editor-feature em-alias
"Handles creation and management of command aliases for eshell." "Handles creation and management of command aliases for eshell."
:config :config
(eval-after-load 'em-alias (lambda () (eval-after-load 'em-alias (lambda () (add-to-list 'eshell-command-aliases-list '("lsl" "ls -lh $1")))))
(add-to-list 'eshell-command-aliases-list '("lsl" "ls -lh $1")))))
;; ---------------------------------- ;; ;; ---------------------------------- ;;
;; files ;; files