Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

From time to time I want to rename a file while editing it. For example from .html to .xhtml or so.

To do that in Vim, I must do

:saveas new_file
:!rm old_file

Is there a built-in command, that allows me to get rid of the :!rm part? It's annoying to re-type the old path and filename.

share|improve this question
up vote 3 down vote accepted

You could always define your own. E.g., put this in your .vimrc:

" First define a function
function! MoveFunction(newname)
    " get the current file name
    let a:oldname = expand("%:p")
    " save under the new name
    exec "saveas " . a:newname 
    " delete the old file
    call delete(a:oldname) 
" Next define a command
command! -nargs=1 MoveTo call MoveFunction(<f-args>)


:MoveTo <new-name>

should do what you want.

share|improve this answer
Yes, I feared, that I'd have to. Thanks for the solution! – Boldewyn Sep 24 '10 at 11:57

No, there is no command that will rename both the file on-disk and the buffer at the same time. There are some ways to reduce the amount of typing you have to do, though. For example, Vim will expand % to the name of the current buffer/file, so you could use

:!mv % new_file

If new_file has the same root name as old_file, you could do this to change just the extension:

:!mv % %:r.xhtml

where %:r expands to the name of the current buffer/file without the extension. See

:help filename-modifiers

Once you have the file name changed on-disk, you can change your buffer to that name with

:e new_file


:f new_file

Both commands have file name completion, so you can type just the first few letters of the file name, then <tab> to have Vim complete the name.

share|improve this answer
Thanks for the explanation. – Boldewyn Sep 24 '10 at 11:58

You can just use

:!mv old_file new_file

You have to save current buffer first, btw!

share|improve this answer

There's also the Rename.vim plugin:

share|improve this answer

(now?) There is also the rename() function, but then you'll have to track any buffer pointing to the old-filename to apply a :filename.

share|improve this answer
rename() function has been available since Vim 5.5 (see :h version5) – Christian Brabandt Oct 16 '14 at 8:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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