After you have finished editing a file, the next step is to save your changes and exit the editor. Vim provides several commands for this purpose, each with a specific function. All these commands are executed in Command-line mode, which you can enter by pressing :.
How to Save in Vim Editor
To save the changes you've made to a file without exiting, you use the write command. This is the fundamental answer to the question "vim how to save".
:w- Writes (saves) the current state of the file to disk.
Exiting Vim
If you want to quit the editor, you have a couple of options depending on whether you want to save your changes.
:q- Quits the editor. This command only works if you have no unsaved changes.:q!- Quits the editor and discards any unsaved changes. This is useful when you've made mistakes and want to revert to the last saved version of the file.
Vim How to Save and Quit
Combining saving and quitting is a very common workflow. The linux wq command is a staple for many developers working on the command line.
:wq- This command first writes (saves) the file and then quits. It's a two-in-one action for efficiency. Many users search forvi write and quit, and this command works for both Vi and Vim.ZZ- This is a shortcut equivalent to:wq. It saves the file if it has been modified and then quits. It's one character faster to type and a favorite among experienced Vim users.
Undoing and Redoing Changes
While editing, you might need to reverse an action or bring it back. These commands are used in Normal mode (press Esc to enter).
u- Undoes your last action.Ctrl-r- Redoes the last action that you undid.
Mastering these basic commands is the first step toward proficiency in Vim. As you become more comfortable, you'll find that these operations become second nature. For more advanced features, the official Vim documentation is an excellent resource.