- pattern.txt* For Vim version 9.1. Last change: 2024 Jun 03
VIM REFERENCE MANUAL by Bram Moolenaar
Patterns and search commands *pattern-searches*
The very basics can be found in section |03.9| of the user manual. A few more
explanations are in chapter 27 |usr_27.txt|.
1. Search commands |search-commands|
2. The definition of a pattern |search-pattern|
3. Magic |/magic|
4. Overview of pattern items |pattern-overview|
5. Multi items |pattern-multi-items|
6. Ordinary atoms |pattern-atoms|
7. Ignoring case in a pattern |/ignorecase|
8. Composing characters |patterns-composing|
9. Compare with Perl patterns |perl-patterns|
10. Highlighting matches |match-highlight|
11. Fuzzy matching |fuzzy-matching|
==============================================================================
1. Search commands *search-commands*
*/*
/{pattern}[/]<CR> Search forward for the [count]'th occurrence of
{pattern} |exclusive|.
/{pattern}/{offset}<CR> Search forward for the [count]'th occurrence of
{pattern} and go |{offset}| lines up or down.
|linewise|.
*/<CR>*
/<CR> Search forward for the [count]'th occurrence of the
latest used pattern |last-pattern| with latest used
|{offset}|.
//{offset}<CR> Search forward for the [count]'th occurrence of the
latest used pattern |last-pattern| with new
|{offset}|. If {offset} is empty no offset is used.
*?*
?{pattern}[?]<CR> Search backward for the [count]'th previous
occurrence of {pattern} |exclusive|.
?{pattern}?{offset}<CR> Search backward for the [count]'th previous
occurrence of {pattern} and go |{offset}| lines up or
down |linewise|.
*?<CR>*
?<CR> Search backward for the [count]'th occurrence of the
latest used pattern |last-pattern| with latest used
|{offset}|.
??{offset}<CR> Search backward for the [count]'th occurrence of the
latest used pattern |last-pattern| with new
|{offset}|. If {offset} is empty no offset is used.
*n*
n Repeat the latest "/" or "?" [count] times.
If the cursor doesn't move the search is repeated with
count + 1.
|last-pattern|
*N*
N Repeat the latest "/" or "?" [count] times in
opposite direction. |last-pattern|
*star* *E348* *E349*
- Search forward for the [count]'th occurrence of the
word nearest to the cursor. The word used for the
search is the first of:
1. the keyword under the cursor |'iskeyword'|
2. the first keyword after the cursor, in the
current line
3. the non-blank word under the cursor
4. the first non-blank word after the cursor,
in the current line
Only whole keywords are searched for, like with the
command "/\<keyword\>". |exclusive|
'ignorecase' is used, 'smartcase' is not.
*#*
# Same as "*", but search backward. The pound sign
(character 163) also works. If the "#" key works as
backspace, try using "stty erase <BS>" before starting
Vim (<BS> is CTRL-H or a real backspace).
*gstar*
g* Like "*", but don't put "\<" and "\>" around the word.
This makes the search also find matches that are not a
whole word.
*g#*
g# Like "#", but don't put "\<" and "\>" around the word.
This makes the search also find matches that are not a
whole word.
*gd*
gd Goto local Declaration. When the cursor is on a local
variable, this command will jump to its declaration.
This was made to work for C code, in other languages
it may not work well.
First Vim searches for the start of the current
function, just like "[[". If it is not found the
search stops in line 1. If it is found, Vim goes back
until a blank line is found. From this position Vim
searches for the keyword under the cursor, like with
"*", but lines that look like a comment are ignored
(see 'comments' option).
Note that this is not guaranteed to work, Vim does not
really check the syntax, it only searches for a match
with the keyword. If included files also need to be
searched use the commands listed in |include-search|.
After this command |n| searches forward for the next
match (not backward).
*gD*
gD Goto global Declaration. When the cursor is on a
global variable that is defined in the file, this
command will jump to its declaration. This works just
like "gd", except that the search for the keyword
always starts in line 1.
*1gd*
1gd Like "gd", but ignore matches inside a {} block that
ends before the cursor position.
*1gD*
1gD Like "gD", but ignore matches inside a {} block that
ends before the cursor position.
*CTRL-C*
CTRL-C Interrupt current (search) command. Use CTRL-Break on
MS-Windows |dos-CTRL-Break|.
In Normal mode, any pending command is aborted.
When Vim was started with output redirected and there
are no changed buffers CTRL-C exits Vim. That is to
help users who use "vim file | grep word" and don't
know how to get out (blindly typing :qa<CR> would
work).
*:noh* *:nohlsearch*
:noh[lsearch] Stop the highlighting for the 'hlsearch' option. It
is automatically turned back on when using a search
command, or setting the 'hlsearch' option.
This command doesn't work in an autocommand, because
the highlighting state is saved and restored when
executing autocommands |autocmd-searchpat|.
Same thing for when invoking a user function.
While typing the search pattern the current match will be shown if the
'incsearch' option is on. Remember that you still have to finish the search
command with <CR> to actually position the cursor at the displayed match. Or
use <Esc> to abandon the search.
All matches for the last used search pattern will be highlighted if you set
the 'hlsearch' option. This can be suspended with the |:nohlsearch| command.
When 'shortmess' does not include the "S" flag, Vim will automatically show an
index, on which the cursor is. This can look like this: >
[1/5] Cursor is on first of 5 matches.
[1/>99] Cursor is on first of more than 99 matches.
[>99/>99] Cursor is after 99 match of more than 99 matches.
[?/??] Unknown how many matches exists, generating the
statistics was aborted because of search timeout.
Note: the count does not take offset into account.
When no match is found you get the error: *E486* Pattern not found
Note that for the `:global` command, when used in legacy script, you get a
normal message "Pattern not found", for Vi compatibility.
In |Vim9| script you get E486 for "pattern not found" or *E538* when the pattern
matches in every line with `:vglobal`.
For the |:s| command the "e" flag can be used to avoid the error message
|:s_flags|.
*search-offset* *{offset}*
These commands search for the specified pattern. With "/" and "?" an
additional offset may be given. There are two types of offsets: line offsets
and character offsets.
The offset gives the cursor position relative to the found match:
[num] [num] lines downwards, in column 1
+[num] [num] lines downwards, in column 1
-[num] [num] lines upwards, in column 1
e[+num] [num] characters to the right of the end of the match
e[-num] [num] characters to the left of the end of the match
s[+num] [num] characters to the right of the start of the match
s[-num] [num] characters to the left of the start of the match
b[+num] [num] identical to s[+num] above (mnemonic: begin)
b[-num] [num] identical to s[-num] above (mnemonic: begin)
;{pattern} perform another search, see |//;|
If a '-' or '+' is given but [num] is omitted, a count of one will be used.
When including an offset with 'e', the search becomes inclusive (the
character the cursor lands on is included in operations).
Examples:
pattern cursor position ~
/test/+1 one line below "test", in column 1
/test/e on the last t of "test"
/test/s+2 on the 's' of "test"
/test/b-3 three characters before "test"
If one of these commands is used after an operator, the characters between
the cursor position before and after the search is affected. However, if a
line offset is given, the whole lines between the two cursor positions are
affected.
An example of how to search for matches with a pattern and change the match
with another word: >
/foo<CR> find "foo"
c//e<CR> change until end of match
bar<Esc> type replacement
//<CR> go to start of next match
c//e<CR> change until end of match
beep<Esc> type another replacement
etc.
<
*//;* *E386*
A very special offset is ';' followed by another search command. For example: >
/test 1/;/test
/test.*/+1;?ing?
The first one first finds the next occurrence of "test 1", and then the first
occurrence of "test" after that.
This is like executing two search commands after each other, except that:
- It can be used as a single motion command after an operator.
- The direction for a following "n" or "N" command comes from the first
search command.
- When an error occurs the cursor is not moved at all.
*last-pattern*
The last used pattern and offset are remembered. They can be used to repeat
the search, possibly in another direction or with another count. Note that
two patterns are remembered: One for "normal" search commands and one for the
substitute command ":s". Each time an empty pattern is given, the previously
used pattern is used. However, if there is no previous search command, a
previous substitute pattern is used, if possible.
The 'magic' option sticks with the last used pattern. If you change 'magic',
this will not change how the last used pattern will be interpreted.
The 'ignorecase' option does not do this. When 'ignorecase' is changed, it
will result in the pattern to match other text.
All matches for the last used search pattern will be highlighted if you set
the 'hlsearch' option.
To clear the last used search pattern: >
:let @/ = ""
This will not set the pattern to an empty string, because that would match
everywhere. The pattern is really cleared, like when starting Vim.
The search usually skips matches that don't move the cursor. Whether the next
match is found at the next character or after the skipped match depends on the
'c' flag in 'cpoptions'. See |cpo-c|.
with 'c' flag: "/..." advances 1 to 3 characters
without 'c' flag: "/..." advances 1 character
The unpredictability with the 'c' flag is caused by starting the search in the
first column, skipping matches until one is found past the cursor position.
When searching backwards, searching starts at the start of the line, using the
'c' flag in 'cpoptions' as described above. Then the last match before the
cursor position is used.
In Vi the ":tag" command sets the last search pattern when the tag is searched
for. In Vim this is not done, the previous search pattern is still remembered,
unless the 't' flag is present in 'cpoptions'. The search pattern is always
put in the search history.
If the 'wrapscan' option is on (which is the default), searches wrap around
the end of the buffer. If 'wrapscan' is not set, the backward search stops
at the beginning and the forward search stops at the end of the buffer. If
'wrapscan' is set and the pattern was not found the error message "pattern
not found" is given, and the cursor will not be moved. If 'wrapscan' is not
set the message becomes "search hit BOTTOM without match" when searching
forward, or "search hit TOP without match" when searching backward. If
wrapscan is set and the search wraps around the end of the file the message
"search hit TOP, continuing at BOTTOM" or "search hit BOTTOM, continuing at
TOP" is given when searching backwards or forwards respectively. This can be
switched off by setting the 's' flag in the 'shortmess' option. The highlight
method 'w' is used for this message (default: standout).
*search-range*
You can limit the search command "/" to a certain range of lines by including
\%>l items. For example, to match the word "limit" below line 199 and above
line 300: >
/\%>199l\%<300llimit
Also see |/\%>l|.
Another way is to use the ":substitute" command with the 'c' flag. Example: >
:.,300s/Pattern//gc
This command will search from the cursor position until line 300 for
"Pattern". At the match, you will be asked to type a character. Type 'q' to
stop at this match, type 'n' to find the next match.
The "*", "#", "g*" and "g#" commands look for a word near the cursor in this
order, the first one that is found is used:
- The keyword currently under the cursor.
- The first keyword to the right of the cursor, in the same line.
- The WORD currently under the cursor.
- The first WORD to the right of the cursor, in the same line.
The keyword may only contain letters and characters in 'iskeyword'.
The WORD may contain any non-blanks (<Tab>s and/or <Space>s).
Note that if you type with ten fingers, the characters are easy to remember:
the "#" is under your left hand middle finger (search to the left and up) and
the "*" is under your right hand middle finger (search to the right and down).
(this depends on your keyboard layout though).
*E956*
In very rare cases a regular expression is used recursively. This can happen
when executing a pattern takes a long time and when checking for messages on
channels a callback is invoked that also uses a pattern or an autocommand is
triggered. In most cases this should be fine, but if a pattern is in use when
it's used again it fails. Usually this means there is something wrong with
the pattern.
==============================================================================
2. The definition of a pattern *search-pattern* *pattern* *[pattern]*
*regular-expression* *regexp* *Pattern*
*E383* *E476*
For starters, read chapter 27 of the user manual |usr_27.txt|.
*/bar* */\bar* */pattern*
1. A pattern is one or more branches, separated by "\|". It matches anything
that matches one of the branches. Example: "foo\|beep" matches "foo" and
matches "beep". If more than one branch matches, the first one is used.
pattern ::= branch
or branch \| branch
or branch \| branch \| branch
etc.
*/branch* */\&*
2. A branch is one or more concats, separated by "\&". It matches the last
concat, but only if all the preceding concats also match at the same
position. Examples:
"foobeep\&..." matches "foo" in "foobeep".
".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
branch ::= concat
or concat \& concat
or concat \& concat \& concat
etc.
*/concat*
3. A concat is one or more pieces, concatenated. It matches a match for the
first piece, followed by a match for the second piece, etc. Example:
"f[0-9]b", first matches "f", then a digit and then "b".
concat ::= piece
or piece piece
or piece piece piece
etc.
*/piece*
4. A piece is an atom, possibly followed by a multi, an indication of how many
times the atom can be matched. Example: "a*" matches any sequence of "a"
characters: "", "a", "aa", etc. See |/multi|.
piece ::= atom
or atom multi
*/atom*
5. An atom can be one of a long list of items. Many atoms match one character
in the text. It is often an ordinary character or a character class.
Parentheses can be used to make a pattern into an atom. The "\z(\)"
construct is only for syntax highlighting.
atom ::= ordinary-atom |/ordinary-atom|
or \( pattern \) |/\(|
or \%( pattern \) |/\%(|
or \z( pattern \) |/\z(|
*/\%#=* *two-engines* *NFA*
Vim includes two regexp engines:
1. An old, backtracking engine that supports everything.
2. A new, NFA engine that works much faster on some patterns, possibly slower
on some patterns.
*E1281*
Vim will automatically select the right engine for you. However, if you run
into a problem or want to specifically select one engine or the other, you can
prepend one of the following to the pattern:
\%#=0 Force automatic selection. Only has an effect when
'regexpengine' has been set to a non-zero value.
\%#=1 Force using the old engine.
\%#=2 Force using the NFA engine.
You can also use the 'regexpengine' option to change the default.
*E864* *E868* *E874* *E875* *E876* *E877* *E878*
If selecting the NFA engine and it runs into something that is not implemented
the pattern will not match. This is only useful when debugging Vim.
==============================================================================
3. Magic */magic*
Some characters in the pattern, such as letters, are taken literally. They
match exactly the same character in the text. When preceded with a backslash
however, these characters may get a special meaning. For example, "a" matches
the letter "a", while "\a" matches any alphabetic character.
Other characters have a special meaning without a backslash. They need to be
preceded with a backslash to match literally. For example "." matches any
character while "\." matches a dot.
If a character is taken literally or not depends on the 'magic' option and the
items in the pattern mentioned next. The 'magic' option should always be set,
but it can be switched off for Vi compatibility. We mention the effect of
'nomagic' here for completeness, but we recommend against using that.
*/\m* */\M*
Use of "\m" makes the pattern after it be interpreted as if 'magic' is set,
ignoring the actual value of the 'magic' option.
Use of "\M" makes the pattern after it be interpreted as if 'nomagic' is used.
*/\v* */\V*
Use of "\v" means that after it, all ASCII characters except '0'-'9', 'a'-'z',
'A'-'Z' and '_' have special meaning: "very magic"
Use of "\V" means that after it, only a backslash and the terminating
character (usually / or ?) have special meaning: "very nomagic"
Examples:
after: \v \m \M \V matches ~
'magic' 'nomagic'
a a a a literal 'a'
\a \a \a \a any alphabetic character
. . \. \. any character
\. \. . . literal dot
$ $ $ \$ end-of-line
* * \* \* any number of the previous atom
~ ~ \~ \~ latest substitute string
() \(\) \(\) \(\) group as an atom
| \| \| \| nothing: separates alternatives
\\ \\ \\ \\ literal backslash
\{ { { { literal curly brace
{only Vim supports \m, \M, \v and \V}
If you want to you can make a pattern immune to the 'magic' option being set
or not by putting "\m" or "\M" at the start of the pattern.
==============================================================================
4. Overview of pattern items *pattern-overview*
*E865* *E866* *E867* *E869*
Overview of multi items. */multi* *E61* *E62*
More explanation and examples below, follow the links. *E64* *E871*
multi ~
'magic' 'nomagic' matches of the preceding atom ~
|/star| * \* 0 or more as many as possible
|/\+| \+ \+ 1 or more as many as possible
|/\=| \= \= 0 or 1 as many as possible
|/\?| \? \? 0 or 1 as many as possible
|/\{| \{n,m} \{n,m} n to m as many as possible
\{n} \{n} n exactly
\{n,} \{n,} at least n as many as possible
\{,m} \{,m} 0 to m as many as possible
\{} \{} 0 or more as many as possible (same as *)
|/\{-| \{-n,m} \{-n,m} n to m as few as possible
\{-n} \{-n} n exactly
\{-n,} \{-n,} at least n as few as possible
\{-,m} \{-,m} 0 to m as few as possible
\{-} \{-} 0 or more as few as possible
*E59*
|/\@>| \@> \@> 1, like matching a whole pattern
|/\@=| \@= \@= nothing, requires a match |/zero-width|
|/\@!| \@! \@! nothing, requires NO match |/zero-width|
|/\@<=| \@<= \@<= nothing, requires a match behind |/zero-width|
|/\@<!| \@<! \@<! nothing, requires NO match behind |/zero-width|
Overview of ordinary atoms. */ordinary-atom*
More explanation and examples below, follow the links.
ordinary atom ~
magic nomagic matches ~
|/^| ^ ^ start-of-line (at start of pattern) |/zero-width|
|/\^| \^ \^ literal '^'
|/\_^| \_^ \_^ start-of-line (used anywhere) |/zero-width|
|/$| $ $ end-of-line (at end of pattern) |/zero-width|
|/\$| \$ \$ literal '