What would be the most practical way to rename the file you’re currently editing in Vim without messing up your current splits configuration?
Generally, one would need to … save the file under a different name, delete the original one, and re-open the new one without making a mess of the current layout.
Anyone have any idea how to do that in one command (function) or less?
:saveas newnamewill save the buffer with the new name, make that name the current buffer, and set the alternate buffer to the old file.:call delete(expand('#'))will then delete the file associated with the alternate buffer.You can easily turn that into a command with something like
The user manual provides a thorough description of how to create user commands. Here’s an explanation of the elements I’m using above.
-bangallows the command to called as eitherRenameorRename!and<bang>in the constructed command is replaced by either an empty string or!, depending on how it is called. This is used to support the same functionality in the:saveascommand.-complete=filewill let you tab-complete the path that will be used for the new file, similar to:eand:saveasdo.-nargs=+specifies that:Renamerequires at least one argument (the filename), but can take more.<args>is replaced with whatever arguments are given to:Rename. This allows you to specify the extra arguments that:saveasaccepts, so you could do something like:Rename ++enc=latin1 newfileto rename the file to newfile and change the encoding to latin1.