Search code examples
bashemacseshell

How can I include return codes and the command number in my eshell prompt?


I often have need of a shell while using Emacs. Recently, I have been trying to switch over from shell to eshell, so that I will be able to use the same commands regardless of platform.

One of the first things I would like to do is customize my prompt to match my bash prompt. To do this, I am customizing eshell-prompt-function. The only thing I am still missing is the current command count and the last return code. I can do this in bash by setting PS1 to e.g. \! and $? respectively. I have already tried (eshell/echo "$?") for the latter, but it doesn't work (though it works if I execute the command manually in eshell).

Edit:
An example of what part of my current bash prompt looks like is [~][501:0], where 501 is the current command number (so if I type a command and hit Enter it will show 502), and the 0 is the return code.


Solution

  • This puts the return code into the eshell prompt:

    (setq eshell-prompt-function
          (lambda ()
            (format "[%s][%s] "
                    (abbreviate-file-name (eshell/pwd))
                    eshell-last-command-status)))
    

    I couldn't find any simple way to put the latest command number into the prompt—and it might be less than useful, since eshell seems to use a ring for command history, so at some point the counter would be stuck at 128, and all the previous prompts would be inaccurate.

    Note that you should also update eshell-prompt-regexp to match anything that eshell-prompt-function could come up with.