Don't be afraid of eshell

One of the reasons I decided to switch to emacs was the ability to embed a terminal window directly within my editor. Before last week, I never got the embedded terminal working and never really gave eshell much of a try. Initially, I tried to setup my regular shell (fish), but that never quite worked correctly. I also learned the power of dired and haven’t had the need to use the command line quite as much. Then, last week, I found myself editing and re-compiling code in a tight loop. I wanted access to the terminal, to trigger compilation, without leaving emacs. I created a quick-and-dirty eshell setup… and then wondered why I hadn’t done this sooner.

What is eshell?

Emacs shell, or eshell, presents bash-like command line interface built directly on emacs. It tries to be the best of two worlds:

  1. An elisp REPL wherein elisp commands can be directly executed
  2. A command line interface similar to bash

By understanding that eshell is presenting emacs in a terminal/shell sort of way, I can choose if I want to execute a command the elisp or bash way. More details can be found on the eshell page in the emacs manual.

Bare Minimum

First things first: to launch an eshell terminal, run M-x eshell.

Next, there are 2 things I need to have a basic, functioning shell environment:

  1. Have the correct PATH
  2. Have my most used aliases

Path

The PATH variable used by eshell is exposed through the addpath command. Without any arguments, addpath simply prints out the directories currently in PATH. When called with one or more arguments, addpath will append these paths to your PATH. There is no need to include the current PATH contents, as addpath always appends.

Aliases

Eshell has a number of built in commands, which are similar to the bash builtins. The most useful command is alias, which can be used to create any commands that you are used to using in your regular shell. The first step for me was to figure out which aliases I use most often and add them to eshell.

First, I needed the ls alternative that I use. Luckily, there is an ls provided by eshell (shown by running which ls). It takes the same arguments as my Mac’s builtin version.

Next, I needed a way to open an emacs buffer with a specific file. I use the command em to launch a new emacs session within my terminal window (emacs -nw). The alias for eshell is slightly different, since I am already running an emacs session. I simply want to start editing a particular file. Thus, the alias makes a call to the emacs function find-file.

My alias file (~/.emacs.d/eshell/alias) looks like this:

alias la ls -AFGhl $1
alias em find-file $1

Next Steps

The bare minimum takes care of my most frequent use case. Now, I can compile code without switching away from emacs!

The next step for me is to add the aliases to dotfiles and continue to improve my eshell setup. A quick web search lead me to a literate config for eshell in howardabrams/dot-files. There are several settings here that I will have to add for myself.


Comments

Comments are sent directly to me and may or may not be posted for display on this page.