What is the easiest way to rename the file you are currently editing in Vim?

What would be the most practical way to rename the file you are currently editing in Vim without messing up your current partition configuration?

As a rule, you need to ... save the file under a different name, delete the original one and reopen the new one without creating a mess of the current layout.

Does anyone know how to do this in a single command (function) or less?

+5
source share
4 answers

:saveas newname saves the buffer with the new name, makes that name the current buffer and sets an alternative buffer to the old file.

:call delete(expand('#')) then delete the file associated with the alternate buffer.

You can easily turn this into a team with something like

:command! -bang -complete=file -nargs=+ Rename saveas<bang> <args> | call delete(expand('#'))`

. , .

  • -bang Rename Rename! <bang> !, , . :saveas.
  • -complete=file , , :e :saveas do.
  • -nargs=+ , :Rename ( ), . <args> , :Rename. , :saveas, - :Rename ++enc=latin1 newfile, latin1.
+12

:Rename, : vim-eunuch.

:

:saveas newfile
:bw <buffer_for_the_old_file>
:!rm old_file

, , .

+3

:Explorer :E, , r, .

+2

:Move, eunuch.

eunuch also provides other useful file operations, such as :Removesudoedit.

0
source

All Articles