Emacs: Run Shell in Emacs

By Xah Lee. Date: . Last updated: .

Start Shell in Emacs

Alt+x shell.

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

emacs shell 2022-06-02
emacs Alt+x shell

Start a Second Shell in Emacs

Ctrl+u Alt+x shell

The key Ctrl+u calls universal-argument

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.

Call a shell command once

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

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

Or, install the PowerShell mode.

emacs cmd pwsh 2021-08-28
emacs cmd pwsh 2021-08-28

How to have unix shell commands on Windows?

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

Replace the “info” by “install”.

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

My shell doesn't work in Windows?

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

Shell in Emacs

ErgoEmacs mascot-s276x226
Buy Xah Emacs Tutorial

Emacs Tutorial

Quick Start

Manage Windows

File

Buffer

Find Replace

Copy/Paste

Unicode

Whitespace

Rectangle Edit

Line Wrap

Shell

View Special File

Editing Brackets

Org Mode

HTML

Emacs Efficiency

Misc