Existe-t-il un moyen de renommer un fichier ouvert dans Emacs ? Pendant que je le consulte ? Quelque chose comme save-as, mais l'original devrait disparaître.
Réponses
Trop de publicités?
Chris Conway
Points
24671
Jim G
Points
629
Essayez cette fonction de Le .emacs de Steve Yegge :
;; source: http://steve.yegge.googlepages.com/my-dot-emacs-file
(defun rename-file-and-buffer (new-name)
"Renames both current buffer and file it's visiting to NEW-NAME."
(interactive "sNew name: ")
(let ((name (buffer-name))
(filename (buffer-file-name)))
(if (not filename)
(message "Buffer '%s' is not visiting a file!" name)
(if (get-buffer new-name)
(message "A buffer named '%s' already exists!" new-name)
(progn
(rename-file name new-name 1)
(rename-buffer new-name)
(set-visited-file-name new-name)
(set-buffer-modified-p nil))))))
Jetez un coup d'œil à cette page, il y a une autre fonction connexe très utile, appelée "move-buffer-file".
Shawn Hoover
Points
332
Voici une version plus robuste adaptée de stevey.
;; Originally from stevey, adapted to support moving to a new directory.
(defun rename-file-and-buffer (new-name)
"Renames both current buffer and file it's visiting to NEW-NAME."
(interactive
(progn
(if (not (buffer-file-name))
(error "Buffer '%s' is not visiting a file!" (buffer-name)))
(list (read-file-name (format "Rename %s to: " (file-name-nondirectory
(buffer-file-name)))))))
(if (equal new-name "")
(error "Aborted rename"))
(setq new-name (if (file-directory-p new-name)
(expand-file-name (file-name-nondirectory
(buffer-file-name))
new-name)
(expand-file-name new-name)))
;; If the file isn't saved yet, skip the file rename, but still update the
;; buffer name and visited file.
(if (file-exists-p (buffer-file-name))
(rename-file (buffer-file-name) new-name 1))
(let ((was-modified (buffer-modified-p)))
;; This also renames the buffer, and works with uniquify
(set-visited-file-name new-name)
(if was-modified
(save-buffer)
;; Clear buffer-modified flag caused by set-visited-file-name
(set-buffer-modified-p nil))
(message "Renamed to %s." new-name)))
Bozhidar Batsov
Points
23298
Voici une autre version, qui est assez robuste et qui tient compte des VC :
(defun rename-file-and-buffer ()
"Rename the current buffer and file it is visiting."
(interactive)
(let ((filename (buffer-file-name)))
(if (not (and filename (file-exists-p filename)))
(message "Buffer is not visiting a file!")
(let ((new-name (read-file-name "New name: " filename)))
(cond
((vc-backend filename) (vc-rename-file filename new-name))
(t
(rename-file filename new-name t)
(set-visited-file-name new-name t t)))))))
Vous pouvez en savoir plus à ce sujet ici .