418

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?

3
  • 42
    I came with the opposite question. Commented Jan 13, 2017 at 16:36
  • 11
    @cambunctious Me too. I was googling vim "save as" and this question taught me that there is a :saveas that works just like I expected: saves the existing file with a new name and opens the new file. Commented Mar 29, 2019 at 18:41
  • Great addition! By the way, you can do :sav for short and there's also :sav! when overwriting an existing file. Commented Oct 16, 2021 at 19:35

4 Answers 4

596

Use the :w command with a filename:

:w other_filename 
Sign up to request clarification or add additional context in comments.

8 Comments

Furthermore, use :w %:h/other_filename to write to a filename in the same directory as the open file.
@Ioevborg when is that not the case? I just :w fname without reading your comment and the behavior seems to be the default.
@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 vim.wikia.com/wiki/Set_working_directory_to_the_current_file
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....
It may be useful to mention that :w other_filename will write data to other_filename only and not the current file.
|
304

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.

3 Comments

: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).
So, yeah...:sav is closer to the "Save as" I've known.
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.
30

After save new file press

Ctrl-6 

This is shortcut to alternate file

2 Comments

For what it's worth, using nvim 0.1.0 under OS X 10.10.5, this requires SHIFT-CTRL-6.
shift-ctrl-6 on a Homebrew version of Vim on OS X 11.
7

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 # 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.