Emacs as a shell

Home About Codeberg

This article is not about shell-mode, an Emacs wrapper around a your OS's shell REPL, nor is it about term-mode, the Emacs terminal emulator. It is about how Emacs is a shell in and of itself, and how it can replace a CLI shell.

Why a CLI shell is so useful

CLI terminals are nice because you can access any one of hundreds, if not thousands, of functions on your computer system with just a few keystrokes. If you are a regular CLI user, you probably have a favorite terminal emulator, and use it in the following ways:

I do this as an Emacs user, but in a slightly different way.

Long-term, per-project terminals

Start with dired

C-x d — prompts for a directory path to browse, then launches a dired (I pronounce it "Dir--ed"), the Emacs directory editor. I have one dired buffer open for every project I work on in Emacs, and I switch to this buffer as the starting point for anything I might do in the project I am working on.

dired commands

While in the dired buffer, several single-key commands are available to you:

M-x find-grep

Emacs provides a nice wrapper around the find and grep shell programs. It calls these programs, but captures and formats the output in a separate buffer that you can more easily navigate with the cursor. It is like always piping the output of grep to less but with hyperlinks to jump to the search results.

M-x compile

This is a make-shift IDE tool to run a compiler on code in the current directory. This will prompt you for a command to run, such as make or cmake. It is similar to pressing & in the dired buffer, but will use regular expressions to find and highlight file and line numbers, so if your compilation process discovers an error, you can navigate to location of the error in the source code using the cursor or mouse.

Browsing through source code

I wrote a more detailed walk-through comparing the specific actions I would take when starting work on a new long-term project, please refer to the Browsing source ccode article.

Short-term, one-time-use terminals

You might be in the middle of your work, then realize you need to run a command, such as htop to what program is using your CPU the most, or df -h to check how much disk space you have available. You could just launch a terminal emulator to do this, but it is good to get into the habit of using Emacs instead of a terminal emulator for this, since you never know when the text output by this command might be something you want to copy and paste elsewhere.

Further Reading


Emacs for Professionals

This article is part of my Emacs for Professionals series, in which I explain in a few paragraphs how I perform a specific common task using Emacs in ways that people already familiar with command line tools and Linux shell scripting can quickly understand.