Dup Ver Goto 📝

VIM Regex Guide

PT2/editor/vim/regex vim regex does not exist
To
77 lines, 318 words, 2410 chars Page 'Guide' does not exist.

Sources: vimregex.com learnbyexample.github vim.fandom

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

\<the\>                 # matches the if surrounded by non-word chars
\<the                   # the at start of word, their but not lathe
the\>                   # 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

const PtkKeys = { 
  EditKeys, ViewKeys, CommandKeys, GlobalKeys,
  EditChain, ViewChain, CommandChain,
  Selector
}

to

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