X

How to save and exit a file from Vim editor in Ubuntu linux

Dung Do Tien Aug 05 2020 1693
Vim (Vi IMproved) is an open-source text editor for Unix or Linux systems. It is a powerful text editor used in CLI commands. It is also a highly configurable text editor built to make creating and changing any kind of text very efficient.

Please note that in this article I only guideway to manipulation with Vim editor in Ubuntu Linux.

1. Installing Vim editor on Ubuntu.

By default, you need to open a terminal or login to the remote Ubuntu server by using the SSH client. To install Vim editor we need the following apt-get command.

First, we need to update all packages for apt-get.

sudo apt-get update

Finally, run the command below to install the Vim editor.

sudo apt-get install vim

To check Vim version or ready install success, using below command:

 sudo vim --version

2. Using Vim mode.

When editing a file by using Vim editor, there are two modes in vim we have to know to easily manipulate with the tex file.

- Command mode: In this mode, you can delete text, copy, paste, undo, redo, save, exit file. To switch this mode press the ESC key.

- Insert mode: In this mode, you can insert text or edit text and write anything inside the current file. To switch this mode press the i key.

3. Create a file using the Vim editor.

First, you need to select the folder you want to create a file and below is the syntax for creating a file by using Vim:

sudo vim [your_file_name.extention]

For example, I will create the demo.txt file inside the test folder, I will do as below :

And you can see your file opening in Vim editor, press i to switch to insert mode and you can write something for this file. Now you want to save and exit the file, you need to switch to command mode by pressing the ESC key and press :wq! and then press the ENTER key.

4. Open file to edit with Vim editor.

To open a file to edit in Vim, it looks like creating a file with Vim editor. When you enter the command sudo vim [your_file_name.extention] the program will check if this file name does not exist Vim will create this file and open file to edit and if it exists Vim will open it to edit. 

5. How to save changes in Vim editor.

5.1 Save and quit Vim editor.

To save file and quit in Vim editor:

Step 1: Press ESC key to switch to Command mode.

Step 2: Press : (colon) to open the command bar and insert a colon in the left bottom of the window.

Step 3: Enter more wq! and then press ENTER key to save and quit Vim.

Alternatively, we have another short way to save and exit Vim editor:

 :x

5.2 Save file but without quit Vim editor.

To save a file without exiting in Vim:

Step 1: Press ESC key to switch to Command mode.

Step 2: Press : (colon) to open the command bar and insert a colon in the left bottom of the window.

Step 3: Press w [current file name] and then press Enter key.

For example: :w demo.txt

5.3 Quit Vim editor but without save change file.

To exit Vim without saving changes:

Step 1: Press ESC key to switch to Command mode.

Step 2: Press : (colon) to open the command bar and insert a colon in the left bottom of the window.

Step 3: Press qa! and then press the Enter key.

Alternatively, we have another short way to exit without save file in Vim editor:

 :q!

6. Clone and rename file with Vim editor

To rename a file in Vim editor, you can refer to item 5.2. For example, you want to rename demo.txt file to text.txt file, You can do as below :

Change :w demo.txt to :w text.txt and then press Enter key.

Now we check all files in the current folder, we can see two files exist.

7. Summary

In this article, I only guide how to save and exit a file from Vim editor in Ubuntu Linux, also some other manipulation necessary with a file using Vim editor such as create a new file, clone, remane…

I hope this article is helpful to you.