dotfiles/emacs/.emacs.d/config/init-vterm.el
2024-03-20 12:06:33 -04:00

63 lines
2.5 KiB
EmacsLisp

;; -*- lexical-binding: t; -*-
(use-package vterm
:commands vterm
:config
(general-def 'insert vterm-mode-map "C-<escape>" #'vterm-send-escape)
;; Add a hook when vterm changes the directory
(defvar vterm-directory-change-hook nil
"Hooks to be run after vterm changes the directory")
(defun vterm--set-directory (path)
"Set `default-directory' to PATH."
(let ((dir (vterm--get-directory path)))
(when (and dir (eq major-mode 'vterm-mode))
(setq default-directory dir)
(run-hooks 'vterm-directory-change-hook))))
(add-hook 'vterm-mode-hook #'hack-dir-local-variables-non-file-buffer)
(add-hook 'vterm-directory-change-hook #'hack-dir-local-variables-non-file-buffer)
(with-eval-after-load 'consult
(defun vterm-consult-yank-from-kill-ring-action (orig-fun &rest args)
(if (equal major-mode 'vterm-mode)
(let ((inhibit-read-only t)
(yank-undo-function (lambda (_start _end) (vterm-undo))))
(cl-letf (((symbol-function 'insert-for-yank)
(lambda (str) (vterm-send-string str t))))
(apply orig-fun args)))
(apply orig-fun args)))
(defun vterm-disable-consult-preview (orig-fun &rest args)
"Disable Consult previews in vterm buffers. Meant to advise consult--insertion-preview."
(unless (equal major-mode 'vterm-mode)
(apply orig-fun args)))
(advice-add 'consult-yank-from-kill-ring :around #'vterm-consult-yank-from-kill-ring-action)
(advice-add 'consult--insertion-preview :around #'vterm-disable-consult-preview))
(defun vterm-send-password ()
(interactive)
(comint-send-invisible "Enter password: ")
(vterm-send-string "\n")
(clear-this-command-keys))
(defun vterm-edit-zsh-command-line ()
"Edit the current command line in a temp buffer."
(interactive)
(vterm-send "C-x")
(vterm-send "C-e"))
(defun vterm-send-C-g ()
(interactive)
(vterm-send "C-g"))
:general
(leader-map "v" #'vterm)
(vterm-mode-map "C-x C-e" #'vterm-edit-zsh-command-line
"C-c C-g" #'vterm-send-C-g)
:custom
(vterm-max-scrollback 10000)
(vterm-environment '("TYPEWRITTEN_CURSOR=terminal"))
(vterm-ignore-blink-cursor t)
(vterm-clear-scrollback-when-clearing t)
(vterm-eval-cmds '(("find-file" find-file)
("ff" find-file)
("message" message)
("vterm-clear-scrollback" vterm-clear-scrollback)
("man" man)
("ediff" ediff))))
(provide 'init-vterm)