2008-12-21 73 views

回答

76

Steve Yegge's .emacs試試這個功能:

 
;; 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 filename new-name 1) 
      (rename-buffer new-name) 
      (set-visited-file-name new-name) 
      (set-buffer-modified-p nil)))))) 

在該網頁看看,還有另一種真正有用的相關函數存在,被稱爲「移動緩衝文件」。

+1

注意:這個方法是不是`(setq uniquify緩衝名式「進)'的意思兼容,如果你有緩衝名爲`用戶\ index.html`(因爲你已經有`posts \ index.html`的另一個緩衝區),重命名將失敗 – dolzenko 2012-09-27 11:49:43

+0

`(set-buffer-modified-p nil)`似乎沒有必要。如果你在修改後的緩衝區中調用了「rename-file-and-buffer」,然後試圖殺死它,它會高興地做到這一點,而不會警告你未保存的更改。 – roldugin 2013-07-11 07:19:21

+0

這個函數也會(有點令人討厭地)在檢查當前緩衝區是否與文件關聯(在這種情況下會中止)之前詢問你一個新的名字*。 – Thomas 2015-09-11 12:04:12

284

是的,dired模式,您可以:

  • C-x C-jdired-jump到當前文件的名稱,在Dired)
  • R重命名文件(或dired-do-rename)。
  • C-x k RET回去了(改名)文件緩衝區

重命名相當於一個外殼mv,但它也將更新所有打開的緩衝區。

+8

這不是直接重命名當前文件。 – Pablo 2008-12-21 17:21:31

+4

C-x b,您又回到原始緩衝區。你可以編寫一個Elisp函數來完成它,但是我懷疑你會用它來節省很多擊鍵。 – 2008-12-22 01:49:41

+5

此外,您可以按C-x k返回到原始緩衝區,而不是C-x b。 – Yoo 2010-10-14 12:55:26

111

只是爲了完整性,因爲有些人可能會訪問此頁面,認爲他們將獲得Emacs的「另存爲」功能的答案,即打開文件的C-x C-w。

14

下面是從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))) 
    ;; Disable ido auto merge since it too frequently jumps back to the original 
    ;; file name if you pause while typing. Reenable with C-z C-z in the prompt. 
    (let ((ido-auto-merge-work-directories-length -1)) 
     (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))) 
    ;; Only rename if the file was saved before. Update the 
    ;; buffer name and visited file in all cases. 
    (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))) 

    (setq default-directory (file-name-directory new-name)) 

    (message "Renamed to %s." new-name)) 
-1

它可以通過複製來實現。 shift + c文件,emacs會要求你爲包含文件名的路徑指定一個名稱,所以你只需提供新名稱,然後輸入......當然,你必須刪除前一個。

10

這是另一個版本,那是相當穩健和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))))))) 

你可以閱讀更多關於它here

20

我最喜歡的是從Magnars的一個(的emacs rocks screencasts成名。)

不像其他的替代品,你不必從頭鍵入出了名的 - 你當前的名稱進行修改。

(defun rename-current-buffer-file() 
    "Renames current buffer and file it is visiting." 
    (interactive) 
    (let* ((name (buffer-name)) 
     (filename (buffer-file-name)) 
     (basename (file-name-nondirectory filename))) 
    (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: " (file-name-directory filename) basename nil basename))) 
     (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) 
      (set-buffer-modified-p nil) 
      (message "File '%s' successfully renamed to '%s'" 
        name (file-name-nondirectory new-name))))))) 

感謝James Yang提供了正確的版本。

2

基於magnars版本,我作如下修改,固定的INIT文件名部分:

(defun rename-current-buffer-file() 
    "Renames current buffer and file it is visiting." 
    (interactive) 
    (let* ((name (buffer-name)) 
     (filename (buffer-file-name)) 
     (basename (file-name-nondirectory filename))) 
    (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: " (file-name-directory filename) basename nil basename))) 
     (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) 
      (set-buffer-modified-p nil) 
      (message "File '%s' successfully renamed to '%s'" 
        name (file-name-nondirectory new-name)))))))