0

I have the following code to copy the file I am editing to another folder, but when execute the command by press F5, it tries to do command "copy path newpath" literally without replace the variables with the their value. What's the solution?

function! Cpf()

let path = expand('%:p')
echo path
echo "123"
let newpath = substitute(path,'test1','test2','g')
echo "copy " path newpath
:!copy path newpath
endfunction

:map <F5> :call Cpf()<CR>

1 Answer 1

1

try to change the line:

:!copy path newpath

into

call system("copy ".path." ".newpath)

or this

exec '!copy ' . path . ' ' . newpath

Note that if you used copy, it means, it won't work for almost all *nix systems. You may want to add a OS check if you want it to be portable.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Kent, it works. But could you please point me to some doc? What's the meaning of .path." ".newpath, does the dot concat the string?
the "dot" concatenates the string to build your external command. for help, :h system( and :h :exec @DanielWu

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.