Search code examples
emacseshell

Some functions of emacs shell disappears when I put this code in my init file


I tried to put this code in my Emacs init file:

(setq eshell-prompt-function (lambda () 
    (concat "[" 
        (user-real-login-name) 
        "@" 
        (system-name) 
        " " 
        (car (last (split-string (eshell/pwd) "/"))) 
        "]$ ")))

It works nicely, but some errors appears:

  • When I push UP key (to get last command executed), I get this message in minibuffer: Not found
  • When I push TAB key (to autocomplete a path), I get this message in minibuffer: Invalid variable reference

I don't understand why this is happening. Could someone help me? Thanks!


Update

I entered debugger mode. When I press UP key (only UP key, not tab), debugger throws this error:

Debugger entered--Lisp error: (error "Not found")
  signal(error ("Not found"))
  error("Not found")
  eshell-previous-matching-input("^\\[carlos@archlinux ~]\\$ " 1)
  eshell-previous-matching-input-from-input(1)
  funcall-interactively(eshell-previous-matching-input-from-input 1)
  call-interactively(eshell-previous-matching-input-from-input nil nil)
  command-execute(eshell-previous-matching-input-from-input)

Update 2

I set eshell-prompt-regexp to a regexp that matches my setup:

(setq eshell-prompt-regexp "\\[[[:alnum:]]+@[[:alnum:]]+[[:blank:]][[:print:]]+\\]\\$[[:blank:]].*")

Now, I can press UP and it works fine, but when I press TAB key to autocomplete a PATH, Emacs try to autocomplete with other 'thing'. For example, currently, I'm in a path that contains Programming directory. If I type cd Progr and I press TAB key, Emacs try to autocomplete showing this message:

Click on a completion to select it. In this buffer, type RET to select the completion near point.

Possible completions are:

. 2to3

2to3-2.7 2to3-3.6

ControlPanel KateDJ

Magick++-config Magick-config

MagickCore-config MagickWand-config

NetworkManager Wand-config

WebKitWebDriver X

Xorg Xwayland

[ a2ping

and more. Why isn't emacs completing using directory? Lots of thanks to all comments and answers!


Solution

  • C-hv eshell-prompt-function states:

    Make sure to update eshell-prompt-regexp so that it will match your prompt.

    I believe that setting that regexp appropriately will resolve your issues.

    You could doubtless come up with something even more specific than this, but the following regexp matches the prompt you're generating:

    "^\\[.*?@.*? [^/]*?\\]\\$ "
    

    This regexp is matching (at the beginning of a line) [x@y z]$ where x and y are sequences of non-newline characters (. does not match newlines, which isn't a problem here), and z is a sequence of non-/ characters (on account of the split-string call in your prompt function splitting on /).

    *? is the non-greedy version of *, matching as few instances of the preceding pattern as possible/necessary; thus ensuring that we can't inadvertantly match text beyond the end of the prompt.