Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim, removing blank and commented lines in one regex

Tags:

I want to remove blank and commented lined at one time. I already found the similar question regarding removing blank lines, so for blank lines I use:

:g/^$/d 

and for commented lines:

:g/^#/d 

I'm curious is there any way to merge these regex in one? Something like

:g/^[$#]/d 

but obviously it doesn't work in vim.

like image 843
evfwcqcg Avatar asked Sep 30 '12 15:09

evfwcqcg


People also ask

How do I delete blank lines in Vim?

:g/^$/d - Remove all blank lines. The pattern ^$ matches all empty lines. :g/^\s*$/d - Remove all blank lines. Unlike the previous command, this also removes the blank lines that have zero or more whitespace characters ( \s* ).

How do you remove two lines of text when using VI?

Deleting Multiple Lines in Vi and Vim Instead of simply pressing dd, you can specify the number of lines you want to delete. For example, typing 3dd will delete the next three lines from the file. You can also use wildcard characters in the aforementioned command.


1 Answers

You can try this command:

:g/^\(#\|$\)/d 

Or

:g/\v^(#|$)/d 

  • $ matches literal '$' inside [...] (type :help /$ for help)
  • \| is for alternation
  • \v is very magic (minimal backslash escape)
like image 183
kev Avatar answered Oct 14 '22 06:10

kev