Copying file in emacs using existing file name as a "base"

448 Views Asked by At

When in a Dired buffer, is it possible to copy an existing file to a new file using the existing files name as a "base" and then edit it to create a new file name? I know you cannot copy a file to a new file with the exact same name. What I have been doing is copying the old file name to a register, using Shift-C on the old file name, and then pasting and editing the new file name. I'm looking for a way to do this that allows me to skip the step of copying the old file name to a register.

2

There are 2 best solutions below

7
Drew On BEST ANSWER

Just use C in Dired to copy the file of the current line to another file. At the prompt, use M-n to retrieve the existing file's name as the default, and edit it.

Throughout Emacs, M-n when you see a prompt retrieves the default value for your input. For file-name input in Dired, M-n typically retrieves the name of the file on the current line.

0
Ashton Honnecke On

I'm not sure what sort of setup this works with, but It's not mine.

For instance, if I'm trying to copy prepare-commit-msg.sample to prepare-commit-msg, emacs will autocomplete the partial filename when I hit enter from the following: enter image description here I then receive this message: enter image description here

The solution (for me, I'm not sure what all I've added to make it so) is, instead of "Enter", hitting C-M-j resulting in a new file that is the exact beginning of an already existing file: enter image description here