Skip to content

Instantly share code, notes, and snippets.

@jamescherti
Last active September 8, 2023 15:47
Show Gist options
  • Save jamescherti/fc1054ce24606c4574cfa41d1ed67412 to your computer and use it in GitHub Desktop.
Save jamescherti/fc1054ce24606c4574cfa41d1ed67412 to your computer and use it in GitHub Desktop.
Emacs Lisp: Rename both current buffer and file it is visiting.
;; 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