Home Terminal Tuts How to delete lines efficiently in Vi and Vim

How to delete lines efficiently in Vi and Vim

We'll cover a range of commands, from deleting a single line to removing multiple lines or blocks of text. Ready to enhance your text editing skills in Vi and Vim?

by Divya Kiran Kumar
deleting text lines in vi vim

If you’re like me, you’ve come to appreciate the sheer power and versatility of the Vi and Vim text editors. These editors, while often considered daunting for their learning curve, can transform you into a text editing wizard once you get the hang of them. For today’s tutorial, I’ll be focusing on one specific aspect: how to delete lines in Vi and Vim. While it might seem trivial at first glance, mastering this aspect can significantly speed up your workflow and make your coding life easier.

Introduction to Vi and Vim

Vi (which stands for Visual) is a screen-based text editor that was created for Unix in the 1970s. Vim (Vi IMproved) is an extended version of the Vi editor, with many additional features designed to be helpful for programmers. It is a highly configurable text editor built to make creating and changing any kind of text very efficient. Whether you are coding or just need a quick note-taking option in your terminal, Vim is there to your rescue.

Installing Vi and Vim in Linux

Both Vi and Vim are standard components of virtually every Unix system. In many Linux distributions, Vi is installed by default. However, Vim often has to be installed manually. Here are the steps to install both Vi and Vim on several popular Linux distributions:

For Ubuntu, Debian, and their derivatives:

The Advanced Packaging Tool (APT) makes it very straightforward to install Vim on these distributions. Run the following commands in your terminal:

First, update your system’s package index:

sudo apt-get update

Then, install Vim:

sudo apt-get install vim

For Fedora:

On Fedora, you can use the Dandified Yum (DNF) package manager to install Vim. Enter the following commands in your terminal:

First, update your system:

sudo dnf update

Then, install Vim:

sudo dnf install vim

For Arch Linux and its derivatives:

The Pacman package manager allows you to install Vim on Arch Linux. Use these commands in your terminal:

Update your system:

sudo pacman -Syu

Install Vim:

sudo pacman -S vim

For openSUSE:

On openSUSE, you can install Vim using the Zypper package manager:

Update your system:

sudo zypper update

Install Vim:

sudo zypper install vim

Remember, after the installation process, you can verify your Vim version by typing vim –version in your terminal. This will help you confirm whether Vim has been correctly installed on your Linux system.

Deleting lines in Vi and Vim – The basics

Before we start, let’s open a file using Vim to put our theory into practice. You can open any text file you’d like. If you don’t have a text file at hand, you can create one with Vim directly. Open your terminal and type the following:

vim FOSSLinux_log_sample.txt

Press enter, and voila, your text file is ready for editing.

editing a text file using vim editor

Editing a text file using vim editor

Now, let’s say you are editing a text file and you want to delete a line. With Vi and Vim, it’s incredibly simple.

Firstly, move your cursor to the line you want to delete. You can do this by using the arrow keys or the h (left), j (down), k (up), and l (right) keys.

Secondly, once your cursor is on the desired line, enter the command mode by pressing : 7 followed by the line number you want to delete and d. For example, if you want to delete the 7th line, you would enter :7d.

vim in editing mode

Vim in editing mode

Lastly, press enter to execute the command.

And just like that, the specified line disappears. In my example above, you can notice I deleted line 7 containing “24”.

deleting a line in vim editor

Deleting a line in vim editor

No fuss, no muss!

Saving the file in Vi and Vim

Saving a file is a very straightforward process. However, it requires you to be in command mode, not insert mode or visual mode. If you’re unsure, simply press Esc to ensure you’re in command mode. Here’s how to save your work:

To save a file, you use the :w command, which stands for ‘write’. After typing :w, press Enter, and Vim will save your changes to the file.

If you want to save the file under a different name, you can do so by following :w with a space and the new filename. For example, :w newfilename would save the current file under the name ‘newfilename’.

If you want to save your work and immediately exit Vim, you can use the :wq command (write and quit). After typing :wq, press Enter, and Vim will save your changes and then close.

Advanced deletion techniques in Vi and Vim

Once you get comfortable with the basic deletion process, there’s a whole world of advanced deletion commands at your fingertips in Vim. Let’s explore some of these.

Deleting Multiple Lines
If you want to delete multiple lines, Vim has a straightforward way to do that. Let’s say you want to delete lines 5 to 7, you would enter :5,7d.

Deleting All Lines
Now, what if you want to delete all the lines in the file? Instead of specifying each line number, simply enter :%d. The % represents all lines in the file.

Deleting from Current Line to End of File
If you want to delete from the current line to the end of the file, place your cursor on the line where you want to start deleting and enter :, $d. The $ represents the end of the file.

Pro tips for line deletion

As a seasoned Vim user, I have a few tips up my sleeve to make line deletion even more effortless. Here are some of them:

Using Visual Mode for Deletion
One of my personal favorites is using visual mode for deletion. Press V to enter visual line mode, select the lines you want to delete using the arrow keys or hjkl keys, and then press d to delete them.

using visual mode in vim

Using visual mode in vim

Using Number with d command
Another quick tip that I use daily is to combine the delete command d with a number n and the down command j to delete n lines below the cursor. For example, typing 3dj would delete the current line and the next two lines.

Common troubleshooting tips

As you venture further into the Vi/Vim journey, you may encounter certain issues. Here’s how to troubleshoot the most common ones:

Unable to delete lines
If you find yourself unable to delete lines, you’re likely not in command mode. Press Esc to exit insert or visual mode and return to command mode.

Unintentional deletion of lines
If you’ve deleted lines unintentionally, don’t panic! Vim has a robust undo feature. Simply press u in command mode to undo the last operation.

Conclusion

I hope this guide serves as a handy reference on your journey with Vi and Vim. Deleting lines is an elementary task, yet knowing how to do it efficiently can make a substantial difference in your productivity. If I could go back in time and tell my past self one thing about using Vim, it would be to embrace the initial learning curve. The more you use these commands, the more they will become second nature to 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.