Function: treemacs-rename-file

treemacs-rename-file is an autoloaded, interactive and byte-compiled function defined in treemacs-file-management.el.

Signature

(treemacs-rename-file)

Documentation

Rename the file/directory at point.

Buffers visiting the renamed file or visiting a file inside the renamed directory and windows showing them will be reloaded. The list of recent files will likewise be updated.

Key Bindings

Aliases

treemacs-rename (obsolete since v2.9.3)

Source Code

;; Defined in ~/.emacs.d/elpa/treemacs-20251226.1307/treemacs-file-management.el
;;;###autoload
(cl-defun treemacs-rename-file ()
  "Rename the file/directory at point.

Buffers visiting the renamed file or visiting a file inside the renamed
directory and windows showing them will be reloaded.  The list of recent files
will likewise be updated."
  (interactive)
  (treemacs-block
   (treemacs-unless-let (btn (treemacs-current-button))
       (treemacs-pulse-on-failure "Nothing to rename here.")
     (-let [old-path (treemacs--select-file-from-btn btn "Rename: ")]
       (treemacs-error-return-if (null old-path)
         "Found nothing to rename here.")
       (treemacs-error-return-if (not (treemacs--is-node-file-manageable? btn))
         "Only files and directories can be deleted.")
       (treemacs-error-return-if (not (file-exists-p old-path))
         "The file to be renamed does not exist.")
       (let* ((old-name  (treemacs--filename old-path))
              (new-name  (--if-let (treemacs--read-string
                                    "New name: " (file-name-nondirectory old-path))
                             it
                           (treemacs-return nil)))
              (dir       (treemacs--parent-dir old-path))
              (new-path  (treemacs-join-path dir new-name))
              (parent    (treemacs-button-get btn :parent)))
         (treemacs-error-return-if
             (and (file-exists-p new-path)
                  (or (not (eq 'darwin system-type))
                      (not (string= old-name new-name))))
           "A file named %s already exists."
           (propertize new-name 'face font-lock-string-face))
         (treemacs--without-filewatch
          (rename-file old-path new-path)
          (treemacs--replace-recentf-entry old-path new-path)
          (-let [treemacs-silent-refresh t]
            (treemacs-run-in-every-buffer
             (treemacs--on-rename old-path new-path treemacs-filewatch-mode)
             ;; save-excursion does not work for whatever reason
             (-let [p (point)]
               (treemacs-do-update-node (treemacs-button-get parent :path))
               (goto-char p)))))
         (treemacs--reload-buffers-after-rename old-path new-path)
         (run-hook-with-args
          'treemacs-rename-file-functions
          old-path new-path)
         (treemacs-pulse-on-success "Renamed %s to %s."
           (propertize (treemacs--filename old-path) 'face font-lock-string-face)
           (propertize new-name 'face font-lock-string-face)))))))