Vim tip 27: regexp anchors
By default, regexp matches anywhere in the text. You can use line and word anchors to specify additional restrictions regarding the position of matches. These restrictions are made possible by assigning special meaning to certain characters (metacharacters) and escape sequences.
^
restricts the match to the start-of-line^This
matchesThis is a sample
but notDo This
$
restricts the match to the end-of-line)$
matchesapple (5)
but notdef greeting():
^$
match empty line\<pattern
restricts the match to the start of a word- word characters include alphabets, digits and underscore
\<his
matcheshis
orto-his
orhistory
but notthis
or_hist
pattern\>
restricts the match to the end of a wordhis\>
matcheshis
orto-his
orthis
but nothistory
or_hist
\<pattern\>
restricts the match between start of a word and end of a word\<his\>
matcheshis
orto-his
but notthis
orhistory
or_hist
End-of-line can be \r
(carriage return), \n
(newline) or \r\n
depending on your system and fileformat
setting.
See :h pattern-atoms for more details.
Video demo:
See also my Vim Reference Guide and curated list of resources for Vim.