VIM is a short form of Vi Improved. It is a free, open-source text editor and can be installed on any operating system whether on Windows or Linux operating systems. It can be used in CMD (command line) mode as well as a GUI (graphical user interface). It is very flexible and reliable to use and we can edit any code or any script easily in it.
Let’s go through the first step to learn vi / vim to save and close files. This tutorial will help you to understand, how to save files in Vi/Vim and quit from the editor.
How to Save Changes in Vim
Let’s understand save a file in Vim (Vi) and continue editing. Using the below steps you can save any changes made in a file in Vim editor. The file will remain in editing mode.
- Press
ESC to switch to command mode. - Type
: (colon) to start command prompt at bottom-left of the editor. - Type
w just after the colon symbol and press Enter.
The above process will save changes in the current vi/vim editor. Your file will remain open in the editor for further changes.
Vim Save File and Quit
In this part, you will learn, how to save changes in Vim without quitting the editor.
- Press
ESC to switch to command mode. - Type
: (colon) to start command prompt at bottom-left of the editor. - Type
wq just after the colon symbol and press Enter.
The above process will save changes in the current vi/vim editor. Your file will remain open in an editor for further changes.
Vim Quit Editor Without Saving Changes
Sometimes, you found that the changes you made are not correct and just want to quit the file without saving those changes. Use the below instructions to discard any unsaved changes in vim editor.
- Press
ESC to switch to command mode. - Type
:q! to quit the editor discarding any unsaved changes
But, in case you are not sure and don’t want to lose your content, follow the next instructions.
Save the Changes to a New file in Vi/Vim
Everyone wants to be at save file and doesn’t want to lose their hard work. If you think, your changes is useful but not in the current file. Then save the changes to a different file without closing vim editor. Follow the instruction below.
- Press
ESC to switch to command mode. - Type
:w new_file_name to save changes to other file.
Other Save and Quit Commands in Vi/Vim
In the above methods, you learned about save files and quit from vim editor. There are some other methods to handle other scenarios.
The first two steps will remain same as above:
- Press
ESC to switch to command mode. - Type
: (colon) to start command prompt at bottom-left of the editor.
Now, use one of the below commands as per your requirements.
Command | Meaning |
---|---|
:w | Write changes to file without closing editor. |
:wq | Write changes to the file and close editor |
:wq! | Forcefully write changes to file and quit even if file has only read permission |
:q | Quit the editor (Only if not changes has been made to file) |
:q! | Quit the editor with dicarding any changes made in file. |
:w new-name | Save file contnet with a new file name. |
:x | Close file with save changes. |
Conclusion
There are a lot of editors like Nano, gedit, Jed editor, etc. but the flexibility and reliability of the VIM editor are so high that it is being recommended. In this article, we learn how to install the VIM editor as well as how we can exit the editor either by saving or without saving the file.