Vim editor, how can I save a file in other directory

You can enter :pwd to display the current working directory. This is where your file will be saved if simply enter :w filename. You can change the working directory with :cd path/to/new/directory. Or you can enter the full path to the location where you want to save the file with the write command, e.g., :w /var/www/filename.


The w vim command supports as parameter the filename, that can contain a path, so

:w /var/www/filename

should work, provided you have permissions to write to that directory.
You could also use tab completion to build the pathname.

The bare command :w only works if you started vim giving it a filename already.


Navigate to the directory you want to save the new file to, open the file you wish to edit and then use

Esc:sav newfilename or Esc:w newfilename That should work for you.

For more on tips with vim you might find this cheatsheet useful.

Edit as requested.

:sav saves the file with a new name and opens the new file in Vim.

Note: :sav won’t close the initial buffer, it will hide it. By default, hidden buffers are unloaded.

:w save the file with a new name but keeps the original open for editing.

Edit source: https://stackoverflow.com/questions/4980168/how-to-save-as-a-new-file-and-keep-working-on-the-original-one-in-vim

Tags:

Vim