If i uncomment the commented code and then press 'U' the pointer jumps back to the start of the block #+begin_src emacs-lisp (defvar eshell-toggle-window-configuration nil "Variable to store the window configuration before opening eshell.") (defvar eshell-toggle-selected-window nil "Variable to store the selected window before opening eshell.") (defun SpawnEshellSplitBelow () "Open a shell in a small split below or toggle it if already open." (interactive) (if (eq major-mode 'eshell-mode) (progn (when eshell-toggle-window-configuration (set-window-configuration eshell-toggle-window-configuration) (setq eshell-toggle-window-configuration nil)) (when eshell-toggle-selected-window (select-window eshell-toggle-selected-window) (setq eshell-toggle-selected-window nil))) (setq eshell-toggle-window-configuration (current-window-configuration)) (setq eshell-toggle-selected-window (selected-window)) ;; Calculate one third of the total window height (let ((one-third-height (/ (window-total-height) 3))) ;; Ensure the height is at least 1 to avoid errors (setq one-third-height (max one-third-height 1)) (split-window-below (- one-third-height)) (other-window 1) (open-eshell-in-current-directory)))) (defun open-eshell-in-current-directory () "Open eshell in the directory of the current buffer. If an eshell buffer for the directory already exists, switch to it." (interactive) (let* ((buffer-dir (if (buffer-file-name) (file-name-directory (buffer-file-name)) default-directory)) (eshell-buffer-name (concat "*eshell:" buffer-dir "*")) (existing-eshell-buffer (get-buffer eshell-buffer-name))) (if existing-eshell-buffer (switch-to-buffer existing-eshell-buffer) (let ((eshell-buffer (eshell 'N))) (with-current-buffer eshell-buffer (rename-buffer eshell-buffer-name) (eshell/cd buffer-dir)))))) (with-eval-after-load 'evil (define-key evil-normal-state-map (kbd "M-e") 'SpawnEshellSplitBelow) (define-key evil-insert-state-map (kbd "M-e") 'SpawnEshellSplitBelow)) ;; (define-key evil-normal-state-map (kbd "M-e") 'open-eshell-in-current-directory)) (defun SpawnEshellInProjectRoot () "Open eshell in the project's root directory or toggle it if already open." (interactive) (if (eq major-mode 'eshell-mode) (progn (when eshell-toggle-window-configuration (set-window-configuration eshell-toggle-window-configuration) (setq eshell-toggle-window-configuration nil)) (when eshell-toggle-selected-window (select-window eshell-toggle-selected-window) (setq eshell-toggle-selected-window nil))) (setq eshell-toggle-window-configuration (current-window-configuration)) (setq eshell-toggle-selected-window (selected-window)) ;; Calculate one third of the total window height (let ((one-third-height (/ (window-total-height) 3))) ;; Ensure the height is at least 1 to avoid errors (setq one-third-height (max one-third-height 1)) (split-window-below (- one-third-height)) (other-window 1) (let ((project-root (projectile-project-root))) (open-eshell-in-directory project-root))))) (defun open-eshell-in-directory (dir) "Open eshell in the specified directory DIR. If an eshell buffer for the directory already exists, switch to it." (interactive "DDirectory: ") (let* ((eshell-buffer-name (concat "*eshell:" dir "*")) (existing-eshell-buffer (get-buffer eshell-buffer-name))) (if existing-eshell-buffer (switch-to-buffer existing-eshell-buffer) (let ((eshell-buffer (eshell 'N))) (with-current-buffer eshell-buffer (rename-buffer eshell-buffer-name) (eshell/cd dir)))))) (with-eval-after-load 'evil (define-key evil-normal-state-map (kbd "M-p") 'SpawnEshellInProjectRoot)) (defun kill-all-eshell-buffers () "Kill all Eshell buffers." (interactive) (dolist (buffer (buffer-list)) (when (string-match-p "^\\*eshell\\*" (buffer-name buffer)) (kill-buffer buffer)))) ;; (defvar my-saved-window-configuration nil ;; "Variable to store the saved window configuration.") ;; (defun my-eshell-fullscreen () ;; "Replace the current window layout with a fullscreen Eshell." ;; (interactive) ;; (setq my-saved-window-configuration (current-window-configuration)) ;; (delete-other-windows) ;; (eshell)) ;; (defun my-restore-window-configuration () ;; "Restore the previously saved window configuration." ;; (interactive) ;; (when my-saved-window-configuration ;; (set-window-configuration my-saved-window-configuration))) (defvar my-saved-tab-configurations (make-hash-table :test 'equal) "Hash table to store the saved window configurations per tab.") (defun my-current-tab-name () "Get the current tab's name." (alist-get 'name (car (funcall tab-bar-tabs-function)))) (defun my-eshell-fullscreen () "Replace the current window layout with a fullscreen Eshell for the current tab." (interactive) (let ((tab-name (my-current-tab-name))) (puthash tab-name (current-window-configuration) my-saved-tab-configurations) (delete-other-windows) (eshell))) (defun my-restore-window-configuration () "Restore the previously saved window configuration for the current tab." (interactive) (let* ((tab-name (my-current-tab-name)) (config (gethash tab-name my-saved-tab-configurations))) (if config (set-window-configuration config) (message "No saved window configuration for this tab.")))) #+end_src