Emacs Shell Tutorial (Bash, cmd.exe, PowerShell)

, , …,

This page is a tutorial on using Emacs with the operating system's command line shell. On Linuxes, this is usually Bash. On Windows, this is “cmd.exe”, or can be PowerShell. (See: PowerShell Tutorial and PowerShell Modes.)

Interaction with Shell

Running Shell inside Emacs

How to run a shell inside emacs?

Start the shell by calling shell. While 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.

Ellen Taylor provided the following code, which makes shell command always start a new shell.

;; by Ellen Taylor, 2012-07-20
(defadvice shell (around always-new-shell)
  "Always start a new shell."
  (let ((buffer (generate-new-buffer-name "*shell*"))) ad-do-it))

(ad-activate 'shell)

Can i run ssh inside emacs?

You can run ssh inside emacs with emacs's terminal emulator. To start it, call term. Then, you can run {ssh, top, vi, …}. You can even start another emacs. To exit term, press 【Ctrl+d】.

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.

Calling a Single Shell Command

How to call a shell command?

To run single shell command, call shell-commandAlt+!】. This is under the menu 〖Tools ▸ Shell Command…〗.

To remove the resulting split panes, call delete-other-windowsCtrl+x 1】.

It is very useful to run shell commands while in emacs. For example, suppose you are writing a document or programing, and you need a list of a directory's content. Normally, you would switch to a terminal app, do ls, copy the result (probably with the mouse), then switch back to emacs, then paste. But with emacs's shell facilities, you can do all these within emacs.

How to apply a shell command to the current selection?

Select a region, then call shell-command-on-regionAlt+|】. For example, select a region, then type 【Alt+| wc -w Enter ↵】. This will return the number of words in your selection.

You can have the result replace the selected region. To do that, press 【Ctrl+u】 before the 【Alt+|】. This is useful for example when you want to run the command sort | uniq.

(The key 【Ctrl+u】 calls universal-argument. It is a interactive way to give arguments to emacs commands. ⁖ 【Ctrl+u 4 ⇟ Page ▽】 will scroll down 4 lines.)

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 unix's bash, but written entirely in elisp. It supports most unix commands such as {cd, ls, cp, mkdir, …}. 〔☛ Emacs: What's eshell? eshell vs Shell Difference?

How to make shell talk to Cygwin Bash, MSYS Bash, or PowerShell?

You can install ErgoEmacs or Lennart's EmacsW32. With them, you can type 【Alt+x msys-shell】 to start MSYS Bash. ErgoEmacs also have the command powershell which starts a PowerShell interface.

You can install PowerShell interface yourself. See: Emacs PowerShell Modes.

My shell doesn't work in Windows?

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

Viewing “man” Pages and “info”

How to read man page in emacs?

Call man or woman.

Emacs's man command calls the external unix man program, while woman is written in emacs lisp.

How to read GNU “info” manuals inside emacs?

Call infoF1 i】. Use mouse or arrow keys to move cursor to a link.

〔☛ See also: Emacs: Info-mode Keys

Directory Navigation and Managing Files

See: Emacs: File Management, dired Tutorial.

FTP and Remote File Editing

You can also transfer files between different machines connected by a network. The way to do this in emacs is consistent with the way it does directory navigation.

Call diredCtrl+x d】 and type a directory you want to work with. In this directory, suppose myfile.html is the one you want to upload across the network to the machine on example.com in the directory /public_html. Suppose your login name for that machine is “mary”.

Once in dired, move your cursor to the file you want to copy. Type C, then give the dir /ftp:mary@example.com:/public_html. You'll be asked for a password, then it'll be copied over.

Once you logged in to the ftp server, you can actually edit and save files on the remote server, bypassing all the upload pain.

(info "(emacs) Remote Files")

Searching for Text in Files (grep, find)

See: Emacs: Searching for Text in Files (grep, find).


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

Call tmm-menubarAlt+`】.

How to edit a file that requires root privilege?

When you get the file path prompt, type /sudo:: in front of it. ⁖ /sudo::/etc/bashrc. This will load up tramp.

A more basic, but inconvenient solution, is to press 【Alt+!】 (shell-command), then type “sudo chown file name”. Then, edit the file, then change the owner back.

If you need to work as sys admin a lot, you can always start another emacs instance by sudo emacs. Make the background color reddish, so you know that's the dangerous one. Call set-background-color to set the color.

blog comments powered by Disqus