How to Install and Use Vim on Linux

How to Install and Use Vim on Linux

Editors (IDE) today are the main and most valuable tools for software development. Due to this importance, they have a wide variety, each of which has its characteristics and flaws. As a result, getting to know features and editors is not unimportant for programmers because they have an impact on facilitating their tasks and work processes.

Especially for developers who use Linux and its terminal or shell environment, it is essential to use a text editor. Unix-like operating systems on VPS offer root users the ability to easily edit and change all system files while the system is running, using a text editor.

Therefore, after buying a Linux VPS, as one of the principles of server management, every user must be familiar with famous text editors such as vim and how to install and work with the vim editor. Among the text editors for the Linux operating system, such as Vi, gEdit, Nano, gVim, and Emacs, the Vim editor is one of the oldest and most popular Linux text editors and an efficient option for Linux programmers, which is at the top of the list of editors. (If you are interested in installing and familiarizing yourself with Nano text editor, our article can guide you). The superiority of the vim editor compared to other competitors, especially when doing special config on a Linux server, is summed up in providing its very high customization capability.

The purpose of this post is to teach how to install the vim editor and learn how to use it in Linux. So, if you are planning to install the Vim editor on your system and take advantage of its features, don’t miss our step-by-step tutorial.

Introducing the Vim editor

The Vim editor, which stands for Vi Improved, is one of the most popular editors for the Linux terminal environment among users, which has become the most suitable tool for working with the Linux terminal by providing extensive configuration capabilities. Vim software was first introduced as VI Imitation and then renamed Vi IMproved. It is a free and open source editor developed in 1991 by Bram Moolenaar based on the Vi version, which was based on ed, the first Unix text editor.

The VI editor has been around since 1976, with Vim’s primary purpose being to develop and enhance the VI editor. In other words, Vim is actually a clone of the old Vi software that was written for various Unix-based systems such as Linux, OS X, Solaris, *BSD, and MS Windows. But, today, it can be installed on Windows operating systems.

Therefore, vim has been upgraded to be a powerful text editor that can be customized, advanced, and full of unique features. The vim platform strives to be advanced by providing features such as support for several popular programming languages, support for different file formats, and compatibility with various plugins. With its rich and extensible features and the use of functional plugins, Vim can become a platform beyond a simple text editor.

Advantages of the Vim editor

The features and advantages of the vim editor are very commendable and valuable; we will discuss a few of them:

  • Compatible with all types of systems in execution and installation
  • Having two terminal and graphic modes
  • A lightweight platform that requires few resources.
  • Support for the multiple tabs feature and the possibility of running in multiple files
  • Extensible with the benefit of various plugins
  • Multi-window support that allows the user to use multiple windows on one screen.
  • Having the ability to personalize
  • It is a command line based and allows for complex configuration using commands.
  • Support for most programming languages
  • Having simple commands and the possibility of quickly learning vim commands
  • A powerful and reliable tool

Different modes of running the Vim editor

Vim editor can be run in different modes, we will mention the most used modes:

Command line mode: Vim is the default command-line mode in which you can use simple vim editor commands by opening a file to perform tasks such as copying or deleting a line or moving the cursor to a specific position in the file. If, for some reason, you were not in the command line mode, pressing the ESC key will simply put you in the command line mode.

Insert mode: Insert mode is used to insert tasks. To enter and use Insert mode, just press the I key. In this mode, there are no restrictions on adding or changing the file. After completing the insertion, press the ESC key on the keyboard to exit the Insert mode and return to the default mode of the command line.

Visual mode: In Vim, you can use this mode to select and highlight text. In this mode, you can select a character or an entire line of text and then expand your selection by repositioning the cursor. Press the v key to enter Visual mode.

Ex Mode: This mode is also known as Prompt mode. To use this mode, you must press the shift key together with the key “;” This mode helps execute specific commands.

Different modes of running the Vim editor

How to install the Vim editor

Vim editor is a light, compact and free platform that does not have complicated installation steps. In Windows, there is a version of gVim that has a graphical environment that can be run, and you don’t need any prerequisites to install it. In Linux operating systems, the vim editor is installed by default in some Linux distributions. However, note that Vi and Vim are two different platforms. There may be VI in most Linux distributions, such as Ubuntu, but vim, which is an evolved version of Vi, may not be installed.

As a result, since Vim offers more advanced features than Vi, installing Vim can be much more efficient for developers. Follow the steps below to install the vim tool:

1. First, to check if your system already has a Vim editor, use the following command:

$ vim –version

If the Vim editor is not installed on your system, this command will not be executed, and you will encounter the message”: Command ‘vim’ not found, but you can be installed with, ” which offers you different suggestions.

How to install the Vim editor on linux

To begin with, you need to open a terminal with root access and update the package database by entering the following commands in the terminal to configure vim on Linux:

sudo apt update

Note: Before updating, you will be asked to enter your password.

To search for vim packages at this stage, you need to run the following commands:

sudo apt search vim

By executing the above command, you will face a different set of available packages; using them is unnecessary. We are looking to install the vim editor; Also, vim-tiny can be installed as an alternative to vim.

You can use the apt command to install the vim editor:

sudo apt-get update
$ sudo apt install vim -y

Type y to confirm the installation process.

How to install the Vim editor

How to install the Vim editor

To check if the vim editor is installed, type:

vim –version

Note: Linux vim install process on the Windows operating system are as follows:

  1. Refer to the Vim download page
  2. Click on the Download option on the left side of the panel
  3. Click on PC – MS-DOS and MS-Windows
  4. Click on gvim82.exe

After downloading the installation file, install it as simple software on your system, and you can use the command vim text.txt to open the file called text.txt. Pay attention to change Typical mode to Full mode during installation.

2. In the second method, you can also use the snap utility store to install the Vim editor on Linux. For this purpose, you must first install the Snap utility on your system by entering the following command:

$ sudo apt install snapd

How to install the Vim editor on linux

So now you can install vim editor by Snapp utility store by entering the following command:

$ sudo snap install vim-editor –beta

Run the Vim editor

To run the vim editor after startup, run the following command:

Vim

Run the Vim in linux

You will see a terminal environment that provides a version of the editor and basic commands.

Getting to know the main commands of the vim editor

In the vim environment, there are more than 1000 commands to perform essential and different tasks, and to speed up and facilitate your work process with this editor, it is better to be familiar with its essential commands. Join us to explain some of its important commands:

Open file: You can use the “vim” keyword to open and run a file through the vim editor. For example, to execute file.txt in the vim text editor, the command that is performed is as follows:

$ vim file.txt

After entering the above command, the Intended file will be performed in the vim editor.

Note that we use a colon (:) at the beginning of the command to enter any command in the vim editor.

Help Command: By entering: help [command keyword] in command mode, you will have access to complete guidance if you encounter a problem while running a command that you need help with. For example, we proceed as follows to receive guidance on how to use the Copy command:

:help copy

What happens is that a file with instructions and information about the keyword “copy” is displayed as a result.

Open a code file: To run any file, you must enter the command: e [file name] while in command mode. For example, if you want to open a file called code2.php, you would type as follows:

:e code2.php

Quit vim without saving a file: If you want to not save the settings you have applied in the vim platform, you must use the:qa or :q! Command. Press Enter to exit the vim editor without saving and return to the terminal environment.

:qa

Or

:q!

Quit vim by saving the files: If you want to save the changes made in the vim editor, you must enter the:wq command in the search box and then press ENTER.

:wq

Save the file: You can save your work by entering the:w command at any time during your work, and for this purpose, press Enter after typing the command.

:w

Save the file with a name change: if you want to save a file with a different name, run the following command:

:w [file name]

For example:

:w new file

You can use the ZZ command to save your work and then exit the Vim editor environment.

Cursor navigation commands

In the vim editor, the cursor is entirely controlled by the keyboard, and the mouse has no function in moving the cursor. In this part of the article, we will review the various Vim keyboard shortcuts and a list of keys and their purposes. To access different functions related to different keys, just press “:” and then the intended key.

CommandPerformance
ITo displacement the cursor to the appropriate position
kTo place the cursor at the top
jFor Placing the cursor to the bottom position
hTo transfer the cursor to the left side
Mto guide the cursor to the center of the display
Hto bring the cursor to the top of the screen Directly
Lto guide the cursor to the bottom of the screen Directly
eSets the cursor at the end of the word.
bSets the cursor to the previous word's start location.
wPlaces the cursor at the beginning of the next word.
GPuts the cursor at the file's end.
ggPositions the cursor at the beginning of the file
CTRL + bReturns the cursor to the previous page
CTRL + fAdvances the cursor by one page
0Positions the cursor at the beginning of the line
$Positions the cursor at the end of the line
}Places the cursor at the beginning of the subsequent block or paragraph.
{It moves the cursor to the beginning of the previous block or paragraph.
) This function instantly advances the cursor to the beginning of the following sentence.
(Sends the cursor back to the beginning of the preceding sentence.
#To go to a particular line, type the number of lines after the # symbol.
ge Go to the last character of the previous word
BGo to first word (ignores punctuation)

Necessary commands for editing in the vim editor

To make changes to the text, press the “I/i” key to enter the insert mode, then start typing. Some commands like copy, paste, delete and cancel are handy when editing. We will talk about these commands:

Copy Commands: Since the word “copy” comes from “yank” in vim, yw is used when issuing copy commands:

  • yY: for Copy the line
  • yw: for Copy the word
  • y$: Copies from where the cursor is to the end of the line

Paste command: To paste the copied content, you must use the :P command.

Undo command: If you have done something unintentionally or by mistake, you can return to the previous state by entering the “u” command after the semicolon.

Note that you must enter these commands in the command line mode, so if you are in another mode, press the ESC key before entering a command.

Redo command: In command mode, you can redo any action you have done in vim by typing “r.”

Delete commands: The following commands are used to delete sentences and words:

  • D: To delete a line from the current cursor position to the end of the line
  • dgg: to delete the content from where the cursor is to the beginning of the file
  • dd: to delete the entire line
  • dw : to delete the word
  • d: to delete a part of the line that you specified.
  • dG : to delete a line from the cursor’s current position to the end of the file.

Selection commands: Below are some commands used to select or highlight text:

  • v is used to highlight the character
  • V is used to highlight a line.

Note: Don’t forget the “:” before typing commands.

Search commands in the Vim editor

These commands are handy for finding specific words you are looking for in the vim editor.

  • /text: To search for a word, put the desired word instead of text.
  • n: Go to the following sample in the search
  • N: Go to the previous example in the search
  • /[enter the word]: Scans the entire file for a specific word.
  • ?[enter the word]: Uses a keyword to search for related content in the archive.

Commands to display numbers next to text

To set the display of numbers next to text lines, the following commands can help you find the line you want to edit or find the line you are on:

:set number
:set nu!
:set number!

Tip: To disable the command to add line numbers in vim, enter the following command:

:set nonumber

Word count commands

Similar to other editors, we can keep track of word and character counts in vim. This is possible via two methods:

  1. Press the g key and then CTRL + G
  2. Execute w, !w, and -w commands in the command line environment
:w !wc –w

Compare files command

To compare two files with each other in the vim editor:

$ vimdiff file1 file2

For example, if we want to compare two files, file.txt, and new file:

$ vimdiff file.txt newfile

As a result, it checks and displays the differences between the two files.

How to customize Vim in linux

In the vim editor, you can make changes according to your taste:

how to use vim in linux

Change font

To change the font in vim, you need to enter the following command in command line mode:

:set guifont = courier

By executing this command, we can change the vim font to courier.

color change

To change the color, we use the following command:

:colorscheme <tab>

Then you are free to choose the color from the variety of available colors.

Spell checking in vim

The following command checks the spelling of words:

:set spell

To set the spelling language, we use the following command:

:set spelllang = <language>

Status line: The status line is the line that is displayed at the bottom of the editor. For activating  it, the following command is used:

:set laststatus = 2

To disable the status bar, type:

:set laststatus = 0

Introducing 7 of the best vim plugins

Plugins help to improve and upgrade the software’s capabilities. One of the prominent advantages of vim is that it is compatible with a wide range of plugins, most of which are free. Here are some examples of the best plugins supported by the vim editor:

  1. DumpX plugin: used to implement c++/c codes.
  2. Pathogen plugin: Used as a package manager for vim.
  3. Git-switcher. vim plugin: for saving and loading Vim project files based on changes made in the git branch.
  4. NERDTree plugin: is a file manager that is used to run different files and folders.
  5. NERD Commenter plugin: To maintain the beauty of coding, commenting is important, and this plugin is effective in commenting.
  6. Vim-colors-solarized plugin: Used as a popular theme for developers.
  7. Syntastic Plugin: Checks the syntax of existing code and displays errors. Notably, Syntastic supports the majority of programming languages.

Uninstalling vim editor

If you ever want to uninstall the vim editor from your system, just enter the following command:

sudo apt remove vim

Before starting the uninstalling process, you need to enter your password.

Then, in the next step, you must enter y to confirm the uninstalling process.

FAQ

Editing files in Linux

Press the ESC key to go back to regular mode.

For insert mode, press the I Key.

Press the:q! to exit the editor without saving a file.

Type:wq to save the edited file and close the editor.

To save the file as test. txt, type:w test.

First, By pressing the 'ESC' key, enter command mode in the vi editor. Type ":", then "!", and the command.

The Linux family used the full-screen text editor Vi as its standard. Bram Moolenaar developed an improved version of the vi editor called Vim. The way vim commands are executed is based on vi, and actually, vim means improved vi.

Conclusion

The Vim editor can be the best choice if you are looking for a command line editor with a wide range of valuable features. This is a more sophisticated and advanced version of the popular Vi text editor and can read and edit files written in various programming languages. This editor is great for professionals and beginners with its functions and features.

In this article, we discussed how to set up and use the vim editor in Linux, as well as its various commands. We’ve done our best to explain how to use the commands; we hope you find them helpful.

 

Leave a Reply

Your email address will not be published. Required fields are marked.