1

I feel like there is a better way of writing this with use-package but I just can't get the usual way to work.

(use-package eshell
  :hook
  (eshell-mode
   . (lambda ()
       (define-key
         eshell-mode-map
         (kbd "C-<backspace>")
         (lambda () ; clear shell
           (interactive)
           (message "I am some other function"))))))

I tried using the :bind (:map eshell-mode-map ("C-<backspace>" . <lambda here>)), but that just binds the function to the global key map for some reason. Maybe because eshell-mode-map is nil till eshell is launched. Could someone suggest a better utilisation of use-package to write what I already have?

1

The keybindings created with :bind expect a function name (not sure how lambdas are expanded) so you can define a named function in the :init clause of use-package which should work as expected...

(use-package eshell
  :after esh-mode
  :init 
  (defun my-clear-shell ()
    (interactive)
    (message "I am some other function"))
  :bind (:map 
     eshell-mode-map
     ("C-<backspace>" . my-clear-shell)))
| improve this answer | |
  • The issue with this is that I get a Symbol’s value as variable is void: eshell-mode-map. You can solve that by adding a :after esh-mode but then the keybinding is available everywhere and not just in eshell-mode. – scribe Oct 14 at 5:49
  • I get the same error without the :after esh-mode line (and added to the answer) but keybindings work as expected. – zzkt Oct 14 at 10:32
  • ...in eshell <C-backspace> runs the command my-clear-shell (found in eshell-mode-map), which is an interactive compiled Lisp function in ‘~/.emacs’. – zzkt Oct 14 at 10:33
  • ...and elsewhere <C-backspace> runs the command backward-kill-word (found in global-map), which is an interactive native compiled Lisp function in ‘simple.el’. – zzkt Oct 14 at 10:33
0

Rewrite your config to keep it simple:

(defun your-eshell-clear ()
  (interactive)
  (message "TODO impl your-eshell-clear"))

(defun your-eshell-setup ()
  (define-key eshell-mode-map (kbd "C-<backspace>") #'your-eshell-clear))

(add-hook 'eshell-mode-hook #'your-eshell-setup)

You can simplly put it to your init file, it will not slow your Emacs startup. If you need, you can group it via progn or with-eval-after-load or use-package, thought they does not provide actual benefits, and use-package sometimes can make your init file more complex and harder to understand.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.