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 2016-07-18
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.

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}?

Call Just One 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 in Microsoft Windows FAQ.

View Linux Manual Page

Emacs: View “man” Page

Directory Navigation and Manage Files

For simple shell commands such as {ls, cd, cp, rm, mkdir, …}, it's better to do with dired.

Emacs: File Management (dired tutorial)


How to invoke GUI menu in text terminal?

Call menu-bar-openF10】 to open menu.

Detail at Emacs: Command to Open Menu

Like it? Buy Xah Emacs Tutorial. Thanks.