How do I rename an open file in Emacs?

后端 未结 11 1856
别跟我提以往
别跟我提以往 2020-12-07 07:03

Is there a way to rename an open file in Emacs? While I\'m viewing it? Something like save-as, but the original one should go away.

11条回答
  •  难免孤独
    2020-12-07 07:39

    Here's another version, that's pretty robust and VC aware:

    (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)))))))
    

    You can read more about it here.

提交回复
热议问题