Go to the first, previous, next, last section, table of contents.

Interactive Inferior Shell

To run a subshell interactively, putting its typescript in an Emacs buffer, use M-x shell. This creates (or reuses) a buffer named `*shell*' and runs a subshell with input coming from and output going to that buffer. That is to say, any "terminal output" from the subshell goes into the buffer, advancing point, and any "terminal input" for the subshell comes from text in the buffer. To give input to the subshell, go to the end of the buffer and type the input, terminated by RET.

Emacs does not wait for the subshell to do anything. You can switch windows or buffers and edit them while the shell is waiting, or while it is running a command. Output from the subshell waits until Emacs has time to process it; this happens whenever Emacs is waiting for keyboard input or for time to elapse.

To make multiple subshells, rename the buffer `*shell*' to something different using M-x rename-uniquely. Then type M-x shell again to create a new buffer `*shell*' with its own subshell. If you rename this buffer as well, you can create a third one, and so on. All the subshells run independently and in parallel.

The file name used to load the subshell is the value of the variable explicit-shell-file-name, if that is non-nil. Otherwise, the environment variable ESHELL is used, or the environment variable SHELL if there is no ESHELL. If the file name specified is relative, the directories in the list exec-path are searched; this list is initialized based on the environment variable PATH when Emacs is started. Your `.emacs' file can override either or both of these default initializations.

To specify a coding system for the shell, you can use the command C-x RET c immediately before M-x shell. You can also specify a coding system after starting the shell by using C-x RET p in the shell buffer. See section Specifying a Coding System.

As soon as the subshell is started, it is sent as input the contents of the file `~/.emacs_shellname', if that file exists, where shellname is the name of the file that the shell was loaded from. For example, if you use bash, the file sent to it is `~/.emacs_bash'.

cd, pushd and popd commands given to the inferior shell are watched by Emacs so it can keep the `*shell*' buffer's default directory the same as the shell's working directory. These commands are recognized syntactically by examining lines of input that are sent. If you use aliases for these commands, you can tell Emacs to recognize them also. For example, if the value of the variable shell-pushd-regexp matches the beginning of a shell command line, that line is regarded as a pushd command. Change this variable when you add aliases for `pushd'. Likewise, shell-popd-regexp and shell-cd-regexp are used to recognize commands with the meaning of `popd' and `cd'. These commands are recognized only at the beginning of a shell command line.

If Emacs gets an error while trying to handle what it believes is a `cd', `pushd' or `popd' command, it runs the hook shell-set-directory-error-hook (see section Hooks).

If Emacs does not properly track changes in the current directory of the subshell, use the command M-x dirs to ask the shell what its current directory is. This command works for shells that support the most common command syntax; it may not work for unusual shells.

You can also use M-x dirtrack-mode to enable (or disable) an alternative and more aggressive method of tracking changes in the current directory.

Emacs defines the environment variable EMACS in the subshell, with value t. A shell script can check this variable to determine whether it has been run from an Emacs subshell.

Go to the first, previous, next, last section, table of contents.