3

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.

Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
  • 1
    [This](http://stackoverflow.com/a/12098095/3076724) may be helpful, though I don't use emacs, so a bit confused with what you're trying to do. Could you include an example of how you want your `PS1` to appear? – Reinstate Monica Please Jun 23 '14 at 00:31
  • Edited to add an example. I will take a look at the link in detail a bit later when I get the chance, though at first glance it appears to apply mainly to `bash`, so I'm not sure it will help me in my switch to `eshell`. – Scott Weldon Jun 23 '14 at 00:54

1 Answers1

4

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.

legoscia
  • 39,593
  • 22
  • 116
  • 167
  • 1
    For the count: Define a variable my-eshell-command-count and a function that increments it. Add that hook to the eshell-after-prompt-hook list and implement it in your prompt function. Here's my prompt: http://i.imgur.com/VSiFDDX.png – Jordon Biondo Jun 23 '14 at 13:06
  • 1
    Awesome, that works, thanks! @JordonBiondo, do you have elisp that creates a command counter like that? If not, I may try to implement it myself at some point, and when I do will post back here. – Scott Weldon Jun 23 '14 at 15:57