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 matches This is a sample but not Do This
  • $ restricts the match to the end-of-line
    • )$ matches apple (5) but not def greeting():
  • ^$ match empty line
  • \<pattern restricts the match to the start of a word
    • word characters include alphabets, digits and underscore
    • \<his matches his or to-his or history but not this or _hist
  • pattern\> restricts the match to the end of a word
    • his\> matches his or to-his or this but not history or _hist
  • \<pattern\> restricts the match between start of a word and end of a word
    • \<his\> matches his or to-his but not this or history or _hist

info End-of-line can be \r (carriage return), \n (newline) or \r\n depending on your system and fileformat setting.

info See :h pattern-atoms for more details.

Video demo:


info See also my Vim Reference Guide and curated list of resources for Vim.