HOME

Shell Settings

EShell

EShell is a shell written in elisp. This makes it both less capable than other terminal emulators and also gives it some interesting features.

Opening EShells

First, a short-cut to quickly open a shell. Repeated invocations will bring back the same shell so we don't end up with a bunch of them runningn about.

(global-set-key (kbd "C-x m") 'eshell)

It's nice to have an easy short-cut to launch into a shell in the current buffer's directory. We'll start with a function to launch it:

(defun toby/eshell-in-buffer-dir ()
  "Open eshell in the current buffer's directory"
  (interactive)
  (let ((dir default-directory))
    (let ((b (get-buffer eshell-buffer-name)))
      (unless b (eshell)))
    (display-buffer eshell-buffer-name t)
    (switch-to-buffer-other-window eshell-buffer-name)
    (end-of-buffer)
    (unless (equalp dir default-directory)
      (cd dir)
      (eshell-send-input)
      (end-of-buffer))))

and a binding to launch it:

(global-set-key (kbd "C-x C-m") 'toby/eshell-in-buffer-dir)

Sometimes what you want is another shell instance:

;; Start a new eshell even if one is active.
 (global-set-key (kbd "C-x M") (lambda () (interactive) (eshell t)))

One great feature of Eshell is its ability to call Emacs functions as though they were shell commands. Here we'll define an alias ff that will open a file from the Eshell command line into an Emacs buffer:

(add-hook 'eshell-mode-hook
          (lambda ()
            (defalias 'ff 'find-file)))

Adjust some comint parameters (the mode that handles terminal buffers).

Multi-term is an extension for running multiple native shells within Emacs:

(require-package 'multi-term)
(setq multi-term-program "/bin/bash -l")

Comint Mode

comint-mode is the parent mode of many shell modes.

(setq comint-buffer-maximum-size 10240)
(add-hook 'comint-output-filter-functions 'comint-truncate-buffer)
(add-hook 'comint-output-filter-functions 'comint-watch-for-password-prompt)

Let's add a function to easily clear an active shell buffer:

(defun clear-shell ()
  "Clear the current shell buffer"
  (interactive)
  (let ((comint-buffer-maximum-size 0))
    (comint-truncate-buffer)))

Binding s-k to kill-buffer doesn't make sense to me.

(global-unset-key (kbd "s-k"))
(defun set-my-shell-bindings ()
  (interactive)
  (local-set-key (kbd "<up>")   'comint-previous-input)
  (local-set-key (kbd "<down>") 'comint-next-input)
  (local-set-key (kbd "s-k")    'clear-shell))

(add-hook 'shell-mode-hook 'set-my-shell-bindings)
(add-hook 'eshell-mode-hook 'set-my-shell-bindings)

Author: Toby Tripp

Created: 2015-02-04 Wed 10:41

Emacs 24.3.1 (Org mode 8.2.10)

Validate