52

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.

54

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.

| improve this answer | |
  • 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 '19 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? – Nicholas Cousar Aug 29 at 16:38
16

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
| improve this answer | |
  • 3
    Typing straight :edit %:p:h also seems to work. – Mladen Jablanović Feb 11 '16 at 8:13
  • :E will also open netrw – wisbucky Sep 12 '19 at 22:52
  • I use :ex<tab> which expands to :Explore. – Robert Jul 10 at 16:21
7

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>
| improve this answer | |
  • 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 – user907860 Jul 6 at 0:19
4

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.

| improve this answer | |
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>
| improve this answer | |
0

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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