Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

As a sort of poor-mans version control, I need to save a file someFile.c as someOtherFile.c, but I want :w to continue saving as someFile.c. Is this possible?

The environment is Windows XP SP3, but I do have a Cygwin install here so I could do it in the Cygwin instance if that helps.

Thanks.

share|improve this question
    
Possible cross site duplicate of: stackoverflow.com/questions/4980168/… –  Ciro Santilli Jun 5 at 14:50

2 Answers 2

up vote 8 down vote accepted

Yes. If you use :w someOtherFile.c it will write to that file, but stay editing someFile.c.

Here's a list of vim cheatsheets that you might find useful: http://stackoverflow.com/a/442072/1553090

share|improve this answer
    
I cannot believe that this was the default behaviour! Thank you! –  dotancohen Apr 19 '12 at 0:46
1  
No worries... Neither did I, actually. I just did vim test, entered 'hello', then :w test2, appended 'world' and :w again. The file 'test' contained "hello world", and 'test2' contained "hello". –  paddy Apr 19 '12 at 2:18

You could put an autocommand like this one in your ~/.vimrc:

:au! BufWrite * execute "w" expand("%") . strftime(".%y%m%d.%H%M%S")

That will save a copy of the current file in a file of the same name but with the current date and time appended every time you save the original file. That's probably more than you want, but you could modify it to work with only certain file types, or put your copies in a special directory, etc.

I used to use something similar to make backups of certain often-changed configuration files.

share|improve this answer
    
Thank you Gary, I can find much use for this. –  dotancohen Apr 19 '12 at 0:47
    
I am trying to map this to <F2> but it is not saving. Might you see the issue? noremap <F2> execute "w" expand("%") . strftime(".%y%m%d.%H%M%S") Thanks for this wonderful tip. –  dotancohen Apr 19 '12 at 2:31
    
I didn't try your mapping, but my first thought is that you're missing a : in front of execute. The command argument to an autocommand is an ex command so no : is necessary, but the rhs of a mapping is just a sequence of keys, so a : is necessary to begin ex mode from normal mode. Also, you'll probably want to end your mapping with <CR> so that you don't have to press Enter each time you use it. You're welcome. –  garyjohn Apr 19 '12 at 7:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.