38.3 Shell Mode
The major mode for Shell buffers is Shell mode. Many of its special
commands are bound to the C-c
prefix, and resemble the usual
editing and job control characters present in ordinary shells, except
that you must type C-c
first. Here is a list of Shell mode
commands:
RET
¶
Send the current line as input to the subshell
( comint-send-input
). Any shell prompt at the beginning of the
line is omitted (see Shell Prompts). If point is at the end of
buffer, this is like submitting the command line in an ordinary
interactive shell. However, you can also invoke RET
elsewhere
in the shell buffer to submit the current line as input.
TAB
¶
Complete the command name or file name before point in the shell
buffer ( completion-at-point
). This uses the usual Emacs
completion rules (see Completion), with the completion
alternatives being file names, environment variable names, the shell
command history, and history references (see Shell History References).
For options controlling the completion, see Shell Mode Options.
M-?
¶
Display temporarily a list of the possible completions of the file
name before point ( comint-dynamic-list-filename-completions
).
C-d
¶
Either delete a character or send EOF
( comint-delchar-or-maybe-eof
). Typed at the end of the shell
buffer, this sends EOF to the subshell. Typed at any other
position in the buffer, this deletes a character as usual.
C-c C-a
¶
Move to the beginning of the line, but after the prompt if any
( comint-bol-or-process-mark
). If you repeat this command twice
in a row, the second time it moves back to the process mark, which is
the beginning of the input that you have not yet sent to the subshell.
(Normally that is the same place—the end of the prompt on this
line—but after C-c SPC
the process mark may be in a
previous line.)
C-c SPC
Accumulate multiple lines of input, then send them together
( comint-accumulate
). This command inserts a newline before
point, but does not send the preceding text as input to the
subshell—at least, not yet. Both lines, the one before this newline
and the one after, will be sent together (along with the newline that
separates them), when you type RET
.
C-c C-u
¶
Kill all text pending at end of buffer to be sent as input
( comint-kill-input
). If point is not at end of buffer,
this only kills the part of this text that precedes point.
C-c C-w
¶
Kill a word before point ( backward-kill-word
).
C-c C-c
¶
Interrupt the shell or its current subjob if any
( comint-interrupt-subjob
). This command also kills
any shell input pending in the shell buffer and not yet sent.
C-c C-z
¶
Stop the shell or its current subjob if any ( comint-stop-subjob
).
This command also kills any shell input pending in the shell buffer and
not yet sent.
C-c C-\
¶
Send quit signal to the shell or its current subjob if any
( comint-quit-subjob
). This command also kills any shell input
pending in the shell buffer and not yet sent.
C-c C-o
¶
Delete the last batch of output from a shell command
( comint-delete-output
). This is useful if a shell command spews
out lots of output that just gets in the way. With a prefix argument,
this command saves the deleted text in the kill-ring
(see The Kill Ring), so that you could later yank it (see Yanking)
elsewhere.
C-c C-s
¶
Write the last batch of output from a shell command to a file
( comint-write-output
). With a prefix argument, the file is
appended to instead. Any prompt at the end of the output is not
written.
C-c C-r
¶C-M-l
Scroll to display the beginning of the last batch of output at the top
of the window; also move the cursor there ( comint-show-output
).
C-c C-e
¶
Scroll to put the last line of the buffer at the bottom of the window
( comint-show-maximum-output
).
C-c C-f
¶
Move forward across one shell command, but not beyond the current line
( shell-forward-command
). The variable shell-command-regexp
specifies how to recognize the end of a command.
C-c C-b
¶
Move backward across one shell command, but not beyond the current line
( shell-backward-command
).
M-x dirs
Ask the shell for its working directory, and update the Shell buffer’s default directory. See Directory Tracking.
M-x comint-send-invisible RET text RET
¶
Send text as input to the shell, after reading it without echoing. This is useful when a shell command runs a program that asks for a password.
Please note that Emacs will not echo passwords by default. If you really want them to be echoed, evaluate (see Evaluating Emacs Lisp Expressions) the following Lisp expression:
(remove-hook 'comint-output-filter-functions
'comint-watch-for-password-prompt)
M-x comint-continue-subjob
¶
Continue the shell process. This is useful if you accidentally suspend the shell process. 20
M-x comint-strip-ctrl-m
¶
Discard all control-M characters from the current group of shell output. The most convenient way to use this command is to make it run automatically when you get output from the subshell. To do that, evaluate this Lisp expression:
(add-hook 'comint-output-filter-functions
'comint-strip-ctrl-m)
M-x comint-truncate-buffer
¶
This command truncates the shell buffer to a certain maximum number of
lines, specified by the variable comint-buffer-maximum-size
.
Here’s how to do this automatically each time you get output from the
subshell:
(add-hook 'comint-output-filter-functions
'comint-truncate-buffer)
By default, Shell mode handles common ANSI escape codes (for instance, for changing the color of text). Emacs also optionally supports some extend escape codes, like some of the OSC (Operating System Codes) if you put the following in your init file:
(add-hook 'comint-output-filter-functions 'comint-osc-process-output)
With this enabled, the output from, for instance, ls --hyperlink
will be made into clickable buttons in the Shell mode
buffer.
Shell mode is a derivative of Comint mode, a general-purpose mode for communicating with interactive subprocesses. Most of the features of Shell mode actually come from Comint mode, as you can see from the command names listed above. The special features of Shell mode include the directory tracking feature, and a few user commands.
Other Emacs features that use variants of Comint mode include GUD
(see Running Debuggers Under Emacs) and M-x run-lisp
(see Running an External Lisp).
You can use M-x comint-run
to execute any program of your choice
in a subprocess using unmodified Comint mode—without the
specializations of Shell mode. To pass arguments to the program, use
C-u M-x comint-run
.
Footnotes
(20)
You should not suspend the shell process. Suspending a subjob of the shell is a completely different matter—that is normal practice, but you must use the shell to continue the subjob; this command won’t do it.