(macros)
Code

Terminal

Macros embeds a real terminal emulator (backed by alacritty's VT engine). Unlike the line-oriented shell, this is a full ANSI/VT terminal — curses apps, REPLs, vim, htop, and color all work.

Opening a terminal

Key Command Action
C-c C-t term Open a terminal running your login shell
ansi-term Alias for term
term-run Open a terminal running a specific program

term (and ansi-term) launches your login shell in a read-only term-mode buffer. term-run prompts for a program and runs that instead.

Keys go to the shell

While a terminal buffer is focused, almost every key is sent straight to the shell — including C-c, C-r, the arrow keys, and Tab. The mouse wheel scrolls the terminal's scrollback.

The one exception is the editor prefix C-x, which is reserved so window and buffer commands still work:

Key Action
C-x o switch to another pane
C-x k kill the terminal buffer
C-x 2 / C-x 3 split, e.g. to keep code and a terminal side by side

Two more ways out of the shell:

  • The alt leader. Under evil-mode, M-SPC starts any chord you bound with evil-leader-set — M-SPC n o runs your SPC n o. See Modal editing.
  • Pass-through keys. List any other chords the terminal should hand to the editor:
(set-option "terminal-passthrough-keys" "alt-o ctrl-]")
(define-key "fundamental" "alt-o" 'other-window)

When the shell exits

A terminal whose program has exited releases the keyboard, so normal editor keys work in the leftover buffer. terminal-exit-hook runs with the terminal's buffer name, so you can clean up automatically:

(define (my-close-dead-terminal name)
  (let ((w (get-buffer-window name)))
    (when (and (>= w 0) (> (length (window-list)) 1))
      (select-window w)
      (delete-window))
    (kill-buffer-named name)))
(add-hook "terminal-exit-hook" 'my-close-dead-terminal)

Multiple terminals

Each terminal gets a distinct buffer name, so you can run several at once. make-terminal reuses an existing terminal buffer if the name is already taken, so binding a key to a named terminal gives you a toggle-style "pop to my terminal".

Customizing

term has no global key in Emacs, so Macros picks C-c C-t as a sensible default — rebind it in your init.scm like any other key:

(define-key "fundamental" "ctrl-c enter" 'term)