71

Is it possible to rename the file I'm editing from within Vim? Currently what I do is exit Vim, rename the file and open from Vim again.

3

6 Answers 6

70

You could save the file under a different name using :w newname. But this operation won't remove the old file and the 'old' file stays in your buffer, so any changes will be applied to your 'old' file.

:saveas saves your new file and opens it in a new buffer. But it doesn't delete the old file.

I use tpope/vim-eunuch to :Move files.

:Move: Rename a buffer and the file on disk simultaneously.

3
  • 1
    One downside of :w or :saveas is that it will lose the execute permission if you were editing a script. netrw Rename will keep the permissions.
    – wisbucky
    Sep 12, 2019 at 23:01
  • Is :Move effetively the same as :!mv {src} {dest}, and then running :e {src}, where {src} is the name of the old buffer which consequently is destroyed seeing as you will get `E211: File {src} no longer available, and where {dest} is the name of the new buffer? Aug 29, 2020 at 16:38
  • If most of the time all you do is rename files, then sure, VS Code perhaps does a better job. But if most of the time you edit code or text, then you might want to check the VIM motions, commands, and other features that beat VS Code and potentially all other editors out there hands down. Watch this video to get an idea of what I mean: youtu.be/X6AR2RMB5tE May 14 at 9:21
17

You could drop to Netrw and rename the file there.

If the file you're editing is in the current directory, then do:

:edit .

Navigate to the file, press R, and change the name. Press Enter to edit the file.

There's a caveat though: the original buffer remains in the list of buffers. If you switch to it, it's empty.

If the file you're editing is in a different directory, you can change to the file's directory with:

:cd %:p:h

If you don't want to change the directory for the entire Vim session but only for the current buffer, then you can do instead:

:lcd %:p:h
4
  • 3
    Typing straight :edit %:p:h also seems to work. Feb 11, 2016 at 8:13
  • 1
    :E will also open netrw
    – wisbucky
    Sep 12, 2019 at 22:52
  • I use :ex<tab> which expands to :Explore.
    – Robert
    Jul 10, 2020 at 16:21
  • This should be the accepted answer.
    – amit kumar
    Mar 23, 2022 at 7:21
12

I've been using the Rename2 plugin for this for years. It renames both the current buffer, and the file on disk:

:Rename {newname}

EDIT: I found this a .vimrc file on github:

function! RenameFile()
    let old_name = expand('%')
    let new_name = input('New file name: ', expand('%'), 'file')
    if new_name != '' && new_name != old_name
        exec ':saveas ' . new_name
        exec ':silent !rm ' . old_name
        redraw!
    endif
endfunction
map <leader>n :call RenameFile()<cr>
1
  • 1
    If those who use this snippet need to delete the buffer with the old file, you can add exec ':bd ' . old_file after the silent !rm … line
    – d.k
    Jul 6, 2020 at 0:19
5

The best way is to use :Move, as OrangTux said. For completeness, you can also perform terminal commands in vi editors using a bang (!) in command mode.

:!mv {current_file} {new_name}
:e {new_name}

However, this method doesn't remove the old buffer from your buffer list.

0
5

An easy way to rename file in Vim is :Explore command. Navigate to file which you want to rename type R command than rename file

0

You can just shell command from vim. A bit dirty since the old buffer is still in place but with NerdTree plugin it's good enough for me:

!mv <current path> <new path>
1

Your Answer

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

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