In this article, we will cover some basic commands in the Vim editor. First, we look at what Linux and VIM editors are and why we use them, followed by basic commands in Vim editor starting from creating/editing a file, different modes, quitting the editor, saving the changes to the file, navigation in the editor, deleting lines, displaying and hiding line numbers, search and replace, and syntax highlighting along with the syntax of commands and screenshots.
Linux
Linux was developed by Linus Torvalds in 1991 as a hobby project. It is an open-source (source code that can be used by anyone freely) kernel that is most popular and widely used in the industry as well as in personal systems. There are various operating systems based on the Linux kernel, some of the popular Linux distributions are Ubuntu, CentOS, Red Hat, Debian, and Kali Linux.
VIM Editor
Vi Editor is a widely used text editor in Unix/Linux systems and is known for its efficiency and flexibility. Vi editor was developed in 1976 by Bill Joy and later in 1991, an improved version of Vi editor was released which is known as VI Improved (VIM).
1. Create/Edit a file
To create/edit a file in the Vim editor run the below command
Syntax:
vim filename
Example:
vim new.txt
Replace [filename] with the name of the file you want to create or edit. In this case, the filename is new.txt
Creating Files in Vim2. Changing modes in the Vim editor
There are Six modes in Vim editor.
- Editing/Insert Mode: To make any changes in a file in Vim editor user has to first enter editing/insert mode, Press 'i' or 'a' to enter editing/insert mode.
Entering in Insert Mode- Command Mode: To run any command in Vim editor user has to enter command mode, if you are in editing/insert mode Press Esc then ':' followed by the command. For example: set the number

3. Quit the Vim editor
To quit the Vim editor enter command mode and use the following command,
Command
| Description
|
|---|
:q
| Quit the editor
|
:q!
| Quit without saving changes i.e. discard changes.
|
:wq
| Save the changes and quit the editor
|
Example/Screenshot:
Quitting Vim Editor4. Save the changes
To save the changes to the file in the Vim editor enter command mode and use the following command,
Command
| Description
|
|---|
:w
| Write to file called [file_name] (save as).
|
:w!
| Overwrite to file called [file_name] (save as forcefully).
|
:wq
| Save the changes and quit the editor
|
5. Navigation in Vim editor
We have covered the navigation in Vim editor in a detailed article at GFG which you can check here.
Command
| Description
|
|---|
k
| Moves the cursor up one line.
|
j
| Moves the cursor down one line.
|
h
| Moves the cursor to the left one-character position.
|
l
| Moves the cursor to the right one-character position.
|
6. Delete a line
To delete a single line Press the Esc key if you are in insert/editing mode, go to the file you want to delete Press 'dd' and then the line gets deleted.
To delete a range of lines use the following command,
Syntax:
:[start],[end]d
Example::3,5d In this command the editor will delete the lines from 3 to 5 including the extremes.
Example Screenshot:
Deleting a LineWe have covered a detailed article on this at GFG which you can check out here.
7. Search and Replace
To find and replace words in the vim editor we use substitute or:s command syntax of the command is as follows:
:[range]s/{pattern}/{string}/[flags] [count]
The command searches the pattern in [range] lines and replaces the [pattern] with [string]. If [range] is not mentioned then the command will replace the words in the current line with [string] only.
Example:
Searching and Replacing8. Display Line Numbers
To display absolute line numbers use any of the following commands,
Syntax:
:set number
or
:set nu
Example/Screenshot:
Displaying Line NumberTo display relative line numbers: In relative line numbers the current line is shown as 0 and the lines above and below are incremented by 1,
Syntax:
:set relativenumber
or
:set rnu
Example/Screenshot:
Displaying Relative Line NumberWe have covered a detailed article on this at GFG which you can check out here.
9. Hide Line numbers
To hide absolute line numbers use any of the following commands,
Syntax:
:set nonumber
or
:set nonu
Example/Screenshot:
Hiding Line NumberTo hide relative line numbers use any of the following commands,
Syntax:
:set norelativenumber
Alternatively,
:set nornu
We have covered a detailed article on this at GFG which you can check out here.
10. Syntax Highlighting
To Enable syntax highlighting type the below command and press Enter.
Syntax:
:syntax on
Screenshot:
Enabling Syntax HighlightingTo disable syntax highlighting type the below command and press Enter.
Syntax:
:syntax off
Screenshot:
Disabling Syntax HighlightingConclusion
In this article, we covered basic commands in Vim editor starting from creating/editing a file, different modes, quitting the editor, saving the changes to the file, navigation in the editor, deleting lines, displaying and hiding line numbers, searching and replace, and syntax highlighting along with the syntax of commands and screenshots. Thereafter we discussed some of the frequently asked questions (FAQs).
Similar Reads
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response
In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network
Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
3-Phase Inverter
An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
What is Vacuum Circuit Breaker?
A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read
Linux Commands Cheat Sheet
Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous W
13 min read