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?

Alt+x shell.

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

How to run a second shell inside emacs?

Alt+x universal-argumentCtrl+u】 first before you Alt+x shell.

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, …}. You can even start another emacs.

To exit term, press 【Ctrl+d】.

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

〔►see Emacs: Difference between shell, 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?

see Why Run Shell in Emacs?

How to call a shell command once?

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

For example, try Alt+x shell-command “ls”.

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, press 【Ctrl+u】 before the 【Alt+|】.

The key 【Ctrl+u】 calls universal-argument. It is a interactive way to give arguments to commands.

Shell on Microsoft Windows

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

How to have unix shell commands on Windows?

Alt+x eshell.

see Emacs: M-x eshell

My shell doesn't work in Windows?

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

Misc

How to invoke GUI menu in text terminal?

Alt+x menu-bar-openF10】 to open menu.

see Emacs: Command to Open Menu

Shell Topic

  1. Emacs: Run Shell in Emacs
  2. Why Run Shell in Emacs?
  3. Emacs: Difference between shell, term, eshell
  4. Emacs: M-x eshell
  5. Emacs: View Linux man Page
  6. Emacs: Interactive Abbrev
  7. Emacs: File Manager, dired
  8. Emacs: Batch Rename Files
  9. Linux: Basic Shell Commands
Like it? Buy Xah Emacs Tutorial. Thanks.