I have a function that renames a file by saving it to the new location then deleting the old:
execute "saveas" fnameescape(l:new_path)
call delete(l:old_path)
I ran into a problem with this today. l:new_path
already existed on disk, and I didn't want to overwrite it, so I answered "No" when prompted, but the function proceeded to delete l:old_path
anyways.
How can I test the execute "saveas"
command and prevent the delete in this case? I want to say something like:
execute "saveas" fnameescape(l:new_path) || return
But this throws an error: E121: Undefined variable: return
.
In the shell I could test $?
. Does VimL have something similar?
abort
tag to the function definition:function MyFunction() abort
, so it exits as soon as it detects an error:saveas
. See:help file-functions
.execute "saveas"
whenset confirm
is enabled, you're prompted to overwrite an existing file and answer "No"? It seems like it may not be possible, in which case I may need an alternative toset confirm
, maybe usingconfirm()
in combination with theglob()
test suggested by @romainl.