From 93ed071ceeb5443fcb531df276cff22efba3702c Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Mon, 26 Sep 2016 15:52:45 +0200 Subject: [PATCH] Add rename-current-buffer-file and bind it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit C-x C-r now renames the current buffer’s file. --- init.el | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/init.el b/init.el index 466890f..13fde80 100644 --- a/init.el +++ b/init.el @@ -471,3 +471,25 @@ Version 2016-02-16" (global-set-key (kbd "") 'open-line-below) (global-set-key (kbd "") 'open-line-above) + +; Copied from http://whattheemacsd.com/file-defuns.el-01.html +(defun rename-current-buffer-file () + "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))) + (error "Buffer '%s' is not visiting a file!" name) + (let ((new-name (read-file-name "New name: " filename))) + (if (get-buffer new-name) + (error "A buffer named '%s' already exists!" new-name) + (rename-file filename new-name 1) + (rename-buffer new-name) + (set-visited-file-name new-name) + ; TODO: this is suspicious for me… + (set-buffer-modified-p nil) + (message "File '%s' successfully renamed to '%s'" + name (file-name-nondirectory new-name))))))) + +(global-set-key (kbd "C-x C-r") 'rename-current-buffer-file)