Vim is an improved adaptation of Vi - the standard Unix visual text editor. Vim is available on literally all Unix-like systems, including Linux and Mac OS.
Vim is quite fast and almost all functions have shortcut keys. Vim is a very useful text editor for programming/scripting and editing configuration files and more.
In this tutorial, we will show you how to save a file in Vim / Vi and quit the editor.
|1||Open File||Type vim filename|
|2||Edit File||Press i|
|3||Save File||Press ESC and type :w|
|4||To Quit||Press ESC and type :q|
|5||To Save and Quit||Press ESC and type :wq|
|6||To Quit Without Saving||Press ESC and type :q!|
Save a file in Vim / Vi
Firstly, run the command below to open Vim.
You should see a new blank text editor.
On most systems, when you run vi, it will also open Vim.
To open an existing file with Vim, run the command below instead.
$ vim filename
Note: If the existing file is not in the current working directory, you should include the file path. For example:
$ vim /home/shola/filename
Vim has two modes which allow you to perform different sets of operations. They are; Normal mode and Insert mode.
When you open Vim, you are in normal mode. This mode allows you to enter Vim commands and move around the file.
The Vim insert mode allows you to insert text into the editor. To switch from normal mode to insert mode, press i on your keyboard. Notice the -- INSERT -- cue at the bottom left of Vim.
Now, go ahead and enter some text into the editor.
To save your file, you would need to first switch back to normal mode. Press the ESC key to leave the insert mode and return to normal mode.
Next, type :w and press enter to save your file. You can think of this as writing your changes to disk.
Note: If you are working on a new file, you would be required to name your file. Below is an example.
You should see a message at the bottom of Vim indicating that your changes have been written.
Quit Vim / Vi
While in normal mode, enter the following command to quit Vim.
To quit Vim without saving your changes, the command you should run is;
Further, it is possible to save your file and quit Vim in one go as follows.
Alternatively, run the command below to save changes and quit Vim;
In this tutorial, we showed you how to save a file in Vim, as well as how to quit the Vim text editor. If you have any questions, we are happy to clarify them for you.