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.
6 Answers
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.
-
1One 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.– wisbuckySep 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
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
-
3
-
1
-
-
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>
-
1If those who use this snippet need to delete the buffer with the old file, you can add
exec ':bd ' . old_file
after thesilent !rm …
line– d.kJul 6, 2020 at 0:19
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.
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
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