49

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/?

1
  • @ElderGeek, this worked beautifully, if you make that an answer I will accept it.
    – auden
    Nov 10, 2016 at 22:41

8 Answers 8

66

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.

1
  • :wq path/filename.txt
    – Adam
    Feb 5, 2023 at 1:05
22

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.

1
  • 1
    Yes, 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
    Aug 17, 2013 at 14:25
12

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

3
  • You should probably explain straight in the answer the difference between :sav and :w, it's quite useful to know. Nov 11, 2016 at 8:55
  • 1
    @MatteoItalia good point. Done.
    – Elder Geek
    Nov 11, 2016 at 12:56
  • Perfect, upvoted! Nov 11, 2016 at 13:06
8

I believe you want to try something like this. (don't forget the double slash at the end.)

:w /var/www//%:t
5
  • 1
    Yes I tried something like that and it worked. Thanks
    – berga007
    Aug 17, 2013 at 14:35
  • 1
    @berga007 please don't post a "thank you" comment, instead accept the answer.
    – guntbert
    Aug 17, 2013 at 15:44
  • 1
    It worked! Why do you need the double slash? Jan 20, 2022 at 20:16
  • @DrFabioGori For escaping the %
    – thedemon
    Mar 21, 2022 at 18:55
  • Maybe there are circumstances where it's necessary that I am not aware of, but whenever I need to write a file to a different directory, %:t works just fine without the extra /. Nov 24, 2023 at 18:56
7

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
3

If you created a new file with Vim, pressing the key sequence Esc-:-w-q-Enter will save the file to the current location where you launched Vim. For example, if you were at /home/$USER the file will be created under this directory. The easy way is to launch vim using:

vim myFile.txt

This will create a new file, or overwrite a file with name myFile.txt in the current location.

2

On your launcher the second icon is a picture of a filing cabinet. This is called Nautilus (Ubuntu's File Manager).

Select Nautilus and your home directory should open. You should see your file there.

Right click on your file and select 'copy'. Open your email, compose a new message, click on the message body. Then 'right click' and this time select 'Paste'.

These instructions work for Ubuntu 14.04 and 16.04 but I don't know if 12.04 includes Nautilus.

3
  • 2
    12.04 includes Nautilus. See here: packages.ubuntu.com/search?keywords=nautilus
    – wjandrea
    Nov 11, 2016 at 6:47
  • 4
    -1 This answer has nothing to do with Vim. I think you posted a legit answer on the wrong question.
    – wjandrea
    Nov 11, 2016 at 6:49
  • @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. Nov 11, 2016 at 11:33
1

If the filepath and filename you are trying to save to has no spaces then camsolo's answer (:w /var/www/filename) will work for you. If the filepath/filename has space(s) in it then you will need to escape those space to save it there. I have noticed that all or some versions of vim will not save to a filepath when quotation marks are in the command.

This will not work: :w "/var/www/pa th/file name"

This will work: :w /var/www/pa\ th/file\ name

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .