5

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. – Martin Tournoij 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
5

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 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.

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

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.