tags: vim regex title: VIM Regex Guide Sources: [vimregex.com](https://vimregex.com/) [learnbyexample.github](https://learnbyexample.github.io/vim_reference/Regular-Expressions.html) [vim.fandom](https://vim.fandom.com/wiki/Regex_lookahead_and_lookbehind) ## Quantifiers ### Greedy ``` * match zero or more times \+ match one or more times \? match zero or one times \= can also be used, helpful if you are searching backwards with the ? command \{m,n} match m to n times (inclusive) \{m,} match at least m times \{,n} match up to n times (including 0 times) \{n} match exactly n times ``` ### Non Greedy ``` \{-} # matches 0 or more of the preceding atom, as few as possible \{-n,m} # matches 1 or more of the preceding characters... \{-n,} # matches at lease or more of the preceding characters... \{-,m} # matches 1 or more of the preceding characters... ``` ## Anchors, Lookahead, Lookbehind ``` \ # matches the if surrounded by non-word chars \ # the at end of word, lathe but not their hello\zsworld\zeflibble # matched text will be world, only if hello before and flibble after ``` ## Grouping ``` \(...\) # group \1 \2 \3 ... # backreference to group n ~ # previous substitute string ``` So if we replace `:s/hello/world` then `:s/pattern/~` is equivalent to `:s/pattern/world` and `:s/pattern/~_~` is equivalent to `:s/pattern/world_world`. ## Replacing ``` \U...\E # make uppercase \L...\E # make lowercase \u # make next char uppercase \l # make next char lowercase \r # split line ``` ## Example ### Changing case of first character of word To change case, use `\l` To get from ```js const PtkKeys = { EditKeys, ViewKeys, CommandKeys, GlobalKeys, EditChain, ViewChain, CommandChain, Selector } ``` to ```js const ptkKeys = { editKeys, viewKeys, commandKeys, globalKeys, editChain, viewChain, commandChain, selector } ``` select lines with visual and use ``` :s/\w\+/\l&/g " or to do it less elegantly :s/\(\w\+\)/\l\1/g " or the even uglier :'<,'>s/\(\s\)\([A-Z]\)/\1\l\2/g ``` ## Links * [vim.fandom: Changing Case with Regular Expressions](https://vim.fandom.com/wiki/Changing_case_with_regular_expressions)