Introduction to Emacs Shell

While starting up a new job, I’ve been pretty pre-occupied and have neglected my weekly learning more about the tools I use everyday. However, I was this morning, I was re-impressed with how well Emacs’ eshell-mode works with Zshell, and thought I’d explain a bit more about it.

After you’ve started Emacs, just hit M-x and type eshell and you’ll start up what appears like a shell process in an Emacs buffer. Most of the keystrokes you expect from a shell and from Emacs are available, with a few exceptions:

Keep in mind that it isn’t an actual Z shell. Type echo $0 to prove that nothing is actually running. Eshell just looks like a shell. When you type in a command, it then spawns off your current shell for execution, like zsh.

No Aliases!?

Since Eshell doesn’t have an actual shell executable hanging around, it is a bit more efficient, however, this is why your hot cool Zshell prompt doesn’t display, nor are your aliases available.

I know, I know. I have a lot of functions and aliases that I depend on, however, you can add something like the following to .emacs file to get back some of what you’ve lost:

(defalias 'e 'find-file)

However, this becomes a global command, not just what is available in Eshell. To localize it, use this instead:

(defun eshell/e (file)
      (find-file file))

(defun eshell/ee (file)
      (find-file-other-window file))

Definitely create a eshell/emacs command just in case you make a mistake.

More on Aliases

You can have more traditional aliases if you drop the equals sign, as in:

alias ll 'ls -l $*'

Note: Unlike a normal shell alias, the arguments aren’t automatically appended, so you need to place the $* appropriately.

The aliases that you define in this way are automatically saved. Restart Emacs and an Eshell, and type alias and you’ll see all your aliases.

Want to dive in a bit deeper?