
Running Shells in Emacs: An Overview - twampss
http://www.masteringemacs.org/articles/2010/11/01/running-shells-in-emacs-overview/
======
technomancy
Little-known fact about Eshell: not only can you implement your prompt
functions and highlighting in lisp, you can also (a) pipe output directly to
buffers and (b) invoke M-x commands from the shell:

a) $ ifconfig > #<buffer interfaces>

b) find-file README.txt # same as doing C-x C-f

~~~
jfb
Awesome. There are many fact that remain little known in eshell, it seems.

------
e40
Based on comint, M-x su:

    
    
      (defvar explicit-su-file-name "/bin/su")
      (defvar explicit-su-args '("-"))
    
      (defun su (&optional buffer)
        (interactive
         (list
          (and current-prefix-arg
    	   (prog1
    	       (read-buffer "SU buffer: "
    			    (generate-new-buffer-name "*su*"))
    	     (if (file-remote-p default-directory)
    		 ;; It must be possible to declare a local default-directory.
    		 (setq default-directory
    		       (expand-file-name
    			(read-file-name
    			 "Default directory: " default-directory default-directory
    			 t nil 'file-directory-p))))))))
        (setq buffer (get-buffer-create (or buffer "*su*")))
        ;; Pop to buffer, so that the buffer's window will be correctly set
        ;; when we call comint (so that comint sets the COLUMNS env var properly).
        (pop-to-buffer buffer)
        (unless (comint-check-proc buffer)
          (let* ((prog explicit-su-file-name)
    	     (name (file-name-nondirectory prog))
    	     (startfile (concat "~/.emacs_" name))
    	     (xargs-name (intern-soft (concat "explicit-" name "-args"))))
    	(apply 'make-comint-in-buffer "su" buffer prog
    	       (if (file-exists-p startfile) startfile)
    	       (if (and xargs-name (boundp xargs-name))
    		   (symbol-value xargs-name)
    		 '("-i")))
    	(shell-mode)))
        buffer)

~~~
jrockway

        ;; It must be possible to declare a local default-directory.
    

make-local-variable is an interactive built-in function in `C source code'.

    
    
        (make-local-variable VARIABLE)
    

Make VARIABLE have a separate value in the current buffer. Other buffers will
continue to share a common default value. (The buffer-local value of VARIABLE
starts out as the same value VARIABLE previously had. If VARIABLE was void, it
remains void.) Return VARIABLE.

~~~
e40
That comment came from the original, M-x shell, btw.

~~~
jrockway
That needs to be fixed. I've asked emacs-devel.

------
rayvega
If you are on Windows, another alternative is running PowerShell as an
interactive shell within Emacs:

[http://blogs.msdn.com/b/dotnetinterop/archive/2008/04/10/run...](http://blogs.msdn.com/b/dotnetinterop/archive/2008/04/10/run-
powershell-as-a-shell-within-emacs.aspx)

------
jfb
I know I should be using eshell, particularly now that daemon mode is "stable"
on OS X, but there is a lot of unlearning involved when grep pops up a result
buffer. Why, yes, as a matter of fact, my beard _is_ grey.

------
chrismealy
How did I not know about M-x ansi-term all these years? Argh!

(Thanks for the link)

~~~
revorad
wow it's fast!

~~~
jedbrown
Well, sort of.

urxvt:

    
    
      $ time ls /usr/bin
      0.036 real   0.013 user   0.020 sys   93.76 cpu
    

emacs ansi-term:

    
    
      4.265 real   0.023 user   0.023 sys   1.09 cpu

~~~
leif
Not much beats urxvt, but even gnome-terminal beats ansi-term.

urxvt: real 0m0.314s user 0m0.080s sys 0m0.040s

gnome-terminal: real 0m1.228s user 0m0.090s sys 0m0.040s

ansi-term: real 0m6.493s user 0m0.150s sys 0m0.030s

Also, ls --color=auto really messes up ansi-term. It glitches all over the
place and I think treats some color codes as carriage returns or other motion
commands for some reason.

------
ivanstojic
+1 definitely! Thank you for a great and refreshing info on shells! I've been
using eterm only since I started using Emacs about three years ago, and didn't
even know about ansi-term and friends.

------
jedbrown
Does anyone here use M-x term? Is it stable?

I've been put off by M-x shell because it only offers "line" interaction, so
bash tab completion doesn't work. M-x term looks much better, but "ls
/usr/bin" stalls the output part way through with my cursor off in the middle
of the screen until I type a character in char mode, and I see stuff like

    
    
      error in process filter: cd-absolute: /hom/: no such directory
      error in process filter: /hom/: no such directory
    

in messages. Is this sort of thing endemic?

~~~
KC8ZKF
Bash tab completion works for me in shell. GNU Emacs 23.2.1, x86_64-apple-
darwin, on OS X 10.6.4.

Term works fine as well.

~~~
jedbrown
M-x shell, then type

    
    
      git <TAB>
    

Is it completing subcommands, or is it trying to complete file names (I always
observe the latter)? Now type

    
    
      alias foo='cd /'
      foo
      ls <TAB>
    

See how it tries to complete paths in ~ (or wherever your last path was
instead of in the new directory).

GNU Emacs 24.0.50.1 (Linux), and every version of Emacs I've used in the last
10 years.

~~~
d0mine

      git <TAB>
    

completes subcommands

    
    
      ls <TAB>
    

with the alias shows files from the root /

GNU Emacs 23.1.1 (Linux)

~~~
jedbrown
Are you using shell-mode or something else (e.g. term-mode)? Completion works
fine in term-mode (in term-char-mode) because it actually uses bash's
completion mechanism. In contrast, shell-mode only does line-based interaction
with the shell, and it generates its own completions. It recognizes commands
like cd, pushd, and popd to maintain its own mirror of the current path. As
far as I know, there is no shell-mode implementation that actually calls the
shell for completions.

~~~
d0mine
multi-term (term-char-mode/term-line-mode)

