Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Running Shells in Emacs: An Overview (masteringemacs.org)
79 points by twampss on Nov 10, 2010 | hide | past | favorite | 24 comments


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


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


And if e-shell isn't your thing, may I humbly suggest this:

http://www.shellarchive.co.uk/content/emacs_tips.html#have-e...

I alias that script to 'e' so I can do this:

    $ ifconfig | e
    $ e README.txt


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)


    ;; 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.


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


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


"default-directory" is already buffer local.


Yes. Very misleading comment.


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...


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.


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

(Thanks for the link)


wow it's fast!


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


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.


+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.


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?


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.


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.


  git <TAB>
completes subcommands

  ls <TAB>
with the alias shows files from the root /

GNU Emacs 23.1.1 (Linux)


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.


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


git <TAB> completes filenames, but so does bash in terminal. I don't have any fancy bash completion dingleberries installed. (And yes, my beard is grey too.)

The alias completion does appear broken, as you demonstrated.


What you're seeing is not bash tab completion based on compgen, but shell-mode's implementation of tab completion. Which is why git <TAB>, for example, does not do what you'd like. It seems like it should be possible to write an emacs tab completer that actually calls bash's compgen, but I'm not aware of an implementation of this.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: