Vi search and replace tricks

Double spacing:

  • :%s/$/{ctrl-V}{CR}/g
    This command applies an extra carriage return at the end of all lines

Strip blanks at end of line:

  • :%s/{TAB}*$//

Delete all lines beginning with or matching a pattern:

  • :1,$ /^#/d
    Delete all (first to last line: 1,$ or g) comments lines in file. Delete all lines beginning (^) with “#” (specify text pattern).
  • :g/#/d
    Delete all lines (g) containing comments (comments follow “#”) in file. Delete all lines containing “#”.
  • :g!/^#/d
    Delete all lines except (g! or v) comment lines beginning (^) with “#”.

Strip DOS ctrl-M’s:

  • :1,$ s/{ctrl-V}{ctrl-M}//
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s