Vim (Vi Improved) is an open-source text editor designed for Unix and Linux systems. It is a robust and effective tool for editing and writing text in the Command line interface mode.
In addition, Vim is very popular among programmers, and sysadmins since they frequently use it to edit configuration files and programs written in C/C++, Python, and PHP.
Vim has several modes of operation, so it has a learning curve, and it might take some time to get used to it. However, once you’re familiar with it, Vim can be a handy tool for your day-to-day Linux operations.
But how can you save files in Vim? Let’s check ways in which you save a file in a Linux or Unix-like system using the Vim text editor. For this operation, you only need to have sudo or root privileges.
Open a File in Vim
When you open the Vim editor, you are in normal mode. You can navigate through the file and perform vim commands in this mode.
To insert text, you must enter insert mode by hitting the i key. You can add and remove characters in this mode just like you would in a standard text editor.
If you want to go back to the command mode from the insert mode, simply press the Esc key.
To open a file using Vim, open your terminal and type vim followed by the file you want to create or edit:
vim myfile.txt
To run any command, you must be in command mode, press the i key to enter insert mode.
Save a File using Vim
These are some of the hacks you can use to save files once you have entered your text:
:w – Save changes, but do not exit.
:wa: – Save all open multiple files without exit.
:w filename – Save the file with a different name.
First, press the Esc key and use any of the above commands to save the file, In the screenshot below, I have used :w(filename) to save the file using a new name called text.txt.
In addition, if you’ve made changes to a file in Vim and want to save those changes without exiting the editor, you can also use the :up command. It’s a convenient way to ensure your work is saved without leaving Vim.
Quit Vim Editor
There are ways you can use to quit the editor if you don’t want to save the files:
:q – To quit the editor.
:q! – To force quit (useful when you make changes to a file but you don’t want to save the file ).
:qa! – Quit all open files without saving changes (force-quit).
Press the Esc key and employ any of the above commands to quit the editor, In the screenshot below, I have used we used :q to quit the editor.
Save the File and Quit Editor
There are also some of the ways you can use to both save a file and quit the Vim editor.
:wq – This command saves changes and exits the editor
wq! – The command :w (writes out) and :q! (quit without saving)
In this case, press the Esc key and use any of the above commands to save the file and quit the Vim editor. In the screenshot below, I have used the :wq! command to perform this operation.
Note: In all of the above operations, you must press Enter in order to execute the commands.
Conclusion
You can save files efficiently in Vim by following these best practices, which can boost your productivity and workflow. Keep things simple by using shorter commands. With practice, you can save files in Vim like a pro.