           (when (bound-and-true-p evil-local-mode)
             (evil-change-to-initial-state))
           (goto-char (point-max)))
-      (pop-to-buffer (+vterm--get-create-buffer buffer-name)))))
+      (setenv "PROOT" (or (doom-project-root) default-directory))
+      (let ((buffer (get-buffer-create buffer-name)))
+        (with-current-buffer buffer
+          (unless (eq major-mode 'vterm-mode)
+            (vterm-mode)))
+        (pop-to-buffer buffer)))))
 
 ;;;###autoload
 (defun +vterm/here (arg)

;;; term/vterm/autoload.el -*- lexical-binding: t; -*-

(defun +vterm--get-create-buffer (buffer-name)
  (setenv "PROOT" (or (doom-project-root) default-directory))
  (require 'vterm)
  (let ((buffer (get-buffer-create buffer-name)))
    (with-current-buffer buffer
      (unless (eq major-mode 'vterm-mode)
        (vterm-mode)))
    buffer))

;;;###autoload
(defun +vterm/toggle (arg)

  ;; This hack forces vterm to redraw, fixing strange artefacting in the tty.
  (save-window-excursion
    (pop-to-buffer "*scratch*"))
  (let* ((project-root (or (doom-project-root) default-directory))
         (default-directory
           (if arg
               default-directory
             project-root))
         display-buffer-alist)
    (setenv "PROOT" project-root)
    (vterm)))


(defvar +vterm--insert-point nil)
