:g/foo/d - Delete all lines containing the string “foo”. It also removes line where “foo” is embedded in larger words, such as “football”.
:g!/foo/d - Delete all lines not containing the string “foo”.
:g/^#/d - Remove all comments from a Bash script. The pattern ^# means each line beginning with #.
: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*).
:[range]s/{pattern}/{string}/[flags] [count]
The command searches each line in [range] for a {pattern}, and replaces it with a {string}. [count] is a positive integer that multiplies the command.
:s/foo/bar/
:s/foo/bar/g
:%s/foo/bar/g
Instead of the slash character (
/), you can use any other non-alphanumeric single-byte character as a delimiter. This option is useful when you have the ‘/’ character in the search pattern or the replacement string.
c:s/foo/bar/gc