From: Francois Fleuret Date: Fri, 2 Apr 2010 18:50:30 +0000 (+0200) Subject: Added rename-file-and-buffer. X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=f02deb48b46d25764852a82371b9a5b35a7c8910;p=elisp.git Added rename-file-and-buffer. --- diff --git a/emacs.el b/emacs.el index 8d4c2f0..b582fa2 100644 --- a/emacs.el +++ b/emacs.el @@ -672,6 +672,26 @@ occurrences " ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; http://blog.tuxicity.se/elisp/emacs/2010/03/26/rename-file-and-buffer-in-emacs.htm + +(defun rename-file-and-buffer () + "Renames current buffer and file it is visiting." + (interactive) + (let ((name (buffer-name)) + (filename (buffer-file-name))) + (if (not (and filename (file-exists-p filename))) + (message "Buffer '%s' is not visiting a file!" name) + (let ((new-name (read-file-name "New name: " filename))) + (cond ((get-buffer new-name) + (message "A buffer named '%s' already exists!" new-name)) + (t + (rename-file name new-name 1) + (rename-buffer new-name) + (set-visited-file-name new-name) + (set-buffer-modified-p nil))))))) + +(global-set-key (kbd "C-c r") 'rename-file-and-buffer) + (defun ff/non-existing-filename (dir prefix suffix) "Returns a filename of the form DIR/PREFIX[.n].SUFFIX whose file does not exist"