35

A pattern I seem to be following right now is:

  • edit a file until I like it
  • :w another-file to use it as a starting point for another-file
  • :e another-file to polish up another-file

Is there an existing ex command to do the latter two steps at once? Something like :writeAndEdit another-file?

I can fake one using vimscript, I just want to know if there's an existing command.

rampion
  • 82,104
  • 41
  • 185
  • 301
  • 1
    Good question. I use that method too, and it's very error prone. – Judge Maygarden Apr 28 '11 at 21:07
  • 2
    Vim questions belong here. This was voted on in meta long ago. – Randy Morris Apr 28 '11 at 21:44
  • 1
    What I used to do was :w newfile and then :[UPARROW][HOME][DEL]e[ENTER] which takes the last command and changes it from a :w to an :e so that your chance of a typo is much less. Still two steps just less error prone. Now that I know about saveas I will be using that but I thought I would mention it here because the idea could be useful for other tasks. – stephenmm Apr 28 '11 at 23:38
  • @Randy I did not realize that. Thanks. – Judge Maygarden Apr 29 '11 at 03:38

2 Answers2

51

Could :saveas do the job?

:sav[eas][!] [++opt] {file}
            Save the current buffer under the name {file} and set
            the filename of the current buffer to {file}.  The
            previous name is used for the alternate file name.
Vincent Guerci
  • 14,094
  • 4
  • 47
  • 55
12

You are less likely to overwrite your original file if you open the new file first and then copy the original file into the buffer.

:e another-file
:r original-file

I found an answer for you. Type :help saveas and enjoy!

:sav[eas][!] [++opt] {file} Save the current buffer under the name {file} and set the filename of the current buffer to {file}. The previous name is used for the alternate file name. The [!] is needed to overwrite an existing file. When 'filetype' is empty filetype detection is done with the new name, before the file is written. When the write was successful 'readonly' is reset. {not in Vi}

Judge Maygarden
  • 25,264
  • 8
  • 76
  • 98
  • 2
    @rampion not according to the timestamps, it looks like Judge was 1s quicker! –  Dec 11 '14 at 10:32