If you’re coming from macOS to Ubuntu, you might be familiar with the Vim text editor. Ubuntu 20.04 doesn’t come with Vim installed out of the box. You need to perform the standard procedure of updating your packages database, getting the Vim package, and then installing it.
Vim is an open-source and easy-to-use text editor. It is available on almost all of the Linux distributions available today. It comes with comprehensive documentation and an extensive community to help guide you through any questions you might have. You can view and edit the contents of any configuration file via Vim. It is extensible and customizable to a great extent, offering you functionality no one can compete with. It even comes with portable configurations that you can use on all your Linux Computers.
So, with the benefits out of the way, let’s take a look at how to install Vim in Ubuntu 22.04 and use it.
Installing Vim on Ubuntu
To start the process, you need to run:
$ sudo apt update
Running this will update the packages database, and make it easier for the system to locate the Vim package for installation.
Once you get the prompt back, you are ready for the next step.
Depending on the available space, you can either install “vim” or “vim-tiny”. You can use respective commands to install both
$ sudo apt-get install vim $ sudo apt-get install vim-tiny
For this guide, I will be installing the “vim” package.
Vim will take up 34.6 MB space on your hard drive after installation. Once you confirm that you want to proceed, Ubuntu will start fetching and installing the package components. As always, once you get the prompt back, it indicates that the installation has completed.
Verifying the installation
Once the installation has completed, you can go and run the following to get more information about the version installed:
$ vim –-version
You can see the version and the included patches:
This detail is followed by all of the included options that you can use with your Vim installation.
Starting Vim Editor
To start Vim, you can type:
And it will launch the text editor.
When you start typing, it will start composing a new document for you.
Opening a file in Vim
Suppose you want to open a text file in Vim. For instance, I have a “test” text file on my desktop.
In a similar scenario, you need to run the following command:
$ vim test
When you press Enter it will open up your text file in Vim.
Changing a text file using Vim
Now that we have our file open, we can change it. Click on the terminal window and press “i”. You will be notified of the changed status at the bottom.
You are now in edit mode. You can edit the text to anything you prefer and fill up the file. For instance:
Saving a file in Vim
Now when you want to save your file, press “Esc” and it will bring you out of the edit mode. Now type:
This will save your changes to the file. If you want to save and close the file, you can type:
Executing this will save the changes to the file and close the vim editor.
Exiting without saving
You can discard your changes and exit the file without saving anything. This is our current content in the file:
Now let’s add a line:
Now press the “Esc” key and type:
Now when you open up the file again with “vim test” it will not have the added line:
Editing Configuration Files with Vim
Vim is one of the many versatile tools in the belt of a system admin. I wanted to add this section to highlight the fact that you can open up and edit configuration files with Vim. If you have found yourself asking this question on the internet “Which text editor can be used on Ubuntu 22.04 to view and edit the contents of a configuration file?”, Vim has everything that you need. Vim is a very versatile text editor which supports almost all of the text file types out there. Let’s check how to open up a configuration file.
Simply fire up your terminal and type the following to open up any configuration file (Let’s open up the sysctl file for this guide):
$ sudo vim /etc/sysctl.conf
Note that we are opening up a system configuration file, so Vim needs elevated privileges through sudo.
After providing the password, you will get the file
You can update and save this file using the same commands as before.
If, for any reason, you want to remove Vim from your system, you can execute the following to remove any traces of Vim from your system:
$ sudo apt-get remove vim
Seeing this prompt is the equivalent of getting asked, “Are you sure?” in Windows. Once you confirm the operation, it will proceed to completely remove Vim and all of its dependencies it. Once you get the prompt back, the uninstall operation has succeeded.
You can even run “vim” to confirm this.
If you have followed these steps, you now know the complete lifecycle of Vim text editor in Ubuntu 22.04. To explore Vim further, navigate to their website at https://www.vim.org/. It contains all the documentation needed and guides for extending and developing plugins.