Add rename-current-buffer-file and bind it

C-x C-r now renames the current buffer’s file.
This commit is contained in:
Gergely Polonkai 2016-09-26 15:52:45 +02:00
parent f0a11c89e2
commit 93ed071cee

22
init.el
View File

@ -471,3 +471,25 @@ Version 2016-02-16"
(global-set-key (kbd "<C-return>") 'open-line-below) (global-set-key (kbd "<C-return>") 'open-line-below)
(global-set-key (kbd "<C-S-return>") 'open-line-above) (global-set-key (kbd "<C-S-return>") '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)