| expression | description |
|---|---|
| . (dot) | (dot) Any single character except newline |
| * (splat) | zero or more occurances of any character |
| [...] | Any single character specified in the set (refer to sets below) |
| [^...] | Any single character NOT specified in the set |
| \< | Anchor - Matches beginning of word |
| \> | Anchor - Matches end of word |
| ^ | Anchor - beginning of the line |
| $ | Anchor - end of line |
| \(...\) | Grouping - usually used to group conditions |
| \n | Contents of nth grouping |
| \ | Escapes the meaning of the next character |
| \$ | Escapes the $ character |
| \\ | Escapes the \ character |
| example | description |
|---|---|
| [A-Z] | SET contains capital letters A to Z. |
| [a-z] | SET contains lowercase letters a to z. |
| [0-9] | SET contains digits 0 to 9 (All numerals). |
| [./=+] | SET contains . (dot), / (slash), = (euqal), and + (plus). |
| [-A-F] | SET contains the dash, and capital letters A to F. (dashes must be specified first) |
| [0-9 A-Z] | SET contains all digits, a space, and capital letters A to Z. |
| [A-Z][a-zA-Z] | First character position SET contains capital letters A to Z. Second character position SET contains all letters. |
| [a-z]{m} | Look for m occurances from the SET of lowercase letters a to z. |
| [a-z]{m,n} | Look for at least m occurances, but no more than n occurances, from the SET of lowercase letters a to z. |
| example | description |
|---|---|
| /Hello/ | Matches if the line contains the value Hello |
| /^TEST$/ | Matches if the line contains TEST by itself |
| /^[a-zA-Z]/ | Matches if the line starts with any letter |
| /^[a-z].*/ | Matches if the first character of the line is a-z and there is at least one more of any character following it |
| /2134$/ | Matches if line ends with 2134 |
| /\(21|35\)/ | Matches if the line contains 21 or 35 Note the use of ( ) with the pipe symbol to specify the 'or' condition |
| /[0-9]*/ | Matches if there are zero or more numbers in the line |
| /^[^#]/ | Matches if the first character is not a # in the line |
| Notes: | |
| Regular expressions are case sensitive | |
| Regular expressions are to be used where pattern is specified | |
Ranges may precede most "colon" commands and cause them to be executed on a line or lines. For example :3,7d would delete lines 3-7. Ranges are commonly combined with the :s command to perform a replacement on several lines, as with :.,$s/pattern/string/g to make a replacement from the current line to the end of the file.
| range | description |
|---|---|
| :n,m | Range = Lines n-m |
| :50,100s | substitute lines 50 to 100 |
| :. | Range = Current line (colon and dot) |
| :$ | Range = Last line |
| :'c | Range = Marker c |
| :'a,'b | Range = Between Marker a and Marker b |
| :% | Range = All lines in file |
| :g/pattern/ | Range = All lines that contain pattern |
control what line(s) in file to pattern match
| address | description |
|---|---|
| :s | substitute (only current line) |
| :1,$s | substitute lines 1 to end of file |
| :%s | substitute all lines of file (same as ":1,$s") |
| :g | global (all lines of file, with search pattern) |
| address match | pattern to match within search line(s) |
| command | WHAT to do on matched line(s) (usually s) |
| flag(s) |
control how many time PER line command is executed
require confirmation before each replace ( c ) |
______________________________________________________________________________
:s/pattern/new-string/ replace first occurance of pattern on current line
______________________________________________________________________________
:s/pattern/new-string/g replace every occurance of pattern on current line
______________________________________________________________________________
:g/pattern/s//new-string/g replace every occurance of pattern with new-string
______________________________________________________________________________
:g/pattern/s/old-string/new-string/g find each pattern occurance, then
^ ^ ^ replace old-string with new-string,
| | | for every occurance on every line
global command global
range flag
______________________________________________________________________________
>> Global search and replace --> :1,$ s/old/new/g
^ ^ ^ ^ ^
In english, this means: | | | | |
| | | | |
From 1 to $ (end of file) | | | |
| | | |
substitute -----------------/ | | |
| | |
occurrences of "old" ----------/ | |
| |
with occurrences of "new" ---------/ |
|
globally (i.e., all instances of "old")
______________________________________________________________________________
| command_line | command description |
|---|---|
| :s/old/new/c |
after confirmation ([y] then <ENTER>)
current_line replace FIRST old match with new |
| :s/old/new/ | on current_line replace FIRST old match with new |
| :s/old/new/g | on current_line replace ALL old matches with new |
| :%s/old/new/ | on EVERY line replace FIRST old matches with new |
| :10,20s/old/new/ | lines 10-20 replace FIRST old match with new |
| :11,$s/old/new/ |
lines 11-EOF replace FIRST old match with new
|
|
:g/find/s/old/new/
:g/find/ s/old/new/ |
EVERY line containing find
replace FIRST old match with new |
| :g/find/s//new/ |
EVERY line containing find
replace FIRST find match with new ( s// = use find as old pattern ) |
| :%s/\$//g or :g/\$/s///g | EVERY line replace ALL "$" matches with NULL |
| :%s/^M//g or :g/^M/s//g | EVERY line replace ALL "^M" matches with NULL |
| :.,$s/$/append/ | current_line-EOF replace end_of_line with append |
| :.,$s/^/insert/ | current_line-EOF replace start_of_line with insert |
| :g/^[ ]*<tr><td>*/s/$/<\/td><\/tr>/g |
EVERY line replace ALL old matches with new
^[ ]*<tr><td>* = from start of line, zero or more spaces, the string "<tr><td>", then zero or more characters. s/$/<\/td><\/tr>/ = substitute end of line ($) with "</td></tr>" |
| Notes: | |
| EOF | End Of File |
| NULL | in effect, delete |
| :v/./d or :g/^$/d |
Delete all empty lines.
(Add match for blanks if required.) |
| \$ | $ escaped via \ |
| \/ | / escaped via \ |
| ^M | get ^M (carriage return) via <CTRL-V><CTRL-M> |