riscy / shx-for-emacs

An Emacs shell-mode (and comint-mode) extension that enables displaying small plots and graphics and lets users write shell commands in Emacs Lisp.
GNU General Public License v3.0
218 stars 10 forks source link

Every input line is treated as command in history #19

Closed leliamesteban closed 3 years ago

leliamesteban commented 4 years ago

When prompted by a command line program, the response ends up as a command in the command history of shx. For example, when I run sudo pacman -S file and type y to confirm the installation and then click the up button on my keyboard after the command is finished, y appears as a command. This is true for any prompt or anything that is interpreted by the command line app I've run.

Is there a reason for this? I'm wondering if it's on purpose or if it's an unintended effect.

Thank you

riscy commented 4 years ago

Hi! This is actually a feature of comint-mode REPLs (so you also see it in ielm, or inferior-python-mode, shell-mode on which M-x shx is based, etc.) and is sort of baked in. We really only notice it happening in command-line shells though, since command-line shells are often used to jump into and out of other processes.

The reason is that comint-mode supports only a "dumb" terminal. Everything comint-mode knows about the command history doesn't come from bash itself (or whichever shell you use) and the processes it spawns, but is instead recorded by Emacs in one big uninterrupted list.

So that's sort of the reason for it (it's sort of baked into Emacs). As for the effect, I really have grown to view it as a convenient feature. I find it super useful to be able to recover from mistakes like:

$ print(987.014/33)  # oops
bash: syntax error near unexpected token `987.014/33'
$ python
Python 3.7.4 (default, Aug 13 2019, 15:17:50) 
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print(987.014/33)  # I don't have to retype this, I just rerun a previous command
29.90951515151515

or

$ docker run -it <wrong-image> bash
$ cd xyz/abc/123 && bash my_script.sh  # oops, I realized this was the wrong image
...
$ exit
$ docker run -it --rm <correct-image> bash
$ cd xyz/abc/123 && bash_my_script.sh  # easy to recover!