Howardism Musings from my Awakening Dementia
My collected thoughts flamed by hubris
Home PageSend Comment

Emacs E-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:

  • C-p / C-n move the cursor up instead of replaying the history. Use the up and down arrow keys or M-p / M-n to get the history.

  • Hit C-c twice in order to break out things.

  • The !! history works just as you expect.

  • Typing the name of an executable runs it, and colors are accepted, so typing ls works as you expect.

  • If the command you type isn't a program available to the shell, it assumes that it is an Emacs command, so instead of typing cat, type find-file to load the file up in an Emacs buffer.

  • Tab completion is done by Eshell, and isn't as powerful as Zshell. Does anyone know how we can call Zshell for that feature? Seems like a difficult problem.

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 = sign:

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.

Tell others about this article:
Click here to submit this page to Stumble It