To exit vim, you need to use the :q command. This will quit the current session as long as you have saved your work (that is, all text buffers are written to disk).
If you want to quit and save the current file to disk, use :wq. This works only if you have assigned a filename to the file you are working with. If you have not, you will see an error message. To remedy this, you can pass the name of the file you want to save with :wq filename. In the case of the example file we used previously in this section, entering :wq /home/justin/mynewfile will successfully save the file and exit vi cleanly.
To exit vim without saving the file, you can use :q!. With this command, you are not asked for confirmation and you will exit vim immediately. Use with caution.
Was this article helpful?