Home Ubuntu How to install and use Vim on Ubuntu

How to install and use Vim on Ubuntu

by Abraham
install vim ubuntu

Vim is a powerful text editor that is preinstalled on most Linux distributions. Ubuntu includes Vim, vi, nano, Gedit (GUI), and numerous other text editors. Vim is an improvised version of the vi text editor. Ubuntu comes with Vim preinstalled. However, the preinstalled version is very basic.

To get the full version, we shall use the apt-get or apt command to install it on our Ubuntu operating system. Vim is capable of editing all types of plain text. It is beneficial for editing configuration files and programs written in shell, Python, Perl, C/C++, and other languages. This tutorial will demonstrate how to install and use the vim text editor on Ubuntu Linux.

We are utilizing Ubuntu 22.04, the most recent version of Ubuntu. Installation requires executing fundamental Linux commands on the Ubuntu command line (terminal). We used various methods for installing Vim. You may utilize any of these options that you deem fit.

Benefits of using the Vim editor

The vim editor’s features and benefits are highly respectable and advantageous; we will explore a few of them:

  1. Compatible with all system types for installation and execution
  2. It has two graphic and terminal modes
  3. Vim is a platform that uses minimum resources and is hence lightweight.
  4. Provides support for the feature of numerous tabs and the ability to execute several files
  5. Extensible through the use of innumerable plugins
  6. Multi-window functionality enables the user to utilize multiple windows on a single display.
  7. It possesses the power to customize and modify its appearance.
  8. It is command-line-based and permits extensive setup through the use of commands.
  9. Provides support for most programming languages
  10. It has basic commands, and it’s easy to grasp the commands
  11. It is A robust and dependable editor

Let’s get going and learn how to install and use Vim on our Ubuntu 22.04 operating system without further ado!

How to setup Vim on Ubuntu

Vim is a robust text editor accessible on nearly all modern computing platforms. It is included in the default installation of most Linux distributions; however, Ubuntu and Debian ship with a stripped-down version of Vim. The version lacks features such as syntax highlighting, reducing its robustness and utility. Vim is installed by default on Ubuntu. However, it can be downloaded from the Ubuntu repository. Ubuntu 22.04 includes Vim version 8.1.

Follow the steps to install the latest VIM package available in the repository.

To reap the full benefits of Vim, you must install the enhanced version. Ubuntu and Debian include this Vim version in their respective default apt repositories. You may install the complete version of Vim on Ubuntu using the apt command line utility.

Launch the terminal using the application launcher or the shortcut “Ctrl” + “Alt” + “t.

View the list of active and inactive features of the currently installed Vim software by using the following command:

vi --version
check vim version

Check vim version

Remove the default installation package, vim-tiny, by executing the following line of code:

sudo apt remove --assume-yes vim-tiny
remove tiny vim version

Remove the tiny vim version

Install Vim Using apt-get

This section will demonstrate how to install Vim using apt-get.

Step 1: Use apt-get to update your system

Update apt’s package list by using this command:

sudo apt-get update
update system resources

Update system resources

Step 2: Install Vim.

Once the system has been updated, the following command can be executed to install Vim:

sudo apt-get -y install vim
install vim via apt get

Install vim via apt-get

Install Vim on Ubuntu using apt

In this section, we will use apt to install Vim.

Step 1: Use apt to update your system.

Again, we will begin by updating the system. It is possible to accomplish so by issuing the following command:

sudo apt update
update system resources via vim

Update system resources via vim

Step 2: Install Vim.

After the system has been updated, execute the following command to install Vim on your system:

sudo apt -y install vim
install vim via apt

Install vim via apt

Install Vim on Ubuntu using Aptitude

The third method for installing Vim is to use aptitude. Let’s see what we can do.

Step 1: Install aptitude on your machine.

First, we must update our system. It is possible to do so by using the aptitude command:

sudo aptitude update
update system resources via aptitude

Update system resources via aptitude

Step 2: Install Vim.

Then, use the following command to install Vim:

sudo aptitude -y install vim
install vim via aptitude

Install vim via aptitude

You may use any of these three methods to install and use Vim on your Ubuntu machine.

Check that the package was successfully installed and look at the list of activated features.

vi --version
check vim version

Check vim version

Various modes of operation for the Vim editor

The Vim editor may be operated in several modes; below are the most common ones:

  • Insert mode: The insert mode is used to insert tasks. To activate Insert mode, just hit the “I” key. In this mode, there are no limits for adding or modifying files. After the insertion is complete, use the ESC key to quit the Insert mode and return to the command line’s normal mode.
  • Command line mode: This is the default command-line mode in which you may use simple vim editor commands, such as copying or deleting a line or moving the cursor to a specific location in the file after opening a file. If, for some reason, you were not in command line mode, pressing the ESC key will take you there.
  • Ex mode: This mode is often referred to as Prompt mode. To activate this mode, press the shift key and the “;” key. This mode facilitates customized command execution.
  • Visual mode: This mode allows you to choose and highlight text in Vim. In this mode, you may pick a single character or a full line of text and then reposition the mouse to enlarge your selection. Enter Visual mode by using the “v” key.

Launch the Vim editor

Execute the following command to launch the vim editor after the system startup:

Vim
launch vim

Launch Vim

You’ll see a terminal environment with an editor and simple commands.

Learning the primary commands of the vim editor

There are more than a thousand commands in the vim environment that do critical and diverse activities; to speed up and simplify your work with this editor, it is best to be familiar with its essential commands. Join us as we discuss many of its fundamental commands:

The “vim” keyword can be used to open and run a file using the vim editor. For instance, to run foss.txt in the vim text editor, the following command is executed:

vim foss.txt
open fosslinux.txt file

Open fosslinux.txt file

After running the above command, the Intended file will be executed in the vim editor.

To input any command in the vim editor, we use a colon (:) at the beginning of the command.

Help Command: If you find an issue when executing a command for which you require assistance, you may receive comprehensive instruction by typing:

 :help [command keyword]

For example, to obtain instructions on how to utilize the Copy command, we proceed as follows:

:help copy
run help copy command

Run help copy command

As a result, a file containing instructions and information about the term “copy” is presented.

Open a code file: To execute any file, the following command must be entered.

: e [file name]

For instance, to open a file named foss.txt, you would type the following:

:e foss.txt
open fosslinux.txt file

Open fosslinux.txt file

Quit Vim without saving a file: To avoid saving the settings you’ve made in the vim platform, use the following command:

:qa or :q!

Once you are done making changes, press “Enter” to exit the vim editor without saving and return to the terminal environment.

Quit Vim by saving a file: To keep the changes you made in the vim editor, type the following command into the search box and hit ENTER.

:wq

Save the file in Vim: You can save your work at any moment by inputting this command and then pressing Enter after typing the instruction.

:w

Save the file with a distinct name: To save a file with another name, use the following command:

:w [file name]

Example:

:w foss linux
save file with new name

Save the file with a new name

You can save your work and exit the Vim editor environment using the ZZ command.

Commands required for editing in the vim editor

To make changes to the text, enter insert mode using the “I/i” key and begin typing. When editing, commands like copy, paste, remove, and cancel come in handy. We’ll go over the following commands:

  1. Copy Commands: Because the word “copy” in Vim is derived from “yank,” yw is used when issuing copy commands:
yY: for Line Copy

yW: for Word Copy

y$: Copies from where the cursor is to the end of the line
  1. Command for Pasting: You must use the 😛 command to paste the copied content.
  2. Undo command: If you accidentally or inadvertently did something, you can return to the initial state by inserting the “u” command after the semicolon.

Note: Please remember that these commands must be entered in command line mode. Therefore, if you are in another mode, use the ESC key before entering a command.

  1. Redo command: In command mode, type “r” to undo any action you’ve taken in Vim.
  2. Delete commands: To delete sentences and words, use the following commands:
D: Deletes a line from the current cursor point to the line's end.

dgg: to erase the material from where the cursor is to the file's beginning.

dd: to remove a whole line

dw: to delete the word 

dw: to delete the word 

dG: deletes a line from the cursor's current position to the file's end.
  1. Commands for selecting items: The following are some commands for selecting or highlighting text:
The letter v is used to emphasize/highlight the character.

The letter V is used to emphasize/highlight a line.

Note: Remember to include the “:” before inputting commands.

Commands for cursor navigation

The keyboard controls the cursor solely in the vim editor, and the mouse has no purpose in moving the pointer. This section will evaluate the numerous Vim keyboard shortcuts and a list of keys and their functions. To access different functions associated with different keys, hit “:” followed by the desired key.

I: To move the pointer to the desired location

k: To position the cursor at the top

j: For Placing the pointer at the bottom

h: To move the cursor to the left side

M: To move the cursor to the center of the screen

H: Will move the cursor to the top of the screen.

L: To move the cursor to the bottom of the screen Directly

e: Positions the cursor at the word's end.

b: Sets the cursor to the beginning of the previous word.

w: Positions the cursor at the start of the next word.

Vim’s search commands

These commands help discover specific terms in the vim editor.

/text: To search for a word, type the desired term instead of text.

n: In the search, type in the following sample:

N: In the search, go back to the previous example.

/[Enter the word]: Scans the whole file for a given word. 

?[input the word]: Uses a keyword to search the archive for similar items.

Word count instructions

We can keep track of word and character counts in Vim like other editors. This is feasible in two ways:

In the command line environment, press the g key and CTRL + G to execute the w, !wc, and -w commands.

Press g, then “Ctrl + G” on your keyboard

Execute the w, !wc, and -w commands in the command line environment as shown:

:w !wc –w

Command to compare files

In the vim editor, compare two files against one another:

vimdiff foss1.txt foss2.txt
compare foss1 and foss2

Compare foss1 and foss2

As an illustration, let’s compare the foss.txt and the foss2 file by running the following line of code:

vimdiff foss.txt foss2
compare 2 different files

Compare two different files

The above command will check for differences between the two files and show them.

How to customize color in Vim

You can adjust things in the vim editor to your liking. Below are some modifications that can be made to the Vim editor on your Ubuntu OS:

Alter the font

You must type the following command in command line mode to change the font in Vim:

:set guifont = courier

This command will switch the vim font to courier.

Color shift

We use the following command to alter the color:

:colorscheme <tab>

The color is then yours to select from the range of hues offered.

Vim’s spelling checker

The following command verifies the word spellings:

:set spell

We use the following command to set the spelling language:

:set spelllang = <language>

Status line

The line shown at the editor’s bottom is the status line. The following command is used to activate it:

:set laststatus = 2

To turn off the status bar, enter the following:

:set laststatus = 0

Uninstalling Vim in Ubuntu 22.04

Now that we’ve looked at installing the Vim editor on our system let’s look at uninstalling it from Ubuntu. The uninstallation procedure is as straightforward as the installation procedure. All you need are a few Linux commands to uninstall the editor from your machine completely. So without further ado, let’s dive in!

Remove the Vim Package Only

If you want to uninstall the vim package, use the following command:

sudo apt-get remove vim

Remove Vim and its dependencies

If you wish to delete the editor’s dependencies as well as the editor itself, use the following command:

sudo apt-get -y autoremove vim

Delete all Vim configurations and data

If you wish to get rid of Vim and all of its customizations and data, use the purge command:

sudo apt-get -y purge vim

Remove any Vim configuration, data, and dependencies

You may also uninstall Vim and erase its settings, data, and all of its dependencies using the following command:

sudo apt-get -y autoremove --purge vim

Conclusion

This article taught us how to install the Vim editor in our Ubuntu system using various methods. We first utilized the apt-get method. Next, we used the apt way, and finally, we used the aptitude method to install Vim on our Ubuntu operating system. We further learned the various forms of using Vim and its commands. Finally, we discussed how to remove Vim. Several methods for removing Vim were demonstrated in this guide. You can delete the Vim package or all its dependencies, settings, and data from your system using any highlighted methods. It’s all up to you to choose the way that best suits you.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.