Vim editor, how can I save a file in other directory
Solution 1
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
.
Solution 2
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.
Solution 3
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
Solution 4
I believe you want to try something like this. (don't forget the double slash at the end.)
:w /var/www//%:t
Solution 5
Inside Vim, no matter where the file is currently saved, you can give the path to the new location where you want to save it. For example, to save the file on your Desktop:
press esc to go into normal mode, then type
:w ~/Desktop/filename
This works for any path where you have permission to write on the directory. If you want to save somewhere where you don't have write permission, you can do this:
:w !sudo tee /path/to/my/filename
Related videos on Youtube
berga007
Updated on September 18, 2022Comments
-
berga007 over 1 year
I'm new in the world of ubuntu and vim editor.
My question is: how can I save a file on my localhost using vim?
When I use the command:w
I save the file, but where? And how can I change the file location to/var/www/
?-
Auden Young over 7 years@ElderGeek, this worked beautifully, if you make that an answer I will accept it.
-
-
berga007 over 10 yearsYes, when I use the :w command I had already given a name to the file... I tried to save the file into my localhost directory using the tip you gave me and it worked! Thank you very much, if one day I could help you I won't hesitate!
-
berga007 over 10 yearsYes I tried something like that and it worked. Thanks
-
guntbert over 10 years@berga007 please don't post a "thank you" comment, instead accept the answer.
-
wjandrea over 7 years12.04 includes Nautilus. See here: packages.ubuntu.com/search?keywords=nautilus
-
wjandrea over 7 years-1 This answer has nothing to do with Vim. I think you posted a legit answer on the wrong question.
-
Matteo Italia over 7 yearsYou should probably explain straight in the answer the difference between
:sav
and:w
, it's quite useful to know. -
WinEunuuchs2Unix over 7 years@wjandrea At the time I wrote this on my phone the OP had a multi-part question "or perhaps email it to myself". This answer tells OP how to handle the email portion of the question. Thanks for commenting on why you down-voted. Not everyone extends that courtesy.
-
Elder Geek over 7 years@MatteoItalia good point. Done.
-
Matteo Italia over 7 yearsPerfect, upvoted!
-
bkaankuguoglu about 2 yearsIt worked! Why do you need the double slash?
-
thedemon about 2 years@DrFabioGori For escaping the
%