Rename files in VIM

Here's a handy function that I use to rename files in VIM (works in MacVIM and Neovim too)

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
nnoremap n :call RenameFile()


NOTE: I've used which is mapped to comma in my config. but you can use whatever key combiniation you like.

1.07 Thousand
Ali

Get our stories delivered

From us to your inbox weekly.