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,648FansLike
360FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

How to install CMake on Ubuntu

CMake is a cross-platform free and open-source software tool designed to build, test, and package the software. CMake uses a simple platform and compiler-independent configuration files to control the software compilation process.

How to install Lightworks on Ubuntu

Even though Linux may not get a native installer of video editing software like Adobe Premiere or Final Cut Pro, that doesn't mean there are no industry standards tools available. Lightworks is non-linear editing (NLE) video mastering app for Windows, Linux, and macOS. Installing it on Ubuntu is simple due to deb package availability.

How to install DaVinci Resolve on Fedora

Davinci Resolve is a professional application used for color correction, video editing, visual effects, and motion graphics. It is one of the extensively used software by movie industries located in Hollywood.

The 10 Best Programming Languages for Hacking

One of the significant entities we have in Cyber Security is Ethical Hacking (ETH). It is the process of detecting and finding flaws or vulnerabilities in a system that a hacker would exploit.

5 Ways to Open a Terminal in Ubuntu

Even though Ubuntu supports many applications with amazing Graphical User Interfaces (GUI), there are always reasons why users prefer using the Terminal to perform different tasks.

How to install Wine on Fedora Workstation

Linux distributions are becoming more and more popular every day, and Fedora Workstation is not left behind. This popularity brings forth the need to run Windows applications on Linux distros like Fedora. Windows has quite some excellent Software that is not available for Linux.

MUST READ

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.
Elementary OS 5.1 Hera has received a point release with a handful of new features and bug fixes, and we will be reviewing the significant changes in this article. For those new to elementary OS, this Ubuntu-based Linux distribution uses their inhouse built Pantheon desktop environment and AppCenter.

6 ways to find out your Linux file system type

Any Operating system in the market whether its Windows, Linux, Unix, macOS, and any other, must be able to access and manage files and data on storage devices.

Test drive a Linux distro online before you hate it

Enter DistroTest.net, a website that allows Linux users to test various distros online, without downloading the ISO or installing the distro. With DistroTest.net, you can check a distro with no muss, no fuss.

Installing multiple Linux distros on a USB drive

A bootable Linux USB drive, which also referred to as a Live USB drive, is a drive containing files that enable a computer to boot into a specific Linux distro. It is most commonly used to test-drive various Linux distros before installing on the computer for use as a daily driver.

Scrcpy – Control Android devices from a Linux desktop

Scrcpy is a desktop program that can be used to access your Android phone's system and interface through your computer. The app is quite convenient, and some of its best features are highlighted below.