Created
February 25, 2015 17:41
-
-
Save stephane/19fddde21391ea2d42f3 to your computer and use it in GitHub Desktop.
To rename and delete buffer/file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun rename-this-buffer-and-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))) | |
(cond ((get-buffer new-name) | |
(error "A buffer named '%s' already exists!" new-name)) | |
(t | |
(rename-file filename new-name 1) | |
(rename-buffer new-name) | |
(set-visited-file-name new-name) | |
(set-buffer-modified-p nil) | |
(message "File '%s' successfully renamed to '%s'" name (file-name-nondirectory new-name)))))))) | |
(defun delete-this-buffer-and-file () | |
"Removes file connected to current buffer and kills buffer." | |
(interactive) | |
(let ((filename (buffer-file-name)) | |
(buffer (current-buffer)) | |
(name (buffer-name))) | |
(if (not (and filename (file-exists-p filename))) | |
(error "Buffer '%s' is not visiting a file!" name) | |
(when (yes-or-no-p "Are you sure you want to remove this file? ") | |
(delete-file filename) | |
(kill-buffer buffer) | |
(message "File '%s' successfully removed" filename))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment