Skip to content

Instantly share code, notes, and snippets.

@anton0xf
Created February 14, 2011 09:29
Show Gist options
  • Save anton0xf/825659 to your computer and use it in GitHub Desktop.
Save anton0xf/825659 to your computer and use it in GitHub Desktop.
emacs:rename-buffer-file
(defun rename-buffer-file (newname &optional ok-if-already-exists)
"rename visited file (buffer-file-name) to NEWNAME
and appropriately change buffer name (set-visited-file-name NEWNAME).
NEWNAME must be string.
Signals a `file-already-exists' error if a file NEWNAME already exists
unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.
A number as third arg means request confirmation if NEWNAME already exists.
This is what happens in interactive use with M-x."
(interactive "FRename to: \np")
(let ((file (buffer-file-name)))
(when file
(message "Rename buffer file %s to %s" file newname)
(rename-file file newname ok-if-already-exists)
(set-visited-file-name newname))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment