When entering the grep command in an eshell in spacemacs, a separate window and buffer are created to list the grep output results. However, I would prefer for the output to be listed in the eshell buffer like how a stand-alone terminal application would normally behave. How can this be achieved?
eshell has several built-in commands (
grep among them) and it prefers to use the built-in version:
~ $ which grep eshell/grep is a compiled Lisp function in ‘em-unix.el’.
As the manual that I linked to above says:
If you want to discard a given built-in command, you could declare an alias (see Aliases). Example: ~ $ which sudo eshell/sudo is a compiled Lisp function in `em-tramp.el'. ~ $ alias sudo '*sudo $*' ~ $ which sudo sudo is an alias, defined as "*sudo $*"
So you could do the following to get the external
~ $ alias grep '*grep $*' ~ $ which grep grep is an alias, defined as "*grep $*" ~ $ grep foo * *scratch*~: "foo") *scratch*~:(cancel-timer[nil 23357 43792 121024 30 message ("foo") nil 748000]) /usr/bin/grep: Audiobooks/: Is a directory /usr/bin/grep: Desktop/: Is a directory ...
And as @TPog (the OP) points out in a comment, you can go back to the built-in meaning of grep, by removing the alias:
~ $ alias grep ~ $ which grep eshell/grep is a compiled Lisp function in ‘em-unix.el’. ~ $
While @NickD's answer is a decent hack, it's not really the Emacs way of doing things. Emacs is ridiculously discoverable, meaning, questions of the type 'How do I change this or that behavior or why is this or that happening' are really easy to answer compared to most other systems. In this concrete case, the Emacs way is actually quite simple.
- eshell/grep isn't behaving to our liking. No biggie.
C-h f eshell/grep- first line gives: eshell/grep is a compiled Lisp function in ‘em-unix.el’.
- click on em-unix.el and read the one line of code.
- eshell/grep just calls eshell-grep.
C-h f eshell-grep- first line: eshell-grep is a compiled Lisp function in ‘em-unix.el’.
- click on em-unix.el and read the function that does the actual work with the goal of just trying to think intelligently about what you're reading.
- third line has an interesting symbol reference:
(if (or eshell-plain-grep-behavior
C-h v eshell-plain-grep-behavior- we get: If non-nil, standalone "grep" commands will behave normally. Standalone in this context means not redirected, and not on the receiving side of a command pipeline.
- "normally" still doesn't tell us what "normally" is exactly so we test it.
M-S : (setq eshell-plain-grep-behavior t)
- open eshell and do a grep
Turns out normally means do not open a separate buffer and just print the results of grepping in the same eshell buffer, which is exactly what standard grep utilities do.
Put (setq eshell-plain-grep-behavior t) in your init and you're good.
90% of the time it should take you no more than 5 minutes to figure out why Emacs is or is not doing something. Yes, Emacs is that good.