1

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!

class_OpenGL
  • 185
  • 1
  • 10
  • For those forum participants who do not already know how to solve the problem, it may be helpful to enable broader debugging such as by evaluating `(setq debug-on-error t)` and then updating the question to reflect what command was not found and what variable is invalid. – lawlist Oct 31 '17 at 00:08
  • Did you follow the instructions of `C-h v eshell-prompt-function` ? – phils Oct 31 '17 at 05:43
  • It seems that there is not instructions to follow (as you tell us) – class_OpenGL Oct 31 '17 at 07:33
  • I've added the text as an answer, but I note that at minimum it appears in Emacs 23.4 and above (that being the earliest release I have on this machine). – phils Oct 31 '17 at 11:04

1 Answers1

1

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.

phils
  • 71,335
  • 11
  • 153
  • 198
  • I'm trying to build a regexp expresion, but I don't know how to represent character [ I try to put \\[, but it says: `Invalid regexp: "Unmatched [ or [^"` – class_OpenGL Oct 31 '17 at 23:29
  • I didn't know about regexp, but I have created this: `(setq eshell-prompt-regexp ".[[:alnum:]]+@[[:alnum:]]+[[:blank:]][[:print:]]+..[[:blank:]].*")`. With this regexp, I can press UP key without any problem, but autocomplete doesn't work fine. I don't know what uses emacs to autocomplete now. For example, if I try to autocomplete when I wrote `cd Progr`, Emacs, says: `Possible completions are: . 2to3 2to3-2.7 [...]` – class_OpenGL Nov 01 '17 at 00:02
  • I would guess that ending your regexp with `.*` is a mistake which causes it to treat the entire line (not just the prompt) as if it were the prompt, which then means that it is attempting to complete an empty string, and is suggesting anything and everything? – phils Nov 01 '17 at 00:07
  • As I said, I didn't know something about regexp. I'm learning. What do you suggest? Thanks! – class_OpenGL Nov 01 '17 at 00:11
  • And you match `[` and `]` with `"\\["` and `"\\]"` respectively. When a regexp is written as a double-quoted string in elisp, you need to double the backslashes for the regexp syntax, because backslashes are also special to the double-quoted read syntax for strings. – phils Nov 01 '17 at 00:11
  • Ahh Thats true!! Updated – class_OpenGL Nov 01 '17 at 00:13
  • *Maybe* something like`"\\[.*?@.*? [^/]*\\]\\$ "` ? Untested; might not work. – phils Nov 01 '17 at 00:15
  • Ohh I love you!! Could you explaing this regexp in an answer? Thank you so much!!!! – class_OpenGL Nov 01 '17 at 00:16
  • I'll update the answer later, but `*?` is the non-greedy version of `*` so `.*?` means as few `.` matches as possible (with `.` matching anything except a newline). `[^/]*` matches as many characters which are not `/` as possible. – phils Nov 01 '17 at 00:19
  • Ah I understand. Thank you! – class_OpenGL Nov 01 '17 at 00:21