vterm + zsh 配置



  • 多个vterm
  • buffer name和当天目录自动同步
  • toggle命令,可以自动新建目录在当前目录的vterm进程 (vterm-toggle/C-u vterm-toggle)
  • vterm之间跳转 (switch-buffer-same-major-mode)
(require 'cl-lib)

;;; Package

(use-package vterm
  :load-path  "~/attic/emacs-libvterm"
  (setq vterm-shell "zsh")
  (add-hook 'vterm-set-title-functions 'vterm--rename-buffer-as-title))

;;; Functions

(defun vterm--rename-buffer-as-title (title)
  (rename-buffer (format "vterm @ %s" title) t))

(defvar vterm-toggle-prev-buffer nil
  "Used by ‘vterm-toggle’.")

(defun vterm-buffer-list ()
  "Return a list of vterm buffers."
  (cl-remove-if-not (lambda (buf)
                       (buffer-local-value 'major-mode buf)

(defun vterm-cd (dir)
  "cd to DIR."
  (when (derived-mode-p 'vterm-mode)
    (vterm-send-key "u" nil nil t)
    (execute-kbd-macro (format "cd %s
" dir))))

(defun vterm-toggle (&optional arg)
  "Toggle vterm.

ARG: C-u: open the vterm buffer with the same dir of current buffer
If there exists an Vterm buffer with current directory, use that,
otherwise create one.

C-u C-u: same as C-u, but reuse a existing vterm buffer instead of
creating one."
  (interactive "p")
  (if (derived-mode-p 'vterm-mode)
      ;; toggle off
      (if vterm-toggle-prev-buffer
          (switch-to-buffer vterm-toggle-prev-buffer)
        (user-error "No previous buffer to jump back to"))
    ;; toggle on
    (let ((buffer-list (vterm-buffer-list)))
       ((or (eq arg 4) ; C-u
            (eq arg 16)) ; C-u C-u
        ;; open in current dir
        (let* ((dir default-directory)
                (catch 'found
                  (dolist (buffer buffer-list nil)
                    (when (equal dir (buffer-local-value
                      (throw 'found buffer))))))
          ;; found the buffer with the same dir
          (setq vterm-toggle-prev-buffer
          (if buffer-with-same-dir
              (switch-to-buffer buffer-with-same-dir)
            ;; or not and maybe create a new one
             (if (eq arg 16)
                 (progn (message "No valid vterm buffer found, reuse one.")
                        (car buffer-list))
               (message "No valid vterm buffer found, create a new one.")
            (vterm-cd dir))))
       ;; simply open
       (t (setq vterm-toggle-prev-buffer (current-buffer))
          (switch-to-buffer (or (car buffer-list)

(defun switch-buffer-same-major-mode ()
  "Switch buffer among those who have the same major mode as the current one."
    "Buffer: "
    (mapcar #'buffer-name
            (cl-remove-if-not (lambda (buf)
                                 (buffer-local-value 'major-mode buf)


# vterm dir tracking
vterm_prompt_end() {
    # or maybe only pwd?
    printf "\e]51;A$(whoami)@$(hostname):$(pwd)\e\\";

# sync title
# https://github.com/ohmyzsh/ohmyzsh/issues/5700
function precmd () {
  echo -ne "$window_title"




你提的这个bug ,还是没解决吗? zsh版本是多少。



zsh 5.7.1 (x86_64-apple-darwin19.0)