notes from /dev/null

by Charles Choi 최민수


Getting Eshell nth History to Work

26 Jan 2026  Charles Choi

As much as I enjoy using Eshell, it is not without its annoyances. In this case, I’m writing about the common shell convention of using !n to recall a previously entered command.

In Eshell, entering the history command will show you all of your previously entered commands alongside a number n.

The problem is in attempting to use !n to recall a specific command in Eshell as one would do in bash or zsh. By default it will not work as described in Info (eshell) History without configuring eshell-expand-input-functions first. (My current configuration is GNU Emacs 30.2.)

To get !n to work requires adding the function eshell-expand-history-references to the customizable variable eshell-expand-input-functions.

The following configuration Elisp will achieve this.

1
2
(add-to-list 'eshell-expand-input-functions
 #'eshell-expand-history-references)

A nice feature of eshell-expand-history-references is that it will expand using the content of an actual command instead of a number reference. So !foo will invoke the last command that you entered the string “foo” in.

It’s not clear to me whether coupling nth history support to eshell-expand-history-references is intentional or a bug. If the former, then I’d consider this yet another example of staying “on brand” with Emacs’ tendency to ship with questionable defaults. That shade aside, Eshell is still an amazing package that I could not imagine using Emacs without.

Links

emacs

 

AboutMastodonBlueskyGitHub

Feeds & Tags
Get Scrim for macOSGet Captee for macOS

Powered by Pelican