Renaming the current file in Vim?

There's a little plugin that let's you do this.

Up vote 49 down vote favorite 13 share g+ share fb share tw.

How should I go about renaming my current file in vim. For example: I am editing person. Html_erb_spec.

Rb I would like it renamed to person. Haml_spec. Rb I would like to continue editing person.

Haml_spec. Rb How would I go about doing this, elegantly? Vim link|improve this question asked Jul 30 '09 at 8:46Sam Saffron?30.6k1084221 81% accept rate.

This works perfectly, even with rails. Vim – Sam Saffron? Jul 30 '09 at 9:25 code:Rename %:p:h/_new_file_name.html.

Erb will save it in the same directory as the file being renamed. – Mike May 5 '11 at 19:34 :Rename will move a file from its location to the CWD, so make sure you set the CWD first or prefix the new filename with the proper subdirectory. – Thomas Hunter May 14 '11 at 20:11 2 This updated version of Rename.

Vim handles CWD correctly - always saves file in the directory of original one. – sickill Sep 4 '11 at 17:56 @sickill Here's an even better version IMO that tries to be smart, if the new file name contains a "/" it uses the full path, otherwise it assumes the current directory github.com/DelvarWorld/configs/blob/master/bundle/Rename/plugin/… – Andy Ray Dec 5 '11 at 20:13.

The command is called :saveas, but unfortunately it will not delete your old file, you'll have to do that manually. See :help saveas for more info. EDIT: Most vim installations have an integrated file explorer, which you can use for such operations.

Try :Explore in command mode (I would actually map that to a function key, it's very handy). You can rename files with R or delete them with D, for example. But pressing in the explorer will give you a better overview.

Jul 30 '09 at 9:19 1 Explorer trick is great, thanks. – Alan Peabody Mar 25 '11 at 20:56 1 :Explore works great! I'm getting rid of NERD-tree in favor of this!

– bzx Jul 18 '11 at 9:17 1 I don’t think that it is awkward compared to other editors where you normally can’t move a file. Instead you have to go the file manager of your choice, rename the file, and go back to your editor. If you’re lucky you have to quit and restart your editor if it didn’t got the filename change notification.

– Rafael Sep 7 '11 at 17:43 1 You might also like :Sex (shorthand for split explore :-) ) – Peter Jankuliak Dec 7 '11 at 3:03.

Write the file while editing - :w newname - to create a copy. Start editing the new copy - :e#. (Optionally) remove the old copy - :!

Rm oldname. On Windows, the optional 3rd step changes a little: (Optionally) remove old Windows copy - :! Del oldname.

I use this approach all the time, however, the downside is that you lose your undo tree - so you can't undo anything before the :e – rampion Jul 30 '09 at 13:07 2 Nice. Apparently Ctrl-6 does the same as :e# – glenn jackman Jul 31 '09 at 14:37 you can also open the directory, move the cursor to the file and press "D" to delete a file. – Peder Aug 3 '11 at 8:32 The more straightforward way imho is 1) rename the current buffer :f newname 2) save it :w 3) (optionally)!

Rm oldname. The advantage is that your undo history is preserved. – kynan Sep 20 '11 at 14:12 This approach doesn't work with windows :).

So I prefer no OS dependented solution. – nXqd Oct 11 '11 at 3:36.

Sav person. Haml_spec. Rb | call delete(expand('#')).

Nice! Didn't know about call delete(expand('#')) – dorkitude Aug 18 '11 at 23:40.

I don't know if this is the "easiest" method, but assuming you've already saved your file (:w) I would invoke the shell (:sh) and do a simple cp foo foo. Bak To go back to editor use Ctrl-D/Exit. Useful list of vi editor commands on this link.

This will copy the file, not rename it. – innaM Jul 30 '09 at 9:04 Ah - I misread "I would like to continue editing person. Haml_spec.

Rb" My bad! – DBMarcos99 Jul 30 '09 at 9:07 I suppose you could :! Cp foo foo.

New and then :e foo. New? – DBMarcos99 Jul 30 '09 at 9:11.

How about this: :! Mv oldfilename newfilename | e newfilename Worked for me!

Hmmm, I should clarify that it worked on MacVim yesterday but I was sshed into a server and tried it today and the 2nd command didn't seem to be treated as a colon command... – murftown Dec 19 '11 at 17:32 Actually now it isn't working on MacVim either. Back to the drawing board! :) – murftown Dec 19 '11 at 18:34.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions