Renaming files in Vim: A Vimscript experiment
I took an attempt at creating a Vimscript function to rename a file with a simple command. It was not that hard, but there are a few tricks that you need to learn to get started writing Vimscript. Let’s first look at the full code for the function, with a mapping to <leader>fr function! s:RenameCurrentFile( newName ) let l:file = expand('%') let l:currentFilePath = expand('%:p:h') let l:newFile = l:currentFilePath . '/' . a:newName " Save new file execute "saveas " . l:newFile " Open new file in a new buffer execute "e " . l:newFile " Kill old buffer execute "bdelete " . l:file " Remove old file call delete(l:file) endfunction command! -nargs=1 -complete=file RenameCurrentFile call s:RenameCurrentFile(<f-args>) nnoremap <leader>fr :RenameCurrentFile The first thing that had me bumped was how to use saveas. You have to execute a string that you create with the proper arguments, and there is no function for that. All commands that you would type after a colon (like edit, or bdelete, in this example) will have to be executed this way. ...