Use <args>
when referencing command arguments. <args>
will expand to the arguments passed to the command. Also, while not strictly required, because you always want to pass one argument to your command (the new filename) you should probably use -nargs=1
.
I would also recommend saving the new file and then deleting the old file, just to be safe. You can do this by using #
(the alternate file name) instead of %
(the current file name) inside expand()
. WhenAfter using saveas:saveas
, the alternate file name is set to the the old file name.
command! -nargs=1 Move :saveas <args> | call delete(expand('#'))
Optionally, you could expand this to use a !
in a way that is analogous to :w!
(forcefully write) with the -bang
option:
command! -nargs=1 -bang Move :saveas<bang> <args> | call delete(expand('#'))
In this case, <bang>
will be expanded to !
if the command was executed with !
, otherwise it will expand to nothing.
Relevant help:
:h :saveas
:h :_#
:h :command-nargs
:h <args>
:h :command-bang
:h <bang>