Terminal Emulator

This is where my terminal configuration lies! It is a seperate layer. When deactivated, we can still fall back to using bash and the regular shell-mode. This configuration is based on ansi-term.

;; on windows we just use eshell and don't configure ansi-term
(if on-windows
    (defalias 'sh #'eshell)
  (use-package term
    :demand
    :init
    <<do.term/which-shell>>
    <<do.term/close-on-exit>>
    :config
    <<do.term/mode-toggle>>
    <<do.term/hooks>>
    <<do.term/alias>>
    :bind
    <<do.term/keys>>))

TODO Choice of shell to launch

This disables the query before running the mode.

TODO: move to the new advice format.

(defvar do.term/path-to-shell "/usr/bin/zsh")
(defadvice ansi-term (before do.term/which-shell-advice)
  (interactive (list do.term/path-to-shell)))
(ad-activate 'ansi-term)

TODO Close on Exit

Don't linger around when I don't need you anymore.

TODO: move to new advice system.

(defadvice term-sentinel (around do.term/close-on-exit-advice (proc msg))
  (if (memq (process-status proc) '(signal exit))
      (let ((buffer (process-buffer proc)))
        ad-do-it
        (kill-buffer buffer))
    ad-do-it))
(ad-activate 'term-sentinel)

Mode toggle

We can either be in char mode, which is more like a traditional terminal emulator, or in line mode, which is more akin th M-x shell behavior. Toggle with C-x C-j. (bound below)

(defun do.term/toggle-mode ()
  "Switch from char-mode to line-mode and vice versa."
  (interactive)
  (if (term-in-line-mode)
      (term-char-mode)
    (term-line-mode)))

Hooks

(defun do.term/hooks ()
  (goto-address-mode)
  (setq global-hl-line-mode nil)
  (setq term-buffer-maximum-size 10000)
  (setq-local ml-interactive? t) ;; for mode line
  (setq-local show-dir-in-mode-line? t) ;; also mode line
  (setq show-trailing-whitespace nil)
  ;; disable company in favor of shell completion
  (company-mode -1))
(add-hook 'term-mode-hook 'do.term/hooks)

Alias

Run the terminal by doing M-x sh.

(defalias 'sh 'ansi-term)

Keys

Bind the mode toggle commands to some keys.

(:map term-mode-map
      ("C-c C-j" . do.term/toggle-mode)
      ("C-c C-k" . do.term/toggle-mode))

Colors

;; nicer terminal colors
(use-package eterm-256color
  :ensure t
  :config
  (add-hook 'term-mode-hook #'eterm-256color-mode))

TODO Naming

I want to change the name of the terminal buffers from *ansi-term* to something else.