The most straightforward way to save a file in vi is to enter :w in Command mode. You save the file and can continue to edit it.
Saving and quitting a given file is pretty simple, too. In Command mode you enter :wq. If you find yourself as the root user being unable to write to a configuration file, entering :wq! forces the file to be written, as long as the only barrier is a missing write permission.
Two additional methods of saving and quitting a file are available. The first entails entering :x to save and exit. The second is to press Shift+ZZ (hold down the Shift key and press the Z key twice); this is my personal favorite and is easy to remember.
Read questions carefully when asked about saving and/or exiting the vi editor. Many test-takers have mentioned that the question's syntax is critical to getting it right. For example, the question, "Which of the following saves and exits a file in vi?" is very different from the question, "Which of the following can be used to save or exit a file in vi?"