Skip to content

Instantly share code, notes, and snippets.

@shreeshga
Created July 19, 2012 20:42
Show Gist options
  • Save shreeshga/3146677 to your computer and use it in GitHub Desktop.
Save shreeshga/3146677 to your computer and use it in GitHub Desktop.

Revisions

  1. shreeshga renamed this gist Jul 19, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. shreeshga revised this gist Jul 19, 2012. 2 changed files with 27 additions and 13 deletions.
    27 changes: 27 additions & 0 deletions delete_lines.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    The ex command <code>g</code> is very useful for acting on lines that match a pattern. You can use it with the <code>d</code> command, to delete all lines that contain a particular pattern, or all lines that do not contain a pattern.

    For example, to delete all lines containing "profile" (the first command is optional; it shows the lines that the second command will delete):
    <pre>
    :g/profile
    :g/profile/d
    </pre>

    More complex patterns can be used, such as deleting all lines that are empty or that contain only whitespace:
    <pre>
    :g/^\s*$/d
    </pre>

    To delete all lines that do ''not'' contain a pattern, use <code>g!</code>, like this command to delete all lines that are not comment lines in a Vim script:
    <pre>
    :g!/^\s*"/d
    </pre>

    Note that <code>g!</code> is equivalent to <code>v</code>, so you could also do the above with:
    <pre>
    :v/^\s*"/d
    </pre>

    The next example shows use of <code>\|</code> ("or") to delete all lines ''except'' those that contain "<code>error</code>" or "<code>warn</code>" or "<code>fail</code>" ({{help|pattern}}):
    <pre>
    :v/error\|warn\|fail/d
    </pre>
    13 changes: 0 additions & 13 deletions delete_lines.txt
    Original file line number Diff line number Diff line change
    @@ -1,13 +0,0 @@
    The ex command g is very useful for acting on lines that match a pattern. You can use it with the d command, to delete all lines that contain a particular pattern, or all lines that do not contain a pattern.
    For example, to delete all lines containing "profile" (the first command is optional; it shows the lines that the second command will delete):

    :g/profile
    :g/profile/d
    More complex patterns can be used, such as deleting all lines that are empty or that contain only whitespace:
    :g/^\s*$/d
    To delete all lines that do not contain a pattern, use g!, like this command to delete all lines that are not comment lines in a Vim script:
    :g!/^\s*"/d
    Note that g! is equivalent to v, so you could also do the above with:
    :v/^\s*"/d
    The next example shows use of \| ("or") to delete all lines except those that contain "error" or "warn" or "fail" (:help pattern):
    :v/error\|warn\|fail/d
  3. shreeshga created this gist Jul 19, 2012.
    13 changes: 13 additions & 0 deletions delete_lines.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    The ex command g is very useful for acting on lines that match a pattern. You can use it with the d command, to delete all lines that contain a particular pattern, or all lines that do not contain a pattern.
    For example, to delete all lines containing "profile" (the first command is optional; it shows the lines that the second command will delete):

    :g/profile
    :g/profile/d
    More complex patterns can be used, such as deleting all lines that are empty or that contain only whitespace:
    :g/^\s*$/d
    To delete all lines that do not contain a pattern, use g!, like this command to delete all lines that are not comment lines in a Vim script:
    :g!/^\s*"/d
    Note that g! is equivalent to v, so you could also do the above with:
    :v/^\s*"/d
    The next example shows use of \| ("or") to delete all lines except those that contain "error" or "warn" or "fail" (:help pattern):
    :v/error\|warn\|fail/d