Managing Empty Lines, Comments, Line Endings, and Duplicates in Vim

Removing Blank Lines

To delete all blank lines and those containing only whitespace:

:g/^$/d

To remove lines that contain only spaces or tabs:

:g/^\\s\*$/d

To eliminate lines starting with #, or with spaces or tabs followed by #:

:g/^\\s\*#/d

For PHP configuration files where comments start with ;:

:g/^\\s\*;/d

To delete lines from the second line to those containing 'bbs':

:2,/bbs/d

To delete lines from a line containing 'bbs' to the end of the file:

:/bbs/,$d

To remove lines not starting with https:

:g!/https/d

To remove lines with exactly one character before 'bbs':

:g/.bbs/d

(In PhpStorm, deleting blank lines can be done via ^\n replacement with empty string)

Replacing Line Endings in gVim

When editing text files in Vim and wanting to prepend a character like ';' before each line break, the command:

:s/\\n/;\\n/g

may not work as expected.

The correct approach uses:

:s/\\n/;\\r/g

Explanation:

  • During search operations: \\n represents a newline, \\r stands for carriage return (Ctrl-M).
  • During replacements: \\r becomes a newline, while \\n is interpreted as a null byte (0x00).

Deleting Characters After '?' in Each Line

To remove everything aftter a '?' including the '?' itself in each line in Vim:

:%s/\?.*//g

This command:

  • :%s performs substitution across the entire file.
  • \\? matches the literal '?' character.
  • .* matches any characters following the '?'.
  • // replaces matched content with nothing (i.e., deletes it).
  • g ensures global replacement per line.

Alternative for very magic mode:

:%s/\v\zs\?.*//g

Here, \v enables very magic mode, making most special characters behave as literals unless escaped. The \zs sets the match start point just after the '?', avoiding unnecessary escaping.

Ensure you're in command mode (press Esc) before executing these commands.

Removing All ^M Characters

Replace all carriage returns (represented as ^M):

:s/CTRL+V CTRL+M//g

Which means press Ctrl+V followed by Ctrl+M to insert ^M, then replace with nothing.

To remove all trailing spaces:

:s/\\s\*$//g

Or to remove all trailing spaces and ^M:

:s/CTRL+V CTRL+M//g

Removing Everything After Quotes

Delete everything after a quote, including the quote, if it's not preceded by a backslash:

:%s/\([^\\]\)".*/\1/

Explanation:

  1. :%s/ – Substitute through out the file.
  2. \([^\\]\)" – Match a double quote that is not preceded by a backslash.
    • \( ... \) – Captures the character before the quote.
    • [^\\] – Matches any non-backslash character.
  3. .* – Matches anything following the quote.
  4. \1 – Replaces with the captured character before the quote.

Eliminating Duplicate Lines

  1. Simplest method to remove duplicates:

:sort u

This sorts lines and keeps only unique entries. It works best when lines are unordered and identical.

  1. Using pattern matching to delete duplicate lines:
:g/^\(.*\)$\n\1$/d

  • :g/.../d – Delete lines matching the condition.
  • ^\(.*\)$ – Capture the full line.
  • \n\1$ – Match the next line to see if it's identical to the previous one.

Tags: Vim Linux grep regular expressions Text processing

Posted on Wed, 20 May 2026 04:38:34 +0000 by yellowepi