Emacs Advent Calendar 14: Shell Interaction

Mon 2015-12-14

While Emacs is an operating system on its own, sometimes it is necessary to interact with the outside world. For this purpose you can run single shell commands from Emacs or start an interactive shell session.

There are three ways to run a single shell command. The first, M-!, runs synchronously, i.e. Emacs waits for the command to finish before doing anything else. Output from the shell command is shown in the echo are or, if too long, in a dedicated buffer. When prefixed with a numerical argument (e.g. M-1 M-!) output is inserted into the current buffer.

To, for example, insert speaking animals into the current buffer, run C-u M-! cowsay -f sheep "Moo" <RET> and get this as a result:

_____
< Moo >
 -----
  \
   \
       __
      UooU\.'@@@@@@`.
      \__/(@@@@@@@@@@)
           (@@@@@@@@)
           `YY~~~~YY'
            ||    ||

To run a shell command asynchronously (and be able to use Emacs for other things in the meanwhile), use M-& or append a & to the command when run with M-!.

If you want to feed part of a buffer's content to a command on stdin, set the region and run the command with M-|. With a numeric argument the old region is deleted and replaced with the output of the shell command.

When single shell commands are not enough, you can run an interactive subshell. For that, emacs offers M-x shell and M-x term; the latter featuring full terminal emulation.

Shell mode has quite a number of keybindings a selection of which is explained below. Refer to the manual for a full list.

Key Action
<RET> send current line to subshell (works on any line)
<TAB> complete command name or file name
C-c <SPC> insert newline but don't send line to subshell (yet)
C-c C-c interrupt the shell
C-c C-o delete last batch of output

The advantage of shell mode is that all the usual Emacs editing commands can be used. Nontheless it is not a full shell, so some commands (e.g. pagers, git log, other editors) do not work.

Shell mode performs directory tracking. If your shell allows changing directories by specifying the directory name without cd (like zhs does), this does not work reliably. For these cases M-x dirs can be used to update the working directory.

To enter a password without echoing it in the buffer, use M-x send-invisible.

Earlier commands can be repeated from the shell history ring. They are accessed by keybindings similar to the Minibuffer history ring (M-p, M-n, M-r, M-s). To execute some earlier commands in sequence, navigate to the first one, execute it and use C-c C-x to execute each following command. To display a listing of the command history in a separate buffer, use C-c C-l.

In cases where Shell mode does not work to run a program, Term mode can be used (invoked with M-x term). This is a full-fledged terminal, even Emacs itself can be run there (it's Emacs all the way down).

Term mode has two input modes. When in line mode, it acts very like Shell mode. In char mode, each character is directly sent to the subshell, except for C-c. To switch to line mode press C-c C-j, to switch to char mode press C-c C-k. In char mode, the C-c prefix is equivalent to the C-x prefix in normal Emacs operation.

Full documentation on shell invocation from inside Emacs can be found in chapter 37 (Running Shell Commands from Emacs) of the Emacs Reference Manual.

Tags:

This text by Ludger Sandig is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.