How to use Vi editor in Linux (with examples)

In this tutorial, we shall show you how to work with the Vi editor and the most important shortcuts to use that will help you effectively edit any file. Vi has three main modes that you can use including the Command mode, Escape mode, and the Insert mode that we will be discussing today.

Vi editor is short for the visual editor app and is a command-line based tool used as the default editor in almost all Linux distributions. It is similar to Notepad on Windows or TextEdit on macOS.

In this tutorial, we shall show you how to work with the Vi editor and the most important shortcuts to use that will help you effectively edit any file. Vi has three main modes that you can use including the Command mode, Escape mode, and the Insert mode that we will be discussing today.

1. Command mode

The command mode in the vi editor is the first mode you will get when you open up the vi editor. In this mode, every typed character is considered as a command. Hence, the vi editor will not display or show any of those commands in the window. Moreover, as long as you are in this mode, you can copy, paste, move through the file, or even delete some characters from the file. Additionally, if you are using any of the other two modes and need to return to the command mode, all you need to do is just press the [ESC] key from the keyboard.

To open a file in the vi editor, launch the Terminal and enter the following command:

vi filename

Where the filename may be a new file that will be created or an already existing file.

Create and Open Vi Test File
Create and Open Vi Test File

If you created a new file that is not already exiting, then you should get something like the below screenshot:

The Vi Test File Opened
The Vi Test File Opened

You can use the following shortcuts:

  • dd is used to delete a line from the file
  • dw is used to delete a word from a file
  • x is used to remove a character from a file
  • G is used to go to the end of the file
  • Cursors are used to move and navigate within the file

2. Escape mode

To be able to use this mode, you need to first be in the command mode, then press the colon “:” character. Typically, during this mode, you can write any command next to the colon character. The forward slash can be used instead of the colon character, and that is in case you need to search the text in the file.

The following commands can be used during the escape mode:

  • :wq, is used to save and quit the file. Note that the command is case insensitive, and it appears at the bottom of the screen, as you can see in the below screenshot.

Save and Quit File
Save and Quit File

  • :q! is used to quit the file without saving.

Quit The File Without Saving
Quit The File Without Saving

  • :q is used to quit the file.

Quit The File
Quit The File.

  • :w is used to save the file without quitting it.

Save The File
Save The File

  • /string is used to search for the string in the file.

Search For a String
Search For a String

You can hit enter then the cursor will highlight the string you are searching for if it is found like the below screenshot:

The Result of Searching a String
The Result of Searching a String

3. Insert mode

In the insert mode, each keyboard keystroke will be dealt with as a text, and the vi editor will preview it on the screen as entered from the keyboard by the user. This is the mode where you will be allowed to start entering and inputting text into the file.

During the insert mode, you can use the following shortcuts:

  • [ESC] + i, is used to insert text
  • [ESC] + a, is used to append text
  • [ESC] + A is used to append text at the end of a line
  • [ESC] + r, is used to replace a character in the file

Conclusion

That’s all about the Vi editor and its usage in Linux. Hopefully, this tutorial helped you to get you ready to edit your files using the vi editor and effectively use its shortcuts.

Hend Adel
Hi! I'm Hend Adel, a freelancer technical geek with successful experience in Database, Linux and many other IT fields. I help to build solutions to suit business needs and creating streamlined processes. I love Linux and I'm here to share my skills via FOSS Linux! Thanks for reading my article.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,417FansLike
377FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

Basics of Working with the SQLite Database in Python

A database is one of the most useful and popular files for storing data; they can be used to store any kind of data, including text, numbers, images, binary data, files, etc. SQLite is a relational database management system based on the SQL language. It is a C library, and it provides an API to work with other programming languages, including Python. It does not require a separate server process to be run as needed in large database engines like MySQL and Postgresql.

5 Ways to Check the Linux Version

When most people talk of Linux, they are always referring to a Linux distribution. However, this is not the case. Linux itself is a kernel which acts as a bridge between user applications and the hardware. When we talk of a Linux distribution, we refer to an operating system developed from the Linux kernel. A distribution comes with a package manager, pre-installed applications, a Desktop Environment, and several more features.

Getting Started with Linux Operating System

The Linux operating system brings forth a vibrant mix of features and security, making it the best alternative to macOS or Windows operating systems. In this post, we will give you a master guide on Getting started with Linux systems - taking you from a complete beginner to a level where you can begin testing the various Linux distributions available with much ease.

How to Create a Comprehensive Mail Server on Ubuntu

Postal is a free and open-source mail server used to send and receive emails. It comes loaded with tons of excellent features and functionalities, making it extremely popular among large organizations as well as in enterprise settings.

The 10 Best Linux Performance Monitoring Tools

Do you want to monitor the performance of your Linux system? Are you looking for some powerful performance monitoring tools to help you out? If you agree, it's your day as we have put together a detailed list of the ten best Linux performance monitoring tools.

How to Boot your Windows or Linux PC from a USB Drive

Sometime back, the process of installing an operating system required users to pop a bootable media disk into their DVD or CD drive and use it to boot the PC. But times have changed. Nowadays, the most common way of installing an OS is booting from a USB drive. The use of USB drives is further propelled by the current production of slim and lightweight laptops with no support for DVD/CD drives.

MUST READ

Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.
Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.

[Guide] apt vs apt-get commands, and which one to use?

Most Linux users, both veterans, and newbies, often get confused about what the difference between the Linux commands apt, and apt-get are and when they should use one or the other.

Ubuntu MATE 20.04 LTS Review: Refinement at its Best

Ubuntu MATE 20.04 LTS was released a week ago after two years of development. Official updates and security patches will be provided until April 2025. I have installed it on my test laptop for a spin and here are my observations based on almost a week usage.

5 Best Application Launchers for Ubuntu

Ubuntu is one of the most used Linux distributions worldwide. It is also the reason why it has the maximum number of available programs for itself. Today we are going to talk about one category of those programs, the application launchers.

Removing the Virus from a Windows PC with a Ubuntu Live USB drive

In this tutorial, we are going to show you how to clean your Windows machine from infected viruses using an Ubuntu live USB or CD and the ClamAV antivirus. The ClamAV is a free, open-source antivirus that can be used on Ubuntu.