How to save a file with a new name in VIM when switching to this new buffer (and closing the original)

Cover name: I know that

w! newFileName 

will write to newFileName, continuing to edit the original file.

But I want

  • write to newFileName
  • Open new new filename in current buffer
  • (Therefore, the value: close the source file without making updates)

Thanks.

+6
source share
2 answers

The sav team should do what you want.

+14
source

Another way to do this is :w newName and then CTRL-^ (which is the same as CTRL-6 ) to switch to the new name.

When you :w to a new name, it sets this as an "alternative" file name and CTRL-^ switches it to primary. See :help alternate for more information on this. Also useful :f newName , which simply renames the buffer (keeping the old name as an alternative) without saving anything.

This is a little better for me than the command :saveas , because it does not introduce a new command, but only a new shortcut, which is sometimes useful in other contexts. It is always surprising to me that switching to a new name is not the default behavior :w newName when I meet it; I find it difficult to think of a precedent when existing behavior would be preferable.

+2
source

Source: https://habr.com/ru/post/989866/


All Articles