Jak zmienić nazwę otwartego pliku w Emacs?

Czy jest sposób na zmianę nazwy otwartego pliku w Emacsie? Kiedy to oglądam? Coś w stylu save-as, ale oryginał powinien zniknąć.

 214
Author: titanofold, 2008-12-21

10 answers

Spróbuj tej funkcji z Steve 'a Yegge' a .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))))))

Spójrz na tę stronę, jest tam jeszcze jedna bardzo przydatna powiązana funkcja, o nazwie "move-buffer-file".

 80
Author: Matt Curtis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-28 18:16:12

Tak, w trybie dired możesz:

  • C-x C-j (dired-jump do nazwy bieżącego pliku, w Dired)
  • {[3] } aby zmienić nazwę pliku (lub dired-do-rename).
  • C-x k RET aby wrócić do (przemianowanego) bufora plików

Zmiana nazwy jest równoznaczna z powłoką mv, ale zaktualizuje również otwarte bufory.

 297
Author: Chris Conway,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-24 16:44:13

Tak dla kompletności, ponieważ niektórzy ludzie mogą odwiedzać tę stronę myśląc, że otrzymają odpowiedź na funkcję" Zapisz jako " w Emacsie, czyli C-x C - W dla otwartego pliku.

 116
Author: Jim G,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-10-10 17:12:22

[[1]}moim ulubionym jest ten z Magnars (of emacs rocks screencasts fame.)

W przeciwieństwie do innych alternatyw, nie musisz wpisywać nazwy od zera - dostajesz obecną nazwę do modyfikacji.

(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)))))))
Dzięki James Yang za poprawną wersję.
 20
Author: The Unfun Cat,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-24 07:53:38

Oto bardziej wytrzymała wersja zaadaptowana przez stevey ' a.

;; 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))
 14
Author: Shawn Hoover,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-18 15:54:48

Oto kolejna wersja, która jest dość solidna i świadoma 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)))))))

Możesz przeczytać więcej na ten temat tutaj .

 10
Author: Bozhidar Batsov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-30 14:08:44

Jest sposób bardzo prosty, naciskasz polecenie M-x i następnie wpisz VC-rename-file, po tym wystarczy wybrać bieżący plik w katalogu, a następnie wybrać nową nazwę. Premia ze zmienionym plikiem zostanie odświeżona.

Source:https://www.gnu.org/software/emacs/manual/html_node/emacs/VC-Delete_002fRename.html

 4
Author: Rafael Nascimento,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-02 13:15:06

Na podstawie wersji magnars, zmodyfikowałem jak poniżej, poprawiłem część nazwy pliku 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)))))))
 2
Author: James Yang,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-26 09:08:09

Jeśli używasz Spacemacs , to otrzymujesz to zachowanie za darmo, ponieważ pochodzi z implementacji rename-current-buffer-file (opartej na magnarach), która domyślnie jest związana z SPC-f-R.

Https://github.com/syl20bnr/spacemacs/blob/bd7ef98e4c35fd87538dd2a81356cc83f5fd02f3/layers/%2Bdistributions/spacemacs-base/funcs.el#L294

 0
Author: Jason Axelson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-04 21:05:48

Można to osiągnąć przez kopiowanie. shift + c na pliku i emacs poprosi Cię o oznaczenie nazwy ścieżki wraz z nazwą pliku, więc po prostu podaj nową nazwę i wprowadź...oczywiście musisz usunąć poprzedni.

 -1
Author: Eric_Chen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-27 02:49:14