After activating command mode, you can use commands to save your work. The most common way to do so is by using the :wq! command. With this command, several tasks take place at once. First, a colon is used, just because it is part of the command. Then, w is used to save the text you have typed so far. Because no filename followed w, the text will be saved under the same filename that was used when opening the file. If you want to save it under a new filename, just enter the new name after the w command. Next in the :wq! command is q, which will cause the editor to quit after saving the file. Last, the exclamation mark is used to tell vi that it shouldn't complain but just do its work. Since vi has a tendency to be smart with remarks like "A file with this name already exists," you are probably going to like the exclamation mark.
As you have just learned, you can use :wq! to write and quit vi. You can, however, also use just parts of this command. For example, use :w if you just want to write the changes while working on a file without quitting it, or use :q! to quit the file without writing changes. The latter option is a nice panic key if something has happened that you absolutely don't want to store on your system. This is useful because vi will sometimes do magic to the content of your file when you have hit the wrong keys by accident. However, an alternative exists; use the u command to undo the last changes you made to the file.
Was this article helpful?