Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

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?

like image 715
Rook Avatar asked Jan 06 '12 02:01

Rook


People also ask

How do you name a file in Vim?

It's time to open up the command mode of Vim by pressing the “:” key. Now, press the “w” key from the keyword followed by the space key and the new name of a file.

How do I rename a file in Vim?

Use :bd # if you also want to delete the older file from the buffer list. If you want to use a quick command to rename the file, add a new file under ~/.vim/plugin with the following contents: The command Rename will help you to quickly rename a file.

What is the difference between save and move in Vim?

:saveassaves your new file and opens it in a new buffer. But it doesn't delete the old file. I use tpope/vim-eunuchto :Movefiles. :Move: Rename a buffer and the file on disk simultaneously.

How do I rename a file in Linux?

You could drop to Netrwand 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 Enterto edit the file. There's a caveat though: the original buffer remains in the list of buffers.

How do I change the name of a file in Windows?

Fire up File Explorer by pressing Windows+E, and navigate to a directory with either a file or folder to rename. Click on a file or folder to select it, and click “Rename” from the Home menu at the top of File Explorer.


Video Answer


2 Answers

:saveas newname will 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

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

The user manual provides a thorough description of how to create user commands. Here's an explanation of the elements I'm using above.

  • -bang allows the command to called as either Rename or Rename! 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 :saveas command.
  • -complete=file will let you tab-complete the path that will be used for the new file, similar to :e and :saveas do.
  • -nargs=+ specifies that :Rename requires 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 :saveas accepts, so you could do something like :Rename ++enc=latin1 newfile to rename the file to newfile and change the encoding to latin1.
like image 134
jamessan Avatar answered Nov 15 '22 10:11

jamessan


Tim Pope has a plugin that has a function :Rename that does this: vim-eunuch.

You can also do the following sequence of steps:

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

of course this is not as nice as renaming the file in the shell.

like image 43
skeept Avatar answered Nov 15 '22 09:11

skeept