How can I "Save As ..." in Emacs without visiting a new file?

If I use Cx Cw (write-file) to write the current contents of the buffer to a new location, then after that my buffer will visit the new file instead of the original one. Sometimes I would like to save a copy of the buffer to a new location, but then continue editing at the original location .

I know that I can kill the existing file name from the history of the minibuffer (write-file) , and then immediately return the Cx Cf (find-file) prompt to visit the original file again, and this is the workaround that I am currently using. However, it feels inelegant.

I wondered if the (write-file) could accept some kind of prefix argument so as not to visit the buffer, but this only affects the confirmation of the rewriting.

So: is there an easier way to save the contents of the buffer in a file without changing the file I visit?

+13
file-io emacs
source share
6 answers

Select the entire buffer with Cx h , then use Mx write-region .

+16
source share

If you need dired-x by default or otherwise make the dired-jump 1 function available, then the following is pretty simple:

Cx Cj C (enter new name) RET q

What is:

  • Go to the obsolete, indicating the file that you came from
  • Copy this file
  • Exit completed, returns to file buffer

1 Ch i g (dired-x) Optional Installation Dired Jump RET

+4
source share

If you do this regularly enough to annoy you, you could define this function

 (defun write-file-copy (filename) (interactive "F") (write-region (point-min) (point-max) filename)) 

and tie it to something that makes sense to you.

+3
source share

This is based on Inaimathi's answer and my comment on this, as well as some additional settings:

  • Suggest the current file name for editing by default, as I personally often want to change it, and it’s a little annoying not having it there to start with.
  • Do not overwrite an existing file without asking the user, and never overwrite the current buffer file.
  • If the region is active, write the region; otherwise write the entire (extended) buffer.
 (defun my-write-copy-to-file () "Write a copy of the current buffer or region to a file." (interactive) (let* ((curr (buffer-file-name)) (new (read-file-name "Copy to file: " nil nil nil (and curr (file-name-nondirectory curr)))) (mustbenew (if (and curr (file-equal-p new curr)) 'excl t))) (if (use-region-p) (write-region (region-beginning) (region-end) new nil nil nil mustbenew) (save-restriction (widen) (write-region (point-min) (point-max) new nil nil nil mustbenew))))) (global-set-key (kbd "Cc w") 'my-write-copy-to-file) 
+2
source share

After reading the phils (dired-jump) answer, for people who are less familiar with Dired, I came up with the following approach:

M -! cp FILE NEWNAME RET

As a bonus, you have access to tabs and you are already in the FILE directory, so typing the first file name is nice and fast.

+1
source share

You can use write-file, which is designed for this: 'Cx Cw' name-of-file

0
source share

All Articles