tags:

views:

1061

answers:

5

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?

+8  A: 

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

innaM
This works perfectly, even with rails.vim
Sam Saffron
+1  A: 

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

DBMarcos99
This will copy the file, not rename it.
innaM
Ah - I misread "I would like to continue editing person.haml_spec.rb" My bad!
DBMarcos99
I suppose you could :! cp foo foo.new and then :e foo.new ?
DBMarcos99
+11  A: 

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 <F1> in the explorer will give you a better overview.

soulmerge
Thx, Explore gets the job done, it is however a little awkward
Sam Saffron
+4  A: 
  • Write the file while editing - :w newname - to create a copy.
  • Start editing the new copy - :e#.
  • (Optionally) remove the old copy - :!rm oldname.
gimel
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
Nice. Apparently Ctrl-6 does the same as :e#
glenn jackman
+1  A: 
sav person.haml_spec.rb | call delete(expand('#'))
Maxim Kim