Regular Expressions
- Flags for Search and Replace
- Pattern atom
- Pattern Qualifiers
- Character Classes
- Multiple and Saving Patterns
- Word Boundary
- Search Pattern modifiers
- Changing Case using Search and Replace
- Delimiters in Search and Replace
- Further Reading
Flags for Search and Replace
greplace all occurrences within linecask for confirmation before each replacementiignore case for searchpatternIdon't ignore case for searchpattern
Flags can be combined
s/cat/Dog/gireplace every occurrence of cat (ignoring case, so it matches Cat, cAt, etc) with Dog (note thatidoesn't affect the replacement string Dog)
For more info, :h :s_flags
Pattern atom
^start matching from beginning of a line/^Thismatch This only at beginning of line
$match pattern should terminate at end of a line/)$match ) only at end of line/^$match empty line
.match any single character, excluding new line/c.tmatch 'cat' or 'cot' or 'c2t' or 'c^t' but not 'cant'
For more info, :h pattern-atoms
Pattern Qualifiers
*greedy match preceding character 0 or more times/abc*match 'ab' or 'abc' or 'abccc' or 'abcccccc' etc
\+greedy match preceding character 1 or more times/abc\+match 'abc' or 'abccc' but not 'ab'
\?match preceding character 0 or 1 times (\=can also be used)/abc\?match 'ab' or 'abc' but not 'abcc'
\{-}non-greedy match preceding character 0 or more times- Consider this line of text 'This is a sample text'
/h.\{-}swill match: 'his'/h.*swill match: 'his is a s'- Read more on non-greedy matching
\{min,max}greedy match preceding character min to max times (including min and max)- min or max can be left unspecified as they default to 0 and infinity respectively
- greedy match, tries to match as much as possible
\{-min,max}non-greedy match, tries to match as less as possible\{number}match exactly with specified number/c\{5}match exactly 'ccccc'
For more info, :h pattern-overview
Character Classes
[abcde]match any of 'a' or 'b' or 'c' or 'd' or 'e' ONE time- use
[a-e]as shortform
- use
[^abcde]match any character other than 'a' or 'b' or 'c' or 'd' or 'e'- use
[^a-e]as shortform
- use
[aeiou]match vowel character[^aeiou]match consonant character\amatches alphabet character, short-cut for[a-zA-Z]\Amatches other than alphabet[^a-zA-Z]\lmatches lowercase alphabets[a-z]\Lmatches other than lowercase alphabets[^a-z]\umatches uppercase alphabets[A-Z]\Umatches other than uppercase alphabets[^A-Z]\dmatches digit character[0-9]\Dmatches other than digit[^0-9]\xmatches hexademical character[0-9a-fA-F]\Xmatches other than hexademical[^0-9a-fA-F]\wmatches any alphanumeric character or underscore[a-zA-Z0-9_]\Wmatch other than alphanumeric character or underscore[^a-zA-Z0-9_]\smatches white-space characters space and tab\Smatches other than white-space characters\tused in replacestring to insert a Tab character\rused in replacestring to insert a newline character
For more info, :h /character-classes
Multiple and Saving Patterns
\|allows to specify two or more patterns to be matched/min\|maxmatch 'min' or 'max'
\(pattern\)allows to group matched patterns and use special variables\1,\2, etc to represent them in same searchpattern and/or replacestring when using substitute command/hand\(y\|ful\)match 'handy' or 'handful'/\(\a\)\1match repeated alphabets
Word Boundary
\<patternBind the searchpattern to necessarily be starting characters of a word/\<hismatches 'his' and 'history' but not 'this'
pattern\>Bind the searchpattern to necessarily be ending characters of a word/his\>matches 'his' and 'this' but not 'history'
\<pattern\>Bind the searchpattern to exactly match whole word/\<his\>matches 'his' and not 'this' or 'history'
Search Pattern modifiers
\vhelps to avoid\for pattern qualifiers, grouping pattern, etc/\vc{5}match exactly 'ccccc'/\vabc+match 'abc' or 'abccc' but not 'ab'/\vabc?match 'ab' or 'abc' but not 'abcc'/\v<his>match whole word 'his', not 'this' or 'history'/\vmin|maxmatch 'min' or 'max'/\vhand(y|ful)match 'handy' or 'handful'/\v(\a)\1match repeated alphabetss/\v(\d+) (\d+)/\2 \1/swap two numbers separated by space
\Vno need to use\when trying to match special characters/V^.*$match the exact sequence ^.*$
\ccase insensitive search/\cthismatches 'this', 'This', 'thiS', etc
\Ccase sensitive search/\Cthismatch exactly 'this', not 'This', 'thiS', etc
\%Vonly inside visually selected areas/\%Vcat/dog/greplace 'cat' with 'dog' only in visually selected region
For more info
Changing Case using Search and Replace
These are used in the replacestring section
\uuppercases the next character\UUPPERCASES the following characters\land\Lare equivalent for lowercase- use
\eand\Eto end further case changes
Example:
:% s/\v(\a+)/\u\1/gwill Capitalize all the words in current file (i.e only first character of word is capitalized):% s/\v(\a+)/\U\1/gwill change to all letters to UPPERCASE in current file:% s/\v(\w)_(\a+)/\1\u\2/gchange variable_name to camelCase- for ex: 'min_max' will change to 'minMax', 'array_sum' will change to 'arraySum' and so on
- Changing case with regular expressions
Delimiters in Search and Replace
One can also use other characters like #^$ instead of /
:% s#/project/adder/#/verilog/project/high_speed_adder/#gthis avoids mess of having to use\/for every/character
Further Reading
:h regular-expression- vimregex
- What does this regex mean?