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

2,969 views
Skip to first unread message

Shane Dev

unread,
Oct 3, 2015, 4:23:32 PM10/3/15
to vim_use
for example, I want to "backup" a file, in bash I type

cp /etc/foo /etc/foo.0

can I achieve the same time with netrw?

Charles E Campbell

unread,
Oct 5, 2015, 12:06:03 PM10/5/15
to vim...@googlegroups.com
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

Shane Dev

unread,
Oct 5, 2015, 4:02:31 PM10/5/15
to vim_use

> 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

Hi Chip,

When do this, I get an error window with
**error** (netrw) tried using g:netrw_localcopycmd<cp>; it doesn't work!;

I am using vim 7.4

Charles Campbell

unread,
Oct 5, 2015, 4:35:38 PM10/5/15
to vim...@googlegroups.com
Netrw depends on the o/s to do a lot of things, including copying
files. In this case, it is trying to use the "cp" command and its
failing when a system() call is taken...

call system(g:netrw_localcopycmd." ".shellescape(fname)."
".shellescape(topath))

fname will hold the file's name that's to be copied, and
topath holds the target directory (and filename).

or an isexecutable(g:netrw_localcopycmd) indicates the command is not
executable.

Translated: put whatever your system uses for copying files as a command
and enter it into g:netrw_localcopycmd. You may wish to modify the
other, related variables, too:
g:netrw_localmovecmd, g:netrw_localcopydircmd, g:netrw_localrmdir, etc.

If you're using windows, you ought to upgrade netrw (see
http://www.drchip.org/astronaut/vim/index.html#NETRW) because it has an
improved way to get windows to do what's wanted (fyi: using $COMSPEC).

Regards,
Chip Campbell

Shane Dev

unread,
Oct 7, 2015, 12:49:32 AM10/7/15
to vim_use, Charles.E...@nasa.gov
Hi Chris,

I appreciate the netrw author taking an interest in my newbie problem - but I am still stumped. I am running gvim 7.4.884 on Arch Linux. I checked some global variables -

:ec g:loaded_netrwPlugin
v153
:ec g:netrw_localcopycmd
cp
:ec executable(g:netrw_localcopycmd)
1

then I launch netrw on a directory which I have read/write permissions
mt
mf
mc
entered new file name


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

any clues?

Charles Campbell

unread,
Oct 7, 2015, 9:55:48 AM10/7/15
to Shane Dev, vim...@googlegroups.com
Shane Dev wrote:
> Hi Chris,
>
> I appreciate the netrw author taking an interest in my newbie problem - but I am still stumped. I am running gvim 7.4.884 on Arch Linux. I checked some global variables -
>
> :ec g:loaded_netrwPlugin
> v153
> :ec g:netrw_localcopycmd
> cp
> :ec executable(g:netrw_localcopycmd)
> 1
>
> then I launch netrw on a directory which I have read/write permissions
> mt
> mf
> mc
> entered new file name
> **error** (netrw) tried using g:netrw_localcopycmd<cp>; it doesn't work!
>
> any clues?
>
There's one place in the netrw plugin that issues that message; it
means, for whatever reason, the system() command is failing (and setting
v:shell_error; line 6637 in version v154h).

What does

:call system("cp 'oldfilename' 'newfilename'")

do? (where oldfilename is an actual filename). Also see :help
netrw-debug.

Regards
Chip Campbell

Shane Dev

unread,
Oct 8, 2015, 12:17:17 AM10/8/15
to vim_use, devsh...@gmail.com, Charles.E...@nasa.gov
Hi Chris,

:cd [the directory of oldfilename]


:call system("cp 'oldfilename' 'newfilename'")

works as expected.

After further testing, it seems I have to execute netrw-c (make browsing directory the current directory) before the sequence -

1. move cursor to oldfile,
2. mt
3. mf
4. mc
5. enter newfilename.

If I execute :cd (change to home directory) and repeat the above sequence, then I get -

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

Is this part of netrw design?

Charles Campbell

unread,
Oct 8, 2015, 1:22:43 PM10/8/15
to vim...@googlegroups.com
Shane Dev wrote:
> Hi Chris,
>
> :cd [the directory of oldfilename]
> :call system("cp 'oldfilename' 'newfilename'")
>
> works as expected.
>
> After further testing, it seems I have to execute netrw-c (make browsing directory the current directory) before the sequence -
>
> 1. move cursor to oldfile,
> 2. mt
> 3. mf
> 4. mc
> 5. enter newfilename.
>
> If I execute :cd (change to home directory) and repeat the above sequence, then I get -
>
> **error** (netrw) tried using g:netrw_localcopycmd<cp>; it doesn't work!
>
> Is this part of netrw design?
>
Yes; you can change the current directory behavior by setting
g:netrw_keepdir= 0 in your .vimrc.

Vim v6.x's Explorer always kept the vim current directory separate from
its browing directory -- netrw supports backwards compatability by also
doing so, by default; however, it lets you change that behavior. I,
personally, prefer to have netrw's browsing directory and vim's current
directory be the same, myself.

Glad you found the problem, though.

Regards,
Chip Campbell

Reply all
Reply to author
Forward
0 new messages