Say I'm editing file foo. I want to copy/write what I have in the buffer to bar and change the buffer to be editing bar instead of foo. I can achieve this with:

:w bar
:e bar

But that has a few problems

  1. If bar is actually /usr/local/share/long/path/to/bar, I really don't want to type that in twice, even with tab completion.
  2. It reloads the file, potentially messing with the settings/folds/etc. I had for that buffer.
  3. The working directory is left the same.

1 is the biggest problem I'd like a solution to address; 2 would be really helpful, 3 is more of a "nice to have."

Is there a cleaner way to do this?

  • I think this is what you want? How can I rename the file I'm editing?. It doesn't do anything about the working directory, but you can fix that with :cd %:p:h. Commented Feb 20, 2015 at 23:57
  • You can define a function yourself that does this, called e.g. :we -- is that what you're looking for?
    – yo'
    Commented Feb 21, 2015 at 0:06

3 Answers 3


Here's a relatively simple solution:

:saveas bar

This solves problems #1 and #2. It doesn't address problem #3, however.

Note that this still leaves the old file open in another buffer. For example, :w foo<cr>:saveas bar<cr>:ls<cr> will list two buffers, foo and bar.

  • Beat me by twelve seconds!
    – wchargin
    Commented Feb 21, 2015 at 0:22
  • Indeed, saveas is what I was looking for. I don't suppose there's an option to have it open in a new window, is there?
    – Kevin
    Commented Feb 21, 2015 at 0:31
  • @Kevin By "window," do you mean split or tab?
    – Doorknob
    Commented Feb 21, 2015 at 0:32
  • Split, preferably vertical (or an option for both)
    – Kevin
    Commented Feb 21, 2015 at 0:33
  • 1
    @Kevin :vs (with no argument) will split the window, having the same file in both buffers. You can then choose one of the buffers and use :saveas bar.txt, it will leave the 2nd buffer intact.
    – yo'
    Commented Feb 21, 2015 at 1:00

If you are working on a file that is not in the current working directory, and you want to save it under a new name in that directory, you can use the following:

execute 'saveas' expand('%:h') . "/new-file-name"

The execute command allows you to use an argument to saveas that is not a literal string. expand('%:h') gets the relative path of the current file. The rest of the statement concatenates the new file name.

  • 1
    Welcome to Vi and Vim!
    – D. Ben Knoble
    Commented Sep 14, 2021 at 17:39
  • Binding this with a map and it works like a charm!
    – goetz
    Commented Nov 21, 2021 at 21:03
  • who not just :sav %:h/new-file-name?
    – rofrol
    Commented Jul 7, 2023 at 8:13
  • I would like to know the difference btw these two, too!
    – EmilBB
    Commented Oct 11, 2023 at 8:32

Write the file as usual: :w bar and press Ctrl+^ (or Ctrl+6) to alternate the file.

For splits (:ba/:vert ba), check: How to convert all windows into horizontal, vertical or tab splits?

See: :help ctrl-6.

  • Definitely helps avoiding typing out the name twice, but does reopen like :e.
    – Kevin
    Commented Feb 21, 2015 at 0:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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