8

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?

2
  • 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. Feb 20 '15 at 23:57
  • You can define a function yourself that does this, called e.g. :we -- is that what you're looking for?
    – yo'
    Feb 21 '15 at 0:06
6

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.

6
  • Beat me by twelve seconds!
    – wchargin
    Feb 21 '15 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
    Feb 21 '15 at 0:31
  • @Kevin By "window," do you mean split or tab?
    – Doorknob
    Feb 21 '15 at 0:32
  • Split, preferably vertical (or an option for both)
    – Kevin
    Feb 21 '15 at 0:33
  • @Kevin Not that I'm aware of, but you could always use, for example, <C-w><C-s>:saveas bar<cr>. (This could be all in one ex command if you want to make a mapping or something: :wincmd s|saveas bar.)
    – Doorknob
    Feb 21 '15 at 0:36
2

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.

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

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.

New contributor
Swoogan is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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