How to Delete a Line in Vim Editor - Vim Line Delete Command (2022)

A powerful text editor that is used in CLI (command line interface) is Vim Text Editor. Linux uses a lot of different configuration files; therefore, you will often require editing them and for this Vim or Vi Editor is the best tool to do so. This article will teach you everything about:

  • How to delete a line in Vim
  • How to delete multiple lines in Vim
  • How to delete a range of lines in Vim
  • How to delete multiple words in Vim

There are alternatives for Vim as well. These are the command line editors joe and nano.

Vim follows a particular working method. It comes with two main modes. These are as followed:

  • The command mode
  • The other modes

Command Mode is used to help you select the working method that you wish to enter. The methods available save, quit, copy, paste and this sort of thing however you cannot edit the file in command mode directly. This is what most of the users are new to vim puzzles and therefore has to get used to it first.

Contents

Different Modes in Vim Editor

Vim Delete Multiple Words

Vim also comes in different modes. These are as followed:

  • Visual Mode

The visual way lets the user select for the text that you would do with a mouse but in spite of using the mouse using the keyboard.  It is instrumental in copying many lines f text, for example. For this the shortcut is V

  • Insert Mode

With the insert mode, you can insert text in a document. For this, the alternative is “I” (add text where the cursor is) or “o” (add text at the start of the following line).

  • Command Mode

The command mode starts with the symbol “:”. Whenever you are in any other way than to go back you can use the escape key (sometimes you might need to hit it twice).

READ ALSO: How to Install Python 3.7 in Ubuntu 18.04/19.04/19.10.

How to Delete a Line in Vim – Line Delete Command for VIM

  • Install Vim in Ubuntu/Debian distributions

#sudo apt install vim

  • Install Vim in RHEL/CentOS distributions

#yum install vim

READ ALSO: MacBuntu Transformation Pack Download For Ubuntu 19.04/19.10.

How to Delete a Single Line in Vim Editor

Deleting Line in Vim Editor

In vim editor, you can delete a single line. For this, you need to follow the steps below:

  • In the first step, you need to place the cursor there at the beginning of the line
  • Once there press the escape key
  • Then press the letter quickly twice
Recommended:  6 Ways To Bypass Lock Screen Password in Android 11/12 2022

An example has been shown below in which after pressing the letter d twice at the beginning of line 6 will delete the whole range.

READ ALSO: How to Install Arch Linux – Step by Step Tutorial.

How to Delete All Lines in Vim

You are also able to delete all the lines in the vim editor. There are two ways of doing this

  • 1,$d
  • %d

Deleting Multiple Lines in Vim

Moreover, in the vim editor, you can also delete multiple lines. For this, you need to follow the steps below:

  • In the first step, you need to place the cursor there at the beginning of the line
  • After this prefix the dd command with the number of the line that you wish to delete below it. Like if you wish to delete three consecutive lines then below line 3, you need to press #3dd.

READ ALSO: 8 Best Ways to Secure a Linux Server – Hardening Guide.

How to Delete a Range of Lines in Vim

Delete Range of Lines Vim

If you wish to delete a range of files from lines 3-5 then you need to follow the steps below:

[start_line_no],[end_line_no]d

In such a case, you need to hit the escape button. Once done type the following command and press enter:

3,5d

If you want to delete the last line then use:

Sd

If you want to delete all the lines before the current line then use:

1, -1d

If you want to delete all lines after the current line then use

.+1,Sd

READ ALSO: Top 8 Best GTK Themes for Ubuntu 19.10 Download.

How to Delete lines by a given pattern

You can also delete lines by a given pattern there in the vim pattern for this you need to follow the steps below:

If you wish to delete a line that carries a particular word, then you need to press on the escape button and run:

g/word/d

If you want to delete lines that carry the word lazy, then run:

g/lazy/d

If you want to contain every line with the word lazy, then use

%g!/lazy/d

Or

v/lazy/d

If you want to delete lines that start with a particular letter like A, then use

g/^A/d

If you want to delete lines that start with a special letter like $ sign, then prefix the character with a backslash:

Recommended:  How to Block Programs from Accessing the Internet in Windows 10/11

g/^\$/d

If you wish to get rid of all blank lines, then run:

:g/^$/d

READ ALSO: 10 Ubuntu Terminal Commands and Shortcuts for 2022.

Any other tips for Deleting Lines in Vim?

We have shared all the possible terminal commands for deleting lines in Vim. We have shared how to delete a single line in Vim, delete multiple lines in Vim, delete a range of lines and delete all the lines in Vim. However, there is always that one guru that has a trick or tip. Do let us know if you have any other way of deleting text in Vim.

Shaheer is the founder of SecuredYou. He is a cybersecurity freak and loves anything related to Computers and Technology. Apart from being a tech geek, he loves listening to music and going to the gym.

LEAVE A REPLY

Please enter your comment!
Please enter your name here