16.4 Saving Files

There are several ways to save files in vi. If you want to save the current buffer to the file randomness, you would type:

:w randomness

Once you've saved the file once, saving it again is as simple as typing :w. Any changes will be written out to the file. After you've saved the file, you are dumped back into command mode. If you want to save the file and quit vi (a very common operation), you would type :wq. That tells vi to save the current file and quit back to the shell.

On occasion, you want to save a file that is marked as read-only. You can do this by adding an exclamation point after the write command, like so:


However, there will still be instances where you cannot write the file (for example, you are attempting to edit a file that is owned by another user). When this happens, vi will tell you that it cannot save the file. If you really want to edit the file, you'll have to come back and edit it as root or (preferably) the owner of that file.