当前位置:  开发笔记 > 编程语言 > 正文

如何在Emacs中重命名打开的文件?

如何解决《如何在Emacs中重命名打开的文件?》经验,为你挑选了6个好方法。

有没有办法在Emacs中重命名打开的文件?在我看的时候?像save-as之类的东西,但最初的东西应该消失.



1> Chris Conway..:

是的,使用dired模式,您可以:

C-x d 打开直接

RET 选择当前文件的目录

C-x C-j(dired-jump到当前文件的名称,在Dired中)

R重命名文件(或dired-do-rename).

q 返回(重命名)文件缓冲区

重命名等同于shell mv,但它也会更新任何打开的缓冲区.


对我来说,`Cx Cj`默认不受约束.首先执行`Mx load-library RET dired-x RET`使其绑定.
那不是直接重命名当前文件.
此外,您可以按Cx k返回原始缓冲区,而不是Cx b.
Cx b,你回到了原来的缓冲区.你可以写一个Elisp函数来做它,但我怀疑你会用它来保存很多击键.
如果`Cx Cj`未绑定的另一个选择是第一次执行`Mx dired-jump`.它将自动加载`dired-x`(这也将导致`Cx Cj`从此点开始定义).
谢谢你的回答.它比添加功能更简单.至于"那不是直接重命名当前文件",它似乎就是这样做的.该文件在磁盘和缓冲区上重命名.

2> 小智..:

只是为了完整性,因为有些人可能会访问此页面,认为他们会得到Emacs的"另存为"功能的答案,那就是打开文件的Cx Cw.


@asmeurer你错了!保存后,您将编辑新文件.
也许asmeurer意味着,"你正在编辑的文件仍然[存在]".那么,这是正确的吗?我会检查,但是,那么,你不能低估我的评论,哈哈哈.
再一次,为什么没有一个功能可以进行评论?!

3> Matt Curtis..:

试试Steve Yegge的.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))))))

看看那个页面,那里有另一个真正有用的相关函数,叫做"move-buffer-file".


@dolzenko修复了

4> The Unfun Ca..:

我最喜欢的是来自Magnars的那个(emacs摇滚截屏名声.)

与其他替代方案不同,您不必从头开始输入名称 - 您将获得要修改的当前名称.

(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的正确版本.


感谢您为我发布。SO的方法是更正答案,而不是发布新的,稍作修改的答案。

5> Shawn Hoover..:

这是一个更强大的版本改编自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))



6> Bozhidar Bat..:

这是另一个版本,非常强大且可识别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)))))))

你可以在这里阅读更多相关信息.

推荐阅读
惬听风吟jyy_802
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有