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
emacs Alt+x shell

Running Shell in Emacs

How to run a shell inside emacs?

Alt+x shell.

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

How to run a second shell inside emacs?

Ctrl+u Alt+x shell

The key Ctrl+u calls universal-argument

Can i run ssh inside emacs?

Alt+x term (this will create a new term for each command call). Then, you can run ssh, top, vi, etc. You can even start another emacs.

To exit term, press Ctrl+d.

When in term-mode, Alt+x describe-mode to see full list of its keys.

I do not recommend using term because normal emacs keys won't work. It's confusing to have two key systems in emacs. I using recommend a dedicated terminal to run ssh.

How to call a shell command once?

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

How to send current text selection to a shell command?

Select a region, then Alt+x 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, do Alt+x universal-argumentCtrl+u】 before calling the shell on region command.

Shell on Microsoft Windows

On Windows, by default, when you Alt+x shell, it starts cmd.exe.

How to run PowerShell in emacs?

You need to install the PowerShell mode.

How to have unix shell commands on Windows?

Install chocolatey [https://chocolatey.org/], then install

Replace the “info” by “install”.

Or try Emacs: xah-find.el, Find Replace in Pure Elisp

My shell doesn't work in Windows?

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

See also:

If you have a question, put $5 at patreon and message me on xah discord.
Or support me by Buy Xah Emacs Tutorial

Emacs Tutorial

Emacs Init

Emacs Keys


ELisp Examples

ELisp Write Major Mode

Emacs Tutorial

Quick Start

Manage Windows



Find Replace




Rectangle Edit

Line Wrap


View Special File

Working with Brackets

Org Mode


Emacs Efficiency