Remove lines starting with “A”

If you want to delete lines that begin with a specific character in Vim, here’s how you can do it. For instance, if you want to remove all lines starting with the letter “A,” you can use the command

 :g/^A/d

This command will search for lines that begin with “A” and delete them. S

Suppose your file contains the following lines:

Apple
Banana
Avocado
Berry

By executing:g/^A/d, the file will become:

Banana
Berry

How to remove lines starting with a special character

If you need to delete lines starting with a special character like the dollar sign $, you must escape the special character with a backslash. Use the command

 :g/^\$/d

For example, if your file contains

$price
$value
total
sum

After running :g/^\$/d, the file will be:

total
sum

How to remove lines starting with a comment symbol

To remove comments from a bash script, which typically start with the hash symbol #, use the command :

 :g/^\#/d

For example, if your bash script contains:

# This is a comment
echo “Hello World”
# Another comment
echo “Goodbye World”

By executing :g/^\#/d, the script will become: 

echo “Hello World”
echo “Goodbye World”

How to remove blank lines in VIM

To remove all blank lines from your file, use the command :

 :g/^$/d

For example, if your file contains:

First line

Second line

Third line

After running :g/^$/d, it will be:

First line
Second line
Third line

These commands help you efficiently edit your text files by removing unwanted lines based on specific criteria.  

Read also:

How to Delete Lines Starting with a Specific Character in Vim

Categorized in:

Code,