Emacs: Run Shell in Emacs

By Xah Lee. Date: . Last updated: .

This page is a tutorial on running OS shell inside Emacs. On Linux, this is usually Bash. On Windows, this is cmd.exe, or can be PowerShell.

emacs shell 2014-08-22
Bash shell in emacs. M-x shell.

Running Shell in Emacs

How to run a shell inside emacs?

Call shell.

When in shell, to run the previous shell command, press 【Ctrl+】.

How to run a second shell inside emacs?

Call universal-argumentCtrl+u】 first before you call shell.

To always start a new shell. Put the following in your emacs init.

(defadvice shell (around always-new-shell)
  "Always start a new shell."
  ;; 2012-07-20 by Ellen Taylor, https://plus.google.com/b/113859563190964307534/118418624395898188790/posts
  (let ((buffer (generate-new-buffer-name "*shell*"))) ad-do-it))

(ad-activate 'shell)

Can i run ssh inside emacs?

Call term (this will create a new term for each command call). Then, you can run {ssh, top, vi, …}. You can even start another emacs.

To exit term, press 【Ctrl+d】.

When in term-mode, call describe-modeCtrl+h m】 to see full list of its keys.

〔➤see Emacs: What's the difference between {shell, term, ansi-term, eshell}?

In practice, it's probably better to run these processes in a terminal. Because, once inside a terminal emulator inside emacs, normal emacs keys won't work because you told emacs to pretend it is a terminal emulator. This makes the user interface complicated.

What's the advantage of running shell inside emacs?

Emacs: Inferior Shell vs Terminal: What's the advantage of running shell inside emacs?

What's the difference between {shell, term, ansi-term, eshell}?

Emacs: What's the difference between {shell, term, ansi-term, eshell}?

Calling a Single Shell Command

How to call a shell command?

Call shell-commandAlt+!】 to run just 1 shell command.

How to send current text selection to a shell command?

Select a region, then call shell-command-on-regionAlt+|】. For example, select a region, then type 【Alt+| wc -l Enter ↵】. This will print line count of region.

You can have the result replace the selected region. To do that, press 【Ctrl+u】 before the 【Alt+|】.

(The key 【Ctrl+u】 calls universal-argument. It is a interactive way to give arguments to commands. ➢ for example: 【Ctrl+u 30 -】 will insert dash 30 times.)

Shell on Microsoft Windows

On Windows, by default, when you call shell, it starts cmd.exe.

How to have unix shell commands on Windows?

Call eshell.

eshell is similar in purpose to unix's bash, but written entirely in emacs lisp. It supports most unix commands such as {cd, ls, cp, mkdir, …}. 〔➤see Emacs: What's eshell? eshell vs Shell Difference?

My shell doesn't work in Windows?

Most problems are probably caused by incorrect environment variable setup. See: Emacs and Microsoft Windows Tips.

Emacs: View “man” Page

Emacs: View “man” Page

Directory Navigation and Managing Files

For simple shell commands such as {ls, cd, cp, rm, mkdir, …}, it's better to do with dired. 〔➤see Emacs: File Management (dired tutorial)


While running emacs in a text terminal, how to invoke menu?

Call menu-bar-openF10】 to open menu.

emacs menu in text terminal
emacs menu in text terminal (emacs 24.4)

For detail, see: Emacs: Command to Open Menu.

Like it? Buy Xah Emacs Tutorial. Thanks.