I often find the need to start a new file by making a copy of another similar file within the same directory. netrw has several commands for copying files from one directory to another (ie: :h netrw-mc:), but there appears to no means to copy a file to a new name within the same directory.

I got excited when I stumbled across <C-R><C-F> which copies the current file under the cursor and thought about trying:

:!cp <C-R><C-R> new-file-name

But, netrw doesn't include the full path of the copy, so if you've navigated to directory in netrw before attempting the copy, the above command will fail.

Does anyone know if copying a file to a different name in the same directory is possible with netrw?

  • There’s not quite a clear question; it seems to be implicit what your asking. Could you provide some additional details and an actionable question? – D. Ben Knoble Sep 21 at 16:25

According to following thread on the mailing list vim_use from 03.10.15 (https://groups.google.com/forum/#!topic/vim_use/6yqU3RX2CWA):

How to copy and rename a file with netrw where the target and source directory are the same?

answered by the author of netrw DrChip himself:

Copy a file using the usual netrw method for copying: (assuming cursor is atop the file to be copied, and that the file is a file, not a director)

 mt (mark target) 
 mf (mark file) 
 mc 

You'll be given a prompt: Copy [filename] to : [filename]

Just edit the 2nd instance of [filename] to whatever you want, then hit <cr>.

Regards, Chip Campbell


UPDATE

I can confirm that the issue raised on the mailing list also still exist for me. Sorry for not testing before mentioning this thread! The error message is

**error** (netrw) tried using g:netrw_localcopycmd<cp>; it doesn't work!  

OS: macOS 10.12
Vim: 8.1.0280 (MacVim Release 151)
Netrw: v156 (doc claims v162, but file is still v156)

As described you should be able to avoid the issue by either setting in general

  1. let g:netrw_keepdir=0 (default value is 1) in your vimrc, or
  2. press first cd (netrw v162) (formerly c v156) and then mt mf mc

Unfortunately, I still experience an error:

**error** (netrw) copy failed; perhaps due to vim's current directory</Users/hotschke/.vim-configs/main> not matching netrw's (/Users/hotschke/.vim-configs/main/) (see :help netrw-c)

The obvious difference is a missing final slash and the surrounding brackets. I would really hope that netrw could be improved to support this regardless of the setting g:netrw_keepdir.

Note

  • This is the correct answer (according to the plugin author), but I can confirm this recipe does not work. If you read the link to the google groups, the same issue reported by the OP still seems to exists. But I greatly appreciate Hotschke taking the time to share the link from DrChip, – user590028 Sep 23 at 13:15
  • Hi. I can also reproduce the original issue as you can see by my updated answer. However, I would really appreciate it when netrw could be improved to make this more user friendly. Do you like to contact Charles Campbell alias DrChip to see whether he would consider improving the current situation? – Hotschke Sep 24 at 10:17
  • I've sent an email to DrChip with a link to this discussion and a request he review & comment. – user590028 Sep 25 at 12:16
up vote 0 down vote accepted

First, I want to thank Hotcshke for sharing his link to DrChip. If I could up-vote home more, I would. Using the very latest version of netrw and following DrChip's guidance we still experienced the same issues described by the OP in https://groups.google.com/forum/#!topic/vim_use/6yqU3RX2CWA.

BUT, DrChip provided an important setting that allows a simple cp to work as expected. According to the pi_netrw docs

MAKING THE BROWSING DIRECTORY THE CURRENT DIRECTORY

By default, |g:netrw_keepdir| is 1. This setting means that the current directory will not track the browsing directory.

Setting g:netrw_keepdir to 0 tells netrw to make vim's current directory to track netrw's browsing directory.

Add g:netrw_keepdir=0 to your .vimrc. You can then use an os shell to perform the copy:

:!cp old-filename new-file

---- FOLLOWUP INFORMATION ----

As suggested by Hotcshke, I reported this issue Dr Campbell (the author of the netrw plugin). Dr. Campbell graciously responded with the following

Hello:

I'm working out bugs now; when they (seem to be|are) gone, I'll do a release. I think I'll try to make a copy mechanism easier for files in the same directory; I'll have to try to do the same for remotely accessed files, too.

Regards, Chip Campbell

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.