Home Explore Blog CI



neovim

20th chunk of `runtime/doc/pattern.txt`
a5ded448e489f37b52a0b64a197fecbd3c9c823d88b993880000000100000ced
 and end of the text,
by default, but you can set the 'm' flag, which lets them match at
embedded newlines as well.  You can also set the 's' flag, which causes
a . to match newlines as well.  (Both these flags can be changed inside
a pattern using the same syntax used for the i flag above, BTW.)

On the other hand, Vim's ^ and $ always match at embedded newlines, and
you get two separate atoms, \%^ and \%$, which only match at the very
start and end of the text, respectively.  Vim solves the second problem
by giving you the \_ "modifier":  put it in front of a . or a character
class, and they will match newlines as well.

Finally, these constructs are unique to Perl:
- execution of arbitrary code in the regex:  (?{perl code})
- conditional expressions:  (?(condition)true-expr|false-expr)

...and these are unique to Vim:
- changing the magic-ness of a pattern:  \v \V \m \M
   (very useful for avoiding backslashitis)
- sequence of optionally matching atoms:  \%[atoms]
- \& (which is to \| what "and" is to "or";  it forces several branches
   to match at one spot)
- matching lines/columns by number:  \%5l \%5c \%5v
- setting the start and end of the match:  \zs \ze

==============================================================================
10. Highlighting matches				*match-highlight*

							*syntax-vs-match*
		Note that the match highlight mechanism is independent
		of |syntax-highlighting|, which is (usually) a buffer-local
		highlighting, while matching is window-local, both methods
		can be freely mixed.  Match highlighting functions give you
		a bit more flexibility in when and how to apply, but are
		typically only used for temporary highlighting, without strict
		rules.  Both methods can be used to conceal text.

		Thus the matching functions like |matchadd()| won't consider
		syntax rules and functions like |synconcealed()| and the
		other way around.

							*:mat* *:match*
:mat[ch] {group} /{pattern}/
		Define a pattern to highlight in the current window.  It will
		be highlighted with {group}.  Example: >
			:highlight MyGroup ctermbg=green guibg=green
			:match MyGroup /TODO/
<		Instead of // any character can be used to mark the start and
		end of the {pattern}.  Watch out for using special characters,
		such as '"' and '|'.

		{group} must exist at the moment this command is executed.

		The {group} highlighting still applies when a character is
		to be highlighted for 'hlsearch', as the highlighting for
		matches is given higher priority than that of 'hlsearch'.
		Syntax highlighting (see 'syntax') is also overruled by
		matches.

		Note that highlighting the last used search pattern with
		'hlsearch' is used in all windows, while the pattern defined
		with ":match" only exists in the current window.  It is kept
		when switching to another buffer.

		'ignorecase' does not apply, use |/\c| in the pattern to
		ignore case.  Otherwise case is not ignored.

		'redrawtime' defines the maximum time searched for pattern
		matches.

		When matching end-of-line and Vim redraws only part of the
		display you may get unexpected results.  That is because Vim
		looks for a match in the line where redrawing starts.

		Also see |matcharg()| and |getmatches()|.  The former returns
		the highlight group and pattern of a previous |:match|
		command.

Title: Vim Regex: Perl Comparison, Unique Vim Constructs, and Match Highlighting
Summary
This section discusses the differences between Perl and Vim regex, focusing on newline matching and unique constructs. Perl has flags 'm' and 's' to handle newlines, while Vim uses \%^ and \%$ for start/end of text and \_ for matching newlines. Unique Perl features include code execution and conditional expressions, while Vim offers magic-ness control (\v \V \m \M), optional matching sequences (\%[atoms]), the \& operator for forcing multiple branches to match, line/column matching, and setting match start/end. It then details the match highlighting mechanism in Vim, independent of syntax highlighting, and introduces the `:match` command to highlight patterns in the current window with a specified group.