Last active
September 8, 2023 15:47
-
-
Save jamescherti/fc1054ce24606c4574cfa41d1ed67412 to your computer and use it in GitHub Desktop.
Emacs Lisp: Rename both current buffer and file it is visiting.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Language: Emacs Lisp (elisp) | |
;; Description: Rename both current buffer and file it is visiting. | |
;; Author: James Cherti | |
;; License: MIT | |
;; URL: https://gist.github.com/jamescherti/fc1054ce24606c4574cfa41d1ed67412 | |
(defun rename-buffer-and-file () | |
"Rename both current buffer and file it is visiting." | |
(interactive) | |
(let* ((bufname (buffer-name)) | |
(filename (buffer-file-name)) | |
(basename (file-name-nondirectory filename))) | |
(unless filename | |
(error "The buffer with the name '%s' is not visiting a file." bufname)) | |
(unless (file-exists-p filename) | |
(error "The file '%s' cannot be found on the disk." filename)) | |
(let ((new-basename (read-string "New name: " basename))) | |
(when (not (string= basename new-basename)) | |
(save-buffer) | |
(rename-file filename new-basename 1) | |
(set-visited-file-name new-basename))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment