394

Whenever I use the :sav command, it saves the file with a new name and opens the new file open in Vim.

Is it possible to save the file with a new name but keep the original one open for editing?

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
vimguy
  • 3,949
  • 2
  • 15
  • 3

4 Answers4

566

Use the :w command with a filename:

:w other_filename
wilhelmtell
  • 57,473
  • 20
  • 96
  • 131
  • 91
    Furthermore, use `:w %:h/other_filename` to write to a filename in the same directory as the open file. – loevborg Nov 21 '13 at 13:04
  • 3
    @Ioevborg when is that not the case? I just :w fname without reading your comment and the behavior seems to be the default. – Blake Oct 11 '14 at 22:15
  • 13
    @Cokemonkey11 It's not default behavior in vim to have the current file's location be the same as the working directory. You probably have something in your vimrc file that is doing this for you. Some more information can be found here http://vim.wikia.com/wiki/Set_working_directory_to_the_current_file – Derek Jan 07 '15 at 21:27
  • 9
    I just confirmed that without the "%:h/" the file will be saved in the directory you were in when you opened vim, not where the original file was.... – RVC Jun 24 '16 at 11:33
  • 10
    It may be useful to mention that `:w other_filename` will write data to `other_filename` **only** and not the current file. – Ram Patra Jan 03 '17 at 12:26
  • You can see where vim would save by default with `:pwd` and you can change it with `:cd path` – cambunctious Mar 29 '19 at 20:06
  • @loevborg's comment is close but doesn't work for me. I have to use `:w %:p:h/other_filename`. – mplwork Aug 14 '19 at 16:25
  • 2
    @loevborg or `:w %` to expand the current name, then edit it – mykhal Jun 07 '20 at 09:56
297

Thanks for the answers. Now I know that there are two ways of "SAVE AS" in Vim.

Assumed that I'm editing hello.txt.

  • :w world.txt will write hello.txt's content to the file world.txt while keeping hello.txt as the opened buffer in vim.
  • :sav world.txt will first write hello.txt's content to the file world.txt, then close buffer hello.txt, finally open world.txt as the current buffer.
Jianwen W.
  • 3,919
  • 4
  • 18
  • 17
  • 29
    `:sav` won’t close initial buffer, it will hide it. By default, hidden buffers are unloaded, but this can be overriden (with 'hidden' or 'bufhidden' options). – ZyX Mar 29 '12 at 20:00
  • 7
    So, yeah...`:sav` is closer to the "Save as" I've known. – skytreader May 05 '14 at 03:56
  • 9
    From Vim's help: `:sav[eas][!] [++opt] {file}` So, `:sav` **is** the shortener for `:saveas`. Whereas, `:w` is the shortcut for `:[range]w[rite][!] [++opt] {file}`. And everything is in the manual, just few lines above/below. – Atcold Oct 20 '14 at 19:22
28

After save new file press

Ctrl-6

This is shortcut to alternate file

SergioAraujo
  • 11,069
  • 3
  • 50
  • 40
6

The following command will create a copy in a new window. So you can continue see both original file and the new file.

:w {newfilename} | sp #
tivn
  • 1,883
  • 14
  • 14