Home‎ > ‎unix/linux‎ > ‎


(copying in case it gets moved)

# Matching # Matching
. any character except new line    
\s whitespace character \S non-whitespace character
\d digit \D non-digit
\x hex digit \X non-hex digit
\o octal digit \O non-octal digit
\h head of word character (a,b,c...z,A,B,C...Z and _) \H non-head of word character
\p printable character \P like \p, but excluding digits
\w word character \W non-word character
\a alphabetic character \A non-alphabetic character
\l lowercase character \L non-lowercase character
\u uppercase character \U non-uppercase character

Quantifier Description
* matches 0 or more of the preceding characters, ranges or metacharacters .* matches everything including empty line
\+ matches 1 or more of the preceding characters...
\= matches 0 or 1 more of the preceding characters...
\{n,m} matches from n to m of the preceding characters...
\{n} matches exactly n times of the preceding characters...
\{,m} matches at most m (from 0 to m) of the preceding characters...
\{n,} matches at least n of of the preceding characters...
\({data}\) takes {data} and puts it into register \1


Simple Find and Replace: 

Search for text cool and replace it with groovy. Do this as many times as necessary for each line.
 was      is
 I am cool
cool cool cool
 I am groovy
groovy groovy groovy


Add a carriage return before the word text. Only do this once per line.
was      is
 text is written here
 this is more text
 text is written here
 this is more


find any number matching a pattern

Search for all dates (ie: 5-20-2000), and delete: 
was      is


find and replace with regex "word 123" with "word    123" and use grouping brackets

was      is
# something
number 5
 # something
number     5
:%s/\(\w\) \(\d\)/\1\t\2/

Find some non-printed stuff

Delete all empty lines: 
was      is
# something

more text here
 # something
more text here


Delete all empty lines that contain only white spaces
(same as above with spaces)  

Find, copy, and paste

find some text and store it in register $1, then paste it later

was      is
interface GigabitEthernet1/0/1
 description switch
GigabitEthernet1/0/1, description switch

:%s/interface \(\S\+\)\n / \1, / 

Delete lines containing match

any line with the word match should be deleted


if you create a text file (in this example called "xl.vim", and in this example putting it in your home directory), and in that text file put a bunch of search and replace statements that you want to do (like such): 
:%s/<t>/  /g

then in your current file that you want to run this script against, call the script by the following command: 
:source ~/xl.vim

Keyboard Cheat Sheet:

Quick list of what each key does:

also note:
[Ctrl-R]: Redo changes which were undone (undo the undos)

Configuration Settings: 


Config files and good standards for them.