4

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?

2
  • Are you typing your grep command line directly into the eshell buffer at the command prompt; or, are you instead executing a function that launches the buit-in grep search using the compliation-mode/grep-mode generated buffer?
    – lawlist
    Apr 10, 2020 at 18:16
  • I am entering the grep command directly into the command prompt of the eshell buffer. Most other commands entered into the eshell command prompt like this just output onto the eshell buffer as expected and do not open new windows like grep does. Apr 10, 2020 at 19:28

2 Answers 2

4

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 grep command:

~ $ 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’.
~ $
2
  • 1
    Thanks, also FYI for anyone wanting to reset it back to using the built-in Lisp grep function, you can do this by entering alias grep into the eshell command prompt as explained here Apr 10, 2020 at 21:26
  • 1
    Thanks! I incorporated this information into the answer as well.
    – NickD
    Apr 11, 2020 at 15:00
0

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.

  1. eshell/grep isn't behaving to our liking. No biggie.
  2. C-h f eshell/grep - first line gives: eshell/grep is a compiled Lisp function in ‘em-unix.el’.
  3. click on em-unix.el and read the one line of code.
  4. eshell/grep just calls eshell-grep.
  5. C-h f eshell-grep - first line: eshell-grep is a compiled Lisp function in ‘em-unix.el’.
  6. 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.
  7. third line has an interesting symbol reference: (if (or eshell-plain-grep-behavior
  8. 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.
  9. "normally" still doesn't tell us what "normally" is exactly so we test it.
  10. M-S : (setq eshell-plain-grep-behavior t)
  11. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.