Bram Moolenaar | 1aeaf8c | 2012-05-18 13:46:39 +0200 | [diff] [blame] | 1 | *pattern.txt* For Vim version 7.3. Last change: 2012 May 18 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | |
| 3 | |
| 4 | VIM REFERENCE MANUAL by Bram Moolenaar |
| 5 | |
| 6 | |
| 7 | Patterns and search commands *pattern-searches* |
| 8 | |
| 9 | The very basics can be found in section |03.9| of the user manual. A few more |
| 10 | explanations are in chapter 27 |usr_27.txt|. |
| 11 | |
| 12 | 1. Search commands |search-commands| |
| 13 | 2. The definition of a pattern |search-pattern| |
| 14 | 3. Magic |/magic| |
| 15 | 4. Overview of pattern items |pattern-overview| |
| 16 | 5. Multi items |pattern-multi-items| |
| 17 | 6. Ordinary atoms |pattern-atoms| |
| 18 | 7. Ignoring case in a pattern |/ignorecase| |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 19 | 8. Composing characters |patterns-composing| |
| 20 | 9. Compare with Perl patterns |perl-patterns| |
| 21 | 10. Highlighting matches |match-highlight| |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 22 | |
| 23 | ============================================================================== |
Bram Moolenaar | 1514667 | 2011-10-20 22:22:38 +0200 | [diff] [blame] | 24 | 1. Search commands *search-commands* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 25 | |
| 26 | */* |
| 27 | /{pattern}[/]<CR> Search forward for the [count]'th occurrence of |
| 28 | {pattern} |exclusive|. |
| 29 | |
| 30 | /{pattern}/{offset}<CR> Search forward for the [count]'th occurrence of |
| 31 | {pattern} and go |{offset}| lines up or down. |
| 32 | |linewise|. |
| 33 | |
| 34 | */<CR>* |
Bram Moolenaar | 8f3f58f | 2010-01-06 20:52:26 +0100 | [diff] [blame] | 35 | /<CR> Search forward for the [count]'th occurrence of the |
| 36 | latest used pattern |last-pattern| with latest used |
| 37 | |{offset}|. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 38 | |
Bram Moolenaar | 8f3f58f | 2010-01-06 20:52:26 +0100 | [diff] [blame] | 39 | //{offset}<CR> Search forward for the [count]'th occurrence of the |
| 40 | latest used pattern |last-pattern| with new |
| 41 | |{offset}|. If {offset} is empty no offset is used. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 42 | |
| 43 | *?* |
| 44 | ?{pattern}[?]<CR> Search backward for the [count]'th previous |
| 45 | occurrence of {pattern} |exclusive|. |
| 46 | |
| 47 | ?{pattern}?{offset}<CR> Search backward for the [count]'th previous |
| 48 | occurrence of {pattern} and go |{offset}| lines up or |
| 49 | down |linewise|. |
| 50 | |
| 51 | *?<CR>* |
Bram Moolenaar | 8f3f58f | 2010-01-06 20:52:26 +0100 | [diff] [blame] | 52 | ?<CR> Search backward for the [count]'th occurrence of the |
| 53 | latest used pattern |last-pattern| with latest used |
| 54 | |{offset}|. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 55 | |
Bram Moolenaar | 8f3f58f | 2010-01-06 20:52:26 +0100 | [diff] [blame] | 56 | ??{offset}<CR> Search backward for the [count]'th occurrence of the |
| 57 | latest used pattern |last-pattern| with new |
| 58 | |{offset}|. If {offset} is empty no offset is used. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 59 | |
| 60 | *n* |
| 61 | n Repeat the latest "/" or "?" [count] times. |
| 62 | |last-pattern| {Vi: no count} |
| 63 | |
| 64 | *N* |
| 65 | N Repeat the latest "/" or "?" [count] times in |
| 66 | opposite direction. |last-pattern| {Vi: no count} |
| 67 | |
| 68 | *star* *E348* *E349* |
| 69 | * Search forward for the [count]'th occurrence of the |
| 70 | word nearest to the cursor. The word used for the |
| 71 | search is the first of: |
| 72 | 1. the keyword under the cursor |'iskeyword'| |
| 73 | 2. the first keyword after the cursor, in the |
| 74 | current line |
| 75 | 3. the non-blank word under the cursor |
| 76 | 4. the first non-blank word after the cursor, |
| 77 | in the current line |
| 78 | Only whole keywords are searched for, like with the |
| 79 | command "/\<keyword\>". |exclusive| {not in Vi} |
| 80 | 'ignorecase' is used, 'smartcase' is not. |
| 81 | |
| 82 | *#* |
| 83 | # Same as "*", but search backward. The pound sign |
| 84 | (character 163) also works. If the "#" key works as |
| 85 | backspace, try using "stty erase <BS>" before starting |
| 86 | Vim (<BS> is CTRL-H or a real backspace). {not in Vi} |
| 87 | |
| 88 | *gstar* |
| 89 | g* Like "*", but don't put "\<" and "\>" around the word. |
| 90 | This makes the search also find matches that are not a |
| 91 | whole word. {not in Vi} |
| 92 | |
| 93 | *g#* |
| 94 | g# Like "#", but don't put "\<" and "\>" around the word. |
| 95 | This makes the search also find matches that are not a |
| 96 | whole word. {not in Vi} |
| 97 | |
| 98 | *gd* |
| 99 | gd Goto local Declaration. When the cursor is on a local |
| 100 | variable, this command will jump to its declaration. |
| 101 | First Vim searches for the start of the current |
| 102 | function, just like "[[". If it is not found the |
| 103 | search stops in line 1. If it is found, Vim goes back |
| 104 | until a blank line is found. From this position Vim |
| 105 | searches for the keyword under the cursor, like with |
| 106 | "*", but lines that look like a comment are ignored |
| 107 | (see 'comments' option). |
| 108 | Note that this is not guaranteed to work, Vim does not |
| 109 | really check the syntax, it only searches for a match |
| 110 | with the keyword. If included files also need to be |
| 111 | searched use the commands listed in |include-search|. |
| 112 | After this command |n| searches forward for the next |
| 113 | match (not backward). |
| 114 | {not in Vi} |
| 115 | |
| 116 | *gD* |
| 117 | gD Goto global Declaration. When the cursor is on a |
| 118 | global variable that is defined in the file, this |
| 119 | command will jump to its declaration. This works just |
| 120 | like "gd", except that the search for the keyword |
| 121 | always starts in line 1. {not in Vi} |
| 122 | |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 123 | *1gd* |
| 124 | 1gd Like "gd", but ignore matches inside a {} block that |
| 125 | ends before the cursor position. {not in Vi} |
| 126 | |
| 127 | *1gD* |
| 128 | 1gD Like "gD", but ignore matches inside a {} block that |
| 129 | ends before the cursor position. {not in Vi} |
| 130 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 131 | *CTRL-C* |
| 132 | CTRL-C Interrupt current (search) command. Use CTRL-Break on |
| 133 | MS-DOS |dos-CTRL-Break|. |
| 134 | In Normal mode, any pending command is aborted. |
| 135 | |
| 136 | *:noh* *:nohlsearch* |
| 137 | :noh[lsearch] Stop the highlighting for the 'hlsearch' option. It |
| 138 | is automatically turned back on when using a search |
| 139 | command, or setting the 'hlsearch' option. |
| 140 | This command doesn't work in an autocommand, because |
| 141 | the highlighting state is saved and restored when |
| 142 | executing autocommands |autocmd-searchpat|. |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 143 | Same thing for when invoking a user function. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 144 | |
| 145 | While typing the search pattern the current match will be shown if the |
| 146 | 'incsearch' option is on. Remember that you still have to finish the search |
| 147 | command with <CR> to actually position the cursor at the displayed match. Or |
| 148 | use <Esc> to abandon the search. |
| 149 | |
| 150 | All matches for the last used search pattern will be highlighted if you set |
| 151 | the 'hlsearch' option. This can be suspended with the |:nohlsearch| command. |
| 152 | |
Bram Moolenaar | 1514667 | 2011-10-20 22:22:38 +0200 | [diff] [blame] | 153 | When no match is found you get the error: *E486* Pattern not found |
| 154 | Note that for the |:global| command this behaves like a normal message, for Vi |
| 155 | compatibility. For the |:s| command the "e" flag can be used to avoid the |
| 156 | error message |:s_flags|. |
| 157 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 158 | *search-offset* *{offset}* |
| 159 | These commands search for the specified pattern. With "/" and "?" an |
| 160 | additional offset may be given. There are two types of offsets: line offsets |
| 161 | and character offsets. {the character offsets are not in Vi} |
| 162 | |
| 163 | The offset gives the cursor position relative to the found match: |
| 164 | [num] [num] lines downwards, in column 1 |
| 165 | +[num] [num] lines downwards, in column 1 |
| 166 | -[num] [num] lines upwards, in column 1 |
| 167 | e[+num] [num] characters to the right of the end of the match |
| 168 | e[-num] [num] characters to the left of the end of the match |
| 169 | s[+num] [num] characters to the right of the start of the match |
| 170 | s[-num] [num] characters to the left of the start of the match |
| 171 | b[+num] [num] identical to s[+num] above (mnemonic: begin) |
| 172 | b[-num] [num] identical to s[-num] above (mnemonic: begin) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 173 | ;{pattern} perform another search, see |//;| |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 174 | |
| 175 | If a '-' or '+' is given but [num] is omitted, a count of one will be used. |
| 176 | When including an offset with 'e', the search becomes inclusive (the |
| 177 | character the cursor lands on is included in operations). |
| 178 | |
| 179 | Examples: |
| 180 | |
| 181 | pattern cursor position ~ |
| 182 | /test/+1 one line below "test", in column 1 |
| 183 | /test/e on the last t of "test" |
| 184 | /test/s+2 on the 's' of "test" |
| 185 | /test/b-3 three characters before "test" |
| 186 | |
| 187 | If one of these commands is used after an operator, the characters between |
| 188 | the cursor position before and after the search is affected. However, if a |
| 189 | line offset is given, the whole lines between the two cursor positions are |
| 190 | affected. |
| 191 | |
| 192 | An example of how to search for matches with a pattern and change the match |
| 193 | with another word: > |
| 194 | /foo<CR> find "foo" |
| 195 | c//e change until end of match |
| 196 | bar<Esc> type replacement |
| 197 | //<CR> go to start of next match |
| 198 | c//e change until end of match |
| 199 | beep<Esc> type another replacement |
| 200 | etc. |
| 201 | < |
| 202 | *//;* *E386* |
| 203 | A very special offset is ';' followed by another search command. For example: > |
| 204 | |
| 205 | /test 1/;/test |
| 206 | /test.*/+1;?ing? |
| 207 | |
| 208 | The first one first finds the next occurrence of "test 1", and then the first |
| 209 | occurrence of "test" after that. |
| 210 | |
| 211 | This is like executing two search commands after each other, except that: |
| 212 | - It can be used as a single motion command after an operator. |
| 213 | - The direction for a following "n" or "N" command comes from the first |
| 214 | search command. |
| 215 | - When an error occurs the cursor is not moved at all. |
| 216 | |
| 217 | *last-pattern* |
| 218 | The last used pattern and offset are remembered. They can be used to repeat |
| 219 | the search, possibly in another direction or with another count. Note that |
| 220 | two patterns are remembered: One for 'normal' search commands and one for the |
| 221 | substitute command ":s". Each time an empty pattern is given, the previously |
Bram Moolenaar | 662db67 | 2011-03-22 14:05:35 +0100 | [diff] [blame] | 222 | used pattern is used. However, if there is no previous search command, a |
| 223 | previous substitute pattern is used, if possible. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 224 | |
| 225 | The 'magic' option sticks with the last used pattern. If you change 'magic', |
| 226 | this will not change how the last used pattern will be interpreted. |
| 227 | The 'ignorecase' option does not do this. When 'ignorecase' is changed, it |
| 228 | will result in the pattern to match other text. |
| 229 | |
| 230 | All matches for the last used search pattern will be highlighted if you set |
| 231 | the 'hlsearch' option. |
| 232 | |
| 233 | To clear the last used search pattern: > |
| 234 | :let @/ = "" |
| 235 | This will not set the pattern to an empty string, because that would match |
| 236 | everywhere. The pattern is really cleared, like when starting Vim. |
| 237 | |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 238 | The search usually skips matches that don't move the cursor. Whether the next |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 239 | match is found at the next character or after the skipped match depends on the |
| 240 | 'c' flag in 'cpoptions'. See |cpo-c|. |
| 241 | with 'c' flag: "/..." advances 1 to 3 characters |
| 242 | without 'c' flag: "/..." advances 1 character |
| 243 | The unpredictability with the 'c' flag is caused by starting the search in the |
| 244 | first column, skipping matches until one is found past the cursor position. |
| 245 | |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 246 | When searching backwards, searching starts at the start of the line, using the |
| 247 | 'c' flag in 'cpoptions' as described above. Then the last match before the |
| 248 | cursor position is used. |
| 249 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 250 | In Vi the ":tag" command sets the last search pattern when the tag is searched |
| 251 | for. In Vim this is not done, the previous search pattern is still remembered, |
| 252 | unless the 't' flag is present in 'cpoptions'. The search pattern is always |
| 253 | put in the search history. |
| 254 | |
| 255 | If the 'wrapscan' option is on (which is the default), searches wrap around |
| 256 | the end of the buffer. If 'wrapscan' is not set, the backward search stops |
| 257 | at the beginning and the forward search stops at the end of the buffer. If |
| 258 | 'wrapscan' is set and the pattern was not found the error message "pattern |
| 259 | not found" is given, and the cursor will not be moved. If 'wrapscan' is not |
| 260 | set the message becomes "search hit BOTTOM without match" when searching |
| 261 | forward, or "search hit TOP without match" when searching backward. If |
| 262 | wrapscan is set and the search wraps around the end of the file the message |
| 263 | "search hit TOP, continuing at BOTTOM" or "search hit BOTTOM, continuing at |
| 264 | TOP" is given when searching backwards or forwards respectively. This can be |
| 265 | switched off by setting the 's' flag in the 'shortmess' option. The highlight |
| 266 | method 'w' is used for this message (default: standout). |
| 267 | |
| 268 | *search-range* |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 269 | You can limit the search command "/" to a certain range of lines by including |
| 270 | \%>l items. For example, to match the word "limit" below line 199 and above |
| 271 | line 300: > |
| 272 | /\%>199l\%<300llimit |
| 273 | Also see |/\%>l|. |
| 274 | |
| 275 | Another way is to use the ":substitute" command with the 'c' flag. Example: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 276 | :.,300s/Pattern//gc |
| 277 | This command will search from the cursor position until line 300 for |
| 278 | "Pattern". At the match, you will be asked to type a character. Type 'q' to |
| 279 | stop at this match, type 'n' to find the next match. |
| 280 | |
| 281 | The "*", "#", "g*" and "g#" commands look for a word near the cursor in this |
| 282 | order, the first one that is found is used: |
| 283 | - The keyword currently under the cursor. |
| 284 | - The first keyword to the right of the cursor, in the same line. |
| 285 | - The WORD currently under the cursor. |
| 286 | - The first WORD to the right of the cursor, in the same line. |
| 287 | The keyword may only contain letters and characters in 'iskeyword'. |
| 288 | The WORD may contain any non-blanks (<Tab>s and/or <Space>s). |
| 289 | Note that if you type with ten fingers, the characters are easy to remember: |
| 290 | the "#" is under your left hand middle finger (search to the left and up) and |
| 291 | the "*" is under your right hand middle finger (search to the right and down). |
| 292 | (this depends on your keyboard layout though). |
| 293 | |
| 294 | ============================================================================== |
| 295 | 2. The definition of a pattern *search-pattern* *pattern* *[pattern]* |
| 296 | *regular-expression* *regexp* *Pattern* |
Bram Moolenaar | f1f8bc5 | 2005-03-07 23:20:08 +0000 | [diff] [blame] | 297 | *E76* *E383* *E476* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 298 | |
| 299 | For starters, read chapter 27 of the user manual |usr_27.txt|. |
| 300 | |
| 301 | */bar* */\bar* */pattern* |
| 302 | 1. A pattern is one or more branches, separated by "\|". It matches anything |
| 303 | that matches one of the branches. Example: "foo\|beep" matches "foo" and |
| 304 | matches "beep". If more than one branch matches, the first one is used. |
| 305 | |
| 306 | pattern ::= branch |
| 307 | or branch \| branch |
| 308 | or branch \| branch \| branch |
| 309 | etc. |
| 310 | |
| 311 | */branch* */\&* |
| 312 | 2. A branch is one or more concats, separated by "\&". It matches the last |
| 313 | concat, but only if all the preceding concats also match at the same |
| 314 | position. Examples: |
| 315 | "foobeep\&..." matches "foo" in "foobeep". |
| 316 | ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob" |
| 317 | |
| 318 | branch ::= concat |
| 319 | or concat \& concat |
| 320 | or concat \& concat \& concat |
| 321 | etc. |
| 322 | |
| 323 | */concat* |
| 324 | 3. A concat is one or more pieces, concatenated. It matches a match for the |
| 325 | first piece, followed by a match for the second piece, etc. Example: |
| 326 | "f[0-9]b", first matches "f", then a digit and then "b". |
| 327 | |
| 328 | concat ::= piece |
| 329 | or piece piece |
| 330 | or piece piece piece |
| 331 | etc. |
| 332 | |
| 333 | */piece* |
| 334 | 4. A piece is an atom, possibly followed by a multi, an indication of how many |
| 335 | times the atom can be matched. Example: "a*" matches any sequence of "a" |
| 336 | characters: "", "a", "aa", etc. See |/multi|. |
| 337 | |
| 338 | piece ::= atom |
| 339 | or atom multi |
| 340 | |
| 341 | */atom* |
| 342 | 5. An atom can be one of a long list of items. Many atoms match one character |
| 343 | in the text. It is often an ordinary character or a character class. |
| 344 | Braces can be used to make a pattern into an atom. The "\z(\)" construct |
| 345 | is only for syntax highlighting. |
| 346 | |
| 347 | atom ::= ordinary-atom |/ordinary-atom| |
| 348 | or \( pattern \) |/\(| |
| 349 | or \%( pattern \) |/\%(| |
| 350 | or \z( pattern \) |/\z(| |
| 351 | |
| 352 | |
| 353 | ============================================================================== |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 354 | 3. Magic */magic* |
| 355 | |
| 356 | Some characters in the pattern are taken literally. They match with the same |
| 357 | character in the text. When preceded with a backslash however, these |
| 358 | characters get a special meaning. |
| 359 | |
| 360 | Other characters have a special meaning without a backslash. They need to be |
| 361 | preceded with a backslash to match literally. |
| 362 | |
| 363 | If a character is taken literally or not depends on the 'magic' option and the |
| 364 | items mentioned next. |
| 365 | */\m* */\M* |
| 366 | Use of "\m" makes the pattern after it be interpreted as if 'magic' is set, |
| 367 | ignoring the actual value of the 'magic' option. |
| 368 | Use of "\M" makes the pattern after it be interpreted as if 'nomagic' is used. |
| 369 | */\v* */\V* |
| 370 | Use of "\v" means that in the pattern after it all ASCII characters except |
| 371 | '0'-'9', 'a'-'z', 'A'-'Z' and '_' have a special meaning. "very magic" |
| 372 | |
| 373 | Use of "\V" means that in the pattern after it only the backslash has a |
| 374 | special meaning. "very nomagic" |
| 375 | |
| 376 | Examples: |
| 377 | after: \v \m \M \V matches ~ |
| 378 | 'magic' 'nomagic' |
| 379 | $ $ $ \$ matches end-of-line |
| 380 | . . \. \. matches any character |
| 381 | * * \* \* any number of the previous atom |
| 382 | () \(\) \(\) \(\) grouping into an atom |
| 383 | | \| \| \| separating alternatives |
| 384 | \a \a \a \a alphabetic character |
| 385 | \\ \\ \\ \\ literal backslash |
| 386 | \. \. . . literal dot |
| 387 | \{ { { { literal '{' |
| 388 | a a a a literal 'a' |
| 389 | |
| 390 | {only Vim supports \m, \M, \v and \V} |
| 391 | |
| 392 | It is recommended to always keep the 'magic' option at the default setting, |
| 393 | which is 'magic'. This avoids portability problems. To make a pattern immune |
| 394 | to the 'magic' option being set or not, put "\m" or "\M" at the start of the |
| 395 | pattern. |
| 396 | |
| 397 | ============================================================================== |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 398 | 4. Overview of pattern items *pattern-overview* |
| 399 | |
| 400 | Overview of multi items. */multi* *E61* *E62* |
| 401 | More explanation and examples below, follow the links. *E64* |
| 402 | |
| 403 | multi ~ |
| 404 | 'magic' 'nomagic' matches of the preceding atom ~ |
| 405 | |/star| * \* 0 or more as many as possible |
| 406 | |/\+| \+ \+ 1 or more as many as possible (*) |
| 407 | |/\=| \= \= 0 or 1 as many as possible (*) |
| 408 | |/\?| \? \? 0 or 1 as many as possible (*) |
| 409 | |
| 410 | |/\{| \{n,m} \{n,m} n to m as many as possible (*) |
| 411 | \{n} \{n} n exactly (*) |
| 412 | \{n,} \{n,} at least n as many as possible (*) |
| 413 | \{,m} \{,m} 0 to m as many as possible (*) |
| 414 | \{} \{} 0 or more as many as possible (same as *) (*) |
| 415 | |
| 416 | |/\{-| \{-n,m} \{-n,m} n to m as few as possible (*) |
| 417 | \{-n} \{-n} n exactly (*) |
| 418 | \{-n,} \{-n,} at least n as few as possible (*) |
| 419 | \{-,m} \{-,m} 0 to m as few as possible (*) |
| 420 | \{-} \{-} 0 or more as few as possible (*) |
| 421 | |
| 422 | *E59* |
| 423 | |/\@>| \@> \@> 1, like matching a whole pattern (*) |
| 424 | |/\@=| \@= \@= nothing, requires a match |/zero-width| (*) |
| 425 | |/\@!| \@! \@! nothing, requires NO match |/zero-width| (*) |
| 426 | |/\@<=| \@<= \@<= nothing, requires a match behind |/zero-width| (*) |
| 427 | |/\@<!| \@<! \@<! nothing, requires NO match behind |/zero-width| (*) |
| 428 | |
| 429 | (*) {not in Vi} |
| 430 | |
| 431 | |
| 432 | Overview of ordinary atoms. */ordinary-atom* |
| 433 | More explanation and examples below, follow the links. |
| 434 | |
| 435 | ordinary atom ~ |
| 436 | magic nomagic matches ~ |
| 437 | |/^| ^ ^ start-of-line (at start of pattern) |/zero-width| |
| 438 | |/\^| \^ \^ literal '^' |
| 439 | |/\_^| \_^ \_^ start-of-line (used anywhere) |/zero-width| |
| 440 | |/$| $ $ end-of-line (at end of pattern) |/zero-width| |
| 441 | |/\$| \$ \$ literal '$' |
| 442 | |/\_$| \_$ \_$ end-of-line (used anywhere) |/zero-width| |
| 443 | |/.| . \. any single character (not an end-of-line) |
| 444 | |/\_.| \_. \_. any single character or end-of-line |
| 445 | |/\<| \< \< beginning of a word |/zero-width| |
| 446 | |/\>| \> \> end of a word |/zero-width| |
| 447 | |/\zs| \zs \zs anything, sets start of match |
| 448 | |/\ze| \ze \ze anything, sets end of match |
| 449 | |/\%^| \%^ \%^ beginning of file |/zero-width| *E71* |
| 450 | |/\%$| \%$ \%$ end of file |/zero-width| |
Bram Moolenaar | 33aec76 | 2006-01-22 23:30:12 +0000 | [diff] [blame] | 451 | |/\%V| \%V \%V inside Visual area |/zero-width| |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 452 | |/\%#| \%# \%# cursor position |/zero-width| |
Bram Moolenaar | 33aec76 | 2006-01-22 23:30:12 +0000 | [diff] [blame] | 453 | |/\%'m| \%'m \%'m mark m position |/zero-width| |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 454 | |/\%l| \%23l \%23l in line 23 |/zero-width| |
| 455 | |/\%c| \%23c \%23c in column 23 |/zero-width| |
| 456 | |/\%v| \%23v \%23v in virtual column 23 |/zero-width| |
| 457 | |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 458 | Character classes {not in Vi}: */character-classes* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 459 | |/\i| \i \i identifier character (see 'isident' option) |
| 460 | |/\I| \I \I like "\i", but excluding digits |
| 461 | |/\k| \k \k keyword character (see 'iskeyword' option) |
| 462 | |/\K| \K \K like "\k", but excluding digits |
| 463 | |/\f| \f \f file name character (see 'isfname' option) |
| 464 | |/\F| \F \F like "\f", but excluding digits |
| 465 | |/\p| \p \p printable character (see 'isprint' option) |
| 466 | |/\P| \P \P like "\p", but excluding digits |
| 467 | |/\s| \s \s whitespace character: <Space> and <Tab> |
| 468 | |/\S| \S \S non-whitespace character; opposite of \s |
| 469 | |/\d| \d \d digit: [0-9] |
| 470 | |/\D| \D \D non-digit: [^0-9] |
| 471 | |/\x| \x \x hex digit: [0-9A-Fa-f] |
| 472 | |/\X| \X \X non-hex digit: [^0-9A-Fa-f] |
| 473 | |/\o| \o \o octal digit: [0-7] |
| 474 | |/\O| \O \O non-octal digit: [^0-7] |
| 475 | |/\w| \w \w word character: [0-9A-Za-z_] |
| 476 | |/\W| \W \W non-word character: [^0-9A-Za-z_] |
| 477 | |/\h| \h \h head of word character: [A-Za-z_] |
| 478 | |/\H| \H \H non-head of word character: [^A-Za-z_] |
| 479 | |/\a| \a \a alphabetic character: [A-Za-z] |
| 480 | |/\A| \A \A non-alphabetic character: [^A-Za-z] |
| 481 | |/\l| \l \l lowercase character: [a-z] |
| 482 | |/\L| \L \L non-lowercase character: [^a-z] |
| 483 | |/\u| \u \u uppercase character: [A-Z] |
| 484 | |/\U| \U \U non-uppercase character [^A-Z] |
| 485 | |/\_| \_x \_x where x is any of the characters above: character |
| 486 | class with end-of-line included |
| 487 | (end of character classes) |
| 488 | |
| 489 | |/\e| \e \e <Esc> |
| 490 | |/\t| \t \t <Tab> |
| 491 | |/\r| \r \r <CR> |
| 492 | |/\b| \b \b <BS> |
| 493 | |/\n| \n \n end-of-line |
| 494 | |/~| ~ \~ last given substitute string |
| 495 | |/\1| \1 \1 same string as matched by first \(\) {not in Vi} |
| 496 | |/\2| \2 \2 Like "\1", but uses second \(\) |
| 497 | ... |
| 498 | |/\9| \9 \9 Like "\1", but uses ninth \(\) |
| 499 | *E68* |
| 500 | |/\z1| \z1 \z1 only for syntax highlighting, see |:syn-ext-match| |
| 501 | ... |
| 502 | |/\z1| \z9 \z9 only for syntax highlighting, see |:syn-ext-match| |
| 503 | |
| 504 | x x a character with no special meaning matches itself |
| 505 | |
| 506 | |/[]| [] \[] any character specified inside the [] |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 507 | |/\%[]| \%[] \%[] a sequence of optionally matched atoms |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 508 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 509 | |/\c| \c \c ignore case, do not use the 'ignorecase' option |
| 510 | |/\C| \C \C match case, do not use the 'ignorecase' option |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 511 | |/\m| \m \m 'magic' on for the following chars in the pattern |
| 512 | |/\M| \M \M 'magic' off for the following chars in the pattern |
| 513 | |/\v| \v \v the following chars in the pattern are "very magic" |
| 514 | |/\V| \V \V the following chars in the pattern are "very nomagic" |
| 515 | |/\Z| \Z \Z ignore differences in Unicode "combining characters". |
| 516 | Useful when searching voweled Hebrew or Arabic text. |
| 517 | |
Bram Moolenaar | 8f3f58f | 2010-01-06 20:52:26 +0100 | [diff] [blame] | 518 | |/\%d| \%d \%d match specified decimal character (eg \%d123) |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 519 | |/\%x| \%x \%x match specified hex character (eg \%x2a) |
| 520 | |/\%o| \%o \%o match specified octal character (eg \%o040) |
| 521 | |/\%u| \%u \%u match specified multibyte character (eg \%u20ac) |
| 522 | |/\%U| \%U \%U match specified large multibyte character (eg |
| 523 | \%U12345678) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 524 | |
| 525 | Example matches ~ |
| 526 | \<\I\i* or |
| 527 | \<\h\w* |
| 528 | \<[a-zA-Z_][a-zA-Z0-9_]* |
| 529 | An identifier (e.g., in a C program). |
| 530 | |
| 531 | \(\.$\|\. \) A period followed by <EOL> or a space. |
| 532 | |
| 533 | [.!?][])"']*\($\|[ ]\) A search pattern that finds the end of a sentence, |
| 534 | with almost the same definition as the ")" command. |
| 535 | |
| 536 | cat\Z Both "cat" and "càt" ("a" followed by 0x0300) |
| 537 | Does not match "cà t" (character 0x00e0), even |
| 538 | though it may look the same. |
| 539 | |
| 540 | |
| 541 | ============================================================================== |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 542 | 5. Multi items *pattern-multi-items* |
| 543 | |
| 544 | An atom can be followed by an indication of how many times the atom can be |
| 545 | matched and in what way. This is called a multi. See |/multi| for an |
| 546 | overview. |
| 547 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 548 | */star* */\star* *E56* |
| 549 | * (use \* when 'magic' is not set) |
| 550 | Matches 0 or more of the preceding atom, as many as possible. |
| 551 | Example 'nomagic' matches ~ |
| 552 | a* a\* "", "a", "aa", "aaa", etc. |
| 553 | .* \.\* anything, also an empty string, no end-of-line |
| 554 | \_.* \_.\* everything up to the end of the buffer |
| 555 | \_.*END \_.\*END everything up to and including the last "END" |
| 556 | in the buffer |
| 557 | |
| 558 | Exception: When "*" is used at the start of the pattern or just after |
| 559 | "^" it matches the star character. |
| 560 | |
| 561 | Be aware that repeating "\_." can match a lot of text and take a long |
| 562 | time. For example, "\_.*END" matches all text from the current |
| 563 | position to the last occurrence of "END" in the file. Since the "*" |
| 564 | will match as many as possible, this first skips over all lines until |
| 565 | the end of the file and then tries matching "END", backing up one |
| 566 | character at a time. |
| 567 | |
| 568 | */\+* *E57* |
| 569 | \+ Matches 1 or more of the preceding atom, as many as possible. {not in |
| 570 | Vi} |
| 571 | Example matches ~ |
| 572 | ^.\+$ any non-empty line |
| 573 | \s\+ white space of at least one character |
| 574 | |
| 575 | */\=* |
| 576 | \= Matches 0 or 1 of the preceding atom, as many as possible. {not in Vi} |
| 577 | Example matches ~ |
| 578 | foo\= "fo" and "foo" |
| 579 | |
| 580 | */\?* |
| 581 | \? Just like \=. Cannot be used when searching backwards with the "?" |
| 582 | command. {not in Vi} |
| 583 | |
| 584 | */\{* *E58* *E60* *E554* |
| 585 | \{n,m} Matches n to m of the preceding atom, as many as possible |
| 586 | \{n} Matches n of the preceding atom |
| 587 | \{n,} Matches at least n of the preceding atom, as many as possible |
| 588 | \{,m} Matches 0 to m of the preceding atom, as many as possible |
| 589 | \{} Matches 0 or more of the preceding atom, as many as possible (like *) |
| 590 | */\{-* |
| 591 | \{-n,m} matches n to m of the preceding atom, as few as possible |
| 592 | \{-n} matches n of the preceding atom |
| 593 | \{-n,} matches at least n of the preceding atom, as few as possible |
| 594 | \{-,m} matches 0 to m of the preceding atom, as few as possible |
| 595 | \{-} matches 0 or more of the preceding atom, as few as possible |
| 596 | {Vi does not have any of these} |
| 597 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 598 | n and m are positive decimal numbers or zero |
Bram Moolenaar | c81e5e7 | 2007-05-05 18:24:42 +0000 | [diff] [blame] | 599 | *non-greedy* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 600 | If a "-" appears immediately after the "{", then a shortest match |
| 601 | first algorithm is used (see example below). In particular, "\{-}" is |
| 602 | the same as "*" but uses the shortest match first algorithm. BUT: A |
| 603 | match that starts earlier is preferred over a shorter match: "a\{-}b" |
| 604 | matches "aaab" in "xaaab". |
| 605 | |
| 606 | Example matches ~ |
| 607 | ab\{2,3}c "abbc" or "abbbc" |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 608 | a\{5} "aaaaa" |
| 609 | ab\{2,}c "abbc", "abbbc", "abbbbc", etc. |
| 610 | ab\{,3}c "ac", "abc", "abbc" or "abbbc" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 611 | a[bc]\{3}d "abbbd", "abbcd", "acbcd", "acccd", etc. |
| 612 | a\(bc\)\{1,2}d "abcd" or "abcbcd" |
| 613 | a[bc]\{-}[cd] "abc" in "abcd" |
| 614 | a[bc]*[cd] "abcd" in "abcd" |
| 615 | |
| 616 | The } may optionally be preceded with a backslash: \{n,m\}. |
| 617 | |
| 618 | */\@=* |
| 619 | \@= Matches the preceding atom with zero width. {not in Vi} |
| 620 | Like "(?=pattern)" in Perl. |
| 621 | Example matches ~ |
| 622 | foo\(bar\)\@= "foo" in "foobar" |
| 623 | foo\(bar\)\@=foo nothing |
| 624 | */zero-width* |
| 625 | When using "\@=" (or "^", "$", "\<", "\>") no characters are included |
| 626 | in the match. These items are only used to check if a match can be |
| 627 | made. This can be tricky, because a match with following items will |
| 628 | be done in the same position. The last example above will not match |
| 629 | "foobarfoo", because it tries match "foo" in the same position where |
| 630 | "bar" matched. |
| 631 | |
| 632 | Note that using "\&" works the same as using "\@=": "foo\&.." is the |
| 633 | same as "\(foo\)\@=..". But using "\&" is easier, you don't need the |
| 634 | braces. |
| 635 | |
| 636 | |
| 637 | */\@!* |
| 638 | \@! Matches with zero width if the preceding atom does NOT match at the |
| 639 | current position. |/zero-width| {not in Vi} |
Bram Moolenaar | 1aeaf8c | 2012-05-18 13:46:39 +0200 | [diff] [blame] | 640 | Like "(?!pattern)" in Perl. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 641 | Example matches ~ |
| 642 | foo\(bar\)\@! any "foo" not followed by "bar" |
Bram Moolenaar | 1aeaf8c | 2012-05-18 13:46:39 +0200 | [diff] [blame] | 643 | a.\{-}p\@! "a", "ap", "app", "appp", etc. not immediately |
Bram Moolenaar | 251e191 | 2011-06-19 05:09:16 +0200 | [diff] [blame] | 644 | followed by a "p" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 645 | if \(\(then\)\@!.\)*$ "if " not followed by "then" |
| 646 | |
| 647 | Using "\@!" is tricky, because there are many places where a pattern |
| 648 | does not match. "a.*p\@!" will match from an "a" to the end of the |
| 649 | line, because ".*" can match all characters in the line and the "p" |
| 650 | doesn't match at the end of the line. "a.\{-}p\@!" will match any |
Bram Moolenaar | 1aeaf8c | 2012-05-18 13:46:39 +0200 | [diff] [blame] | 651 | "a", "ap", "app", etc. that isn't followed by a "p", because the "." |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 652 | can match a "p" and "p\@!" doesn't match after that. |
| 653 | |
| 654 | You can't use "\@!" to look for a non-match before the matching |
| 655 | position: "\(foo\)\@!bar" will match "bar" in "foobar", because at the |
| 656 | position where "bar" matches, "foo" does not match. To avoid matching |
| 657 | "foobar" you could use "\(foo\)\@!...bar", but that doesn't match a |
Bram Moolenaar | 13fcaaf | 2005-04-15 21:13:42 +0000 | [diff] [blame] | 658 | bar at the start of a line. Use "\(foo\)\@<!bar". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 659 | |
Bram Moolenaar | 8e5af3e | 2011-04-28 19:02:44 +0200 | [diff] [blame] | 660 | Useful example: to find "foo" in a line that does not contain "bar": > |
| 661 | /^\%(.*bar\)\@!.*\zsfoo |
| 662 | < This pattern first checks that there is not a single position in the |
| 663 | line where "bar" matches. If ".*bar" matches somewhere the \@! will |
| 664 | reject the pattern. When there is no match any "foo" will be found. |
| 665 | The "\zs" is to have the match start just before "foo". |
| 666 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 667 | */\@<=* |
| 668 | \@<= Matches with zero width if the preceding atom matches just before what |
| 669 | follows. |/zero-width| {not in Vi} |
Bram Moolenaar | 1aeaf8c | 2012-05-18 13:46:39 +0200 | [diff] [blame] | 670 | Like "(?<=pattern)" in Perl, but Vim allows non-fixed-width patterns. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 671 | Example matches ~ |
| 672 | \(an\_s\+\)\@<=file "file" after "an" and white space or an |
| 673 | end-of-line |
| 674 | For speed it's often much better to avoid this multi. Try using "\zs" |
| 675 | instead |/\zs|. To match the same as the above example: |
| 676 | an\_s\+\zsfile |
| 677 | |
| 678 | "\@<=" and "\@<!" check for matches just before what follows. |
| 679 | Theoretically these matches could start anywhere before this position. |
| 680 | But to limit the time needed, only the line where what follows matches |
| 681 | is searched, and one line before that (if there is one). This should |
| 682 | be sufficient to match most things and not be too slow. |
| 683 | The part of the pattern after "\@<=" and "\@<!" are checked for a |
| 684 | match first, thus things like "\1" don't work to reference \(\) inside |
| 685 | the preceding atom. It does work the other way around: |
| 686 | Example matches ~ |
| 687 | \1\@<=,\([a-z]\+\) ",abc" in "abc,abc" |
| 688 | |
| 689 | */\@<!* |
| 690 | \@<! Matches with zero width if the preceding atom does NOT match just |
| 691 | before what follows. Thus this matches if there is no position in the |
| 692 | current or previous line where the atom matches such that it ends just |
| 693 | before what follows. |/zero-width| {not in Vi} |
Bram Moolenaar | 1aeaf8c | 2012-05-18 13:46:39 +0200 | [diff] [blame] | 694 | Like "(?<!pattern)" in Perl, but Vim allows non-fixed-width patterns. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 695 | The match with the preceding atom is made to end just before the match |
| 696 | with what follows, thus an atom that ends in ".*" will work. |
| 697 | Warning: This can be slow (because many positions need to be checked |
| 698 | for a match). |
| 699 | Example matches ~ |
| 700 | \(foo\)\@<!bar any "bar" that's not in "foobar" |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 701 | \(\/\/.*\)\@<!in "in" which is not after "//" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 702 | |
| 703 | */\@>* |
| 704 | \@> Matches the preceding atom like matching a whole pattern. {not in Vi} |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 705 | Like "(?>pattern)" in Perl. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 706 | Example matches ~ |
| 707 | \(a*\)\@>a nothing (the "a*" takes all the "a"'s, there can't be |
| 708 | another one following) |
| 709 | |
| 710 | This matches the preceding atom as if it was a pattern by itself. If |
| 711 | it doesn't match, there is no retry with shorter sub-matches or |
| 712 | anything. Observe this difference: "a*b" and "a*ab" both match |
| 713 | "aaab", but in the second case the "a*" matches only the first two |
| 714 | "a"s. "\(a*\)\@>ab" will not match "aaab", because the "a*" matches |
| 715 | the "aaa" (as many "a"s as possible), thus the "ab" can't match. |
| 716 | |
| 717 | |
| 718 | ============================================================================== |
| 719 | 6. Ordinary atoms *pattern-atoms* |
| 720 | |
| 721 | An ordinary atom can be: |
| 722 | |
| 723 | */^* |
| 724 | ^ At beginning of pattern or after "\|", "\(", "\%(" or "\n": matches |
| 725 | start-of-line; at other positions, matches literal '^'. |/zero-width| |
| 726 | Example matches ~ |
| 727 | ^beep( the start of the C function "beep" (probably). |
| 728 | |
| 729 | */\^* |
| 730 | \^ Matches literal '^'. Can be used at any position in the pattern. |
| 731 | |
| 732 | */\_^* |
| 733 | \_^ Matches start-of-line. |/zero-width| Can be used at any position in |
| 734 | the pattern. |
| 735 | Example matches ~ |
| 736 | \_s*\_^foo white space and blank lines and then "foo" at |
| 737 | start-of-line |
| 738 | |
| 739 | */$* |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 740 | $ At end of pattern or in front of "\|", "\)" or "\n" ('magic' on): |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 741 | matches end-of-line <EOL>; at other positions, matches literal '$'. |
| 742 | |/zero-width| |
| 743 | |
| 744 | */\$* |
| 745 | \$ Matches literal '$'. Can be used at any position in the pattern. |
| 746 | |
| 747 | */\_$* |
| 748 | \_$ Matches end-of-line. |/zero-width| Can be used at any position in the |
| 749 | pattern. Note that "a\_$b" never matches, since "b" cannot match an |
| 750 | end-of-line. Use "a\nb" instead |/\n|. |
| 751 | Example matches ~ |
| 752 | foo\_$\_s* "foo" at end-of-line and following white space and |
| 753 | blank lines |
| 754 | |
| 755 | . (with 'nomagic': \.) */.* */\.* |
| 756 | Matches any single character, but not an end-of-line. |
| 757 | |
| 758 | */\_.* |
| 759 | \_. Matches any single character or end-of-line. |
| 760 | Careful: "\_.*" matches all text to the end of the buffer! |
| 761 | |
| 762 | */\<* |
| 763 | \< Matches the beginning of a word: The next char is the first char of a |
| 764 | word. The 'iskeyword' option specifies what is a word character. |
| 765 | |/zero-width| |
| 766 | |
| 767 | */\>* |
| 768 | \> Matches the end of a word: The previous char is the last char of a |
Bram Moolenaar | 13fcaaf | 2005-04-15 21:13:42 +0000 | [diff] [blame] | 769 | word. The 'iskeyword' option specifies what is a word character. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 770 | |/zero-width| |
| 771 | |
| 772 | */\zs* |
| 773 | \zs Matches at any position, and sets the start of the match there: The |
| 774 | next char is the first char of the whole match. |/zero-width| |
| 775 | Example: > |
| 776 | /^\s*\zsif |
| 777 | < matches an "if" at the start of a line, ignoring white space. |
| 778 | Can be used multiple times, the last one encountered in a matching |
Bram Moolenaar | 13fcaaf | 2005-04-15 21:13:42 +0000 | [diff] [blame] | 779 | branch is used. Example: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 780 | /\(.\{-}\zsFab\)\{3} |
| 781 | < Finds the third occurrence of "Fab". |
Bram Moolenaar | db84e45 | 2010-08-15 13:50:43 +0200 | [diff] [blame] | 782 | {not in Vi} {not available when compiled without the |+syntax| feature} |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 783 | */\ze* |
| 784 | \ze Matches at any position, and sets the end of the match there: The |
| 785 | previous char is the last char of the whole match. |/zero-width| |
| 786 | Can be used multiple times, the last one encountered in a matching |
| 787 | branch is used. |
| 788 | Example: "end\ze\(if\|for\)" matches the "end" in "endif" and |
| 789 | "endfor". |
Bram Moolenaar | db84e45 | 2010-08-15 13:50:43 +0200 | [diff] [blame] | 790 | {not in Vi} {not available when compiled without the |+syntax| feature} |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 791 | |
| 792 | */\%^* *start-of-file* |
| 793 | \%^ Matches start of the file. When matching with a string, matches the |
| 794 | start of the string. {not in Vi} |
| 795 | For example, to find the first "VIM" in a file: > |
| 796 | /\%^\_.\{-}\zsVIM |
| 797 | < |
| 798 | */\%$* *end-of-file* |
| 799 | \%$ Matches end of the file. When matching with a string, matches the |
| 800 | end of the string. {not in Vi} |
| 801 | Note that this does NOT find the last "VIM" in a file: > |
| 802 | /VIM\_.\{-}\%$ |
| 803 | < It will find the next VIM, because the part after it will always |
| 804 | match. This one will find the last "VIM" in the file: > |
| 805 | /VIM\ze\(\(VIM\)\@!\_.\)*\%$ |
| 806 | < This uses |/\@!| to ascertain that "VIM" does NOT match in any |
| 807 | position after the first "VIM". |
| 808 | Searching from the end of the file backwards is easier! |
| 809 | |
Bram Moolenaar | 33aec76 | 2006-01-22 23:30:12 +0000 | [diff] [blame] | 810 | */\%V* |
| 811 | \%V Match inside the Visual area. When Visual mode has already been |
| 812 | stopped match in the area that |gv| would reselect. |
Bram Moolenaar | 8f3f58f | 2010-01-06 20:52:26 +0100 | [diff] [blame] | 813 | This is a |/zero-width| match. To make sure the whole pattern is |
| 814 | inside the Visual area put it at the start and end of the pattern, |
| 815 | e.g.: > |
| 816 | /\%Vfoo.*bar\%V |
| 817 | < Only works for the current buffer. |
Bram Moolenaar | 33aec76 | 2006-01-22 23:30:12 +0000 | [diff] [blame] | 818 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 819 | */\%#* *cursor-position* |
| 820 | \%# Matches with the cursor position. Only works when matching in a |
| 821 | buffer displayed in a window. {not in Vi} |
| 822 | WARNING: When the cursor is moved after the pattern was used, the |
| 823 | result becomes invalid. Vim doesn't automatically update the matches. |
| 824 | This is especially relevant for syntax highlighting and 'hlsearch'. |
| 825 | In other words: When the cursor moves the display isn't updated for |
| 826 | this change. An update is done for lines which are changed (the whole |
| 827 | line is updated) or when using the |CTRL-L| command (the whole screen |
| 828 | is updated). Example, to highlight the word under the cursor: > |
| 829 | /\k*\%#\k* |
| 830 | < When 'hlsearch' is set and you move the cursor around and make changes |
| 831 | this will clearly show when the match is updated or not. |
| 832 | |
Bram Moolenaar | 33aec76 | 2006-01-22 23:30:12 +0000 | [diff] [blame] | 833 | */\%'m* */\%<'m* */\%>'m* |
| 834 | \%'m Matches with the position of mark m. |
| 835 | \%<'m Matches before the position of mark m. |
| 836 | \%>'m Matches after the position of mark m. |
| 837 | Example, to highlight the text from mark 's to 'e: > |
| 838 | /.\%>'s.*\%<'e.. |
| 839 | < Note that two dots are required to include mark 'e in the match. That |
| 840 | is because "\%<'e" matches at the character before the 'e mark, and |
| 841 | since it's a |/zero-width| match it doesn't include that character. |
| 842 | {not in Vi} |
| 843 | WARNING: When the mark is moved after the pattern was used, the result |
| 844 | becomes invalid. Vim doesn't automatically update the matches. |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 845 | Similar to moving the cursor for "\%#" |/\%#|. |
Bram Moolenaar | 33aec76 | 2006-01-22 23:30:12 +0000 | [diff] [blame] | 846 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 847 | */\%l* */\%>l* */\%<l* |
| 848 | \%23l Matches in a specific line. |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 849 | \%<23l Matches above a specific line (lower line number). |
| 850 | \%>23l Matches below a specific line (higher line number). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 851 | These three can be used to match specific lines in a buffer. The "23" |
| 852 | can be any line number. The first line is 1. {not in Vi} |
| 853 | WARNING: When inserting or deleting lines Vim does not automatically |
| 854 | update the matches. This means Syntax highlighting quickly becomes |
| 855 | wrong. |
| 856 | Example, to highlight the line where the cursor currently is: > |
| 857 | :exe '/\%' . line(".") . 'l.*' |
| 858 | < When 'hlsearch' is set and you move the cursor around and make changes |
| 859 | this will clearly show when the match is updated or not. |
| 860 | |
| 861 | */\%c* */\%>c* */\%<c* |
| 862 | \%23c Matches in a specific column. |
| 863 | \%<23c Matches before a specific column. |
| 864 | \%>23c Matches after a specific column. |
| 865 | These three can be used to match specific columns in a buffer or |
| 866 | string. The "23" can be any column number. The first column is 1. |
| 867 | Actually, the column is the byte number (thus it's not exactly right |
| 868 | for multi-byte characters). {not in Vi} |
| 869 | WARNING: When inserting or deleting text Vim does not automatically |
| 870 | update the matches. This means Syntax highlighting quickly becomes |
| 871 | wrong. |
| 872 | Example, to highlight the column where the cursor currently is: > |
| 873 | :exe '/\%' . col(".") . 'c' |
| 874 | < When 'hlsearch' is set and you move the cursor around and make changes |
| 875 | this will clearly show when the match is updated or not. |
| 876 | Example for matching a single byte in column 44: > |
| 877 | /\%>43c.\%<46c |
| 878 | < Note that "\%<46c" matches in column 45 when the "." matches a byte in |
| 879 | column 44. |
| 880 | */\%v* */\%>v* */\%<v* |
| 881 | \%23v Matches in a specific virtual column. |
| 882 | \%<23v Matches before a specific virtual column. |
| 883 | \%>23v Matches after a specific virtual column. |
| 884 | These three can be used to match specific virtual columns in a buffer |
| 885 | or string. When not matching with a buffer in a window, the option |
| 886 | values of the current window are used (e.g., 'tabstop'). |
| 887 | The "23" can be any column number. The first column is 1. |
| 888 | Note that some virtual column positions will never match, because they |
Bram Moolenaar | 69c2f17 | 2007-05-12 14:57:31 +0000 | [diff] [blame] | 889 | are halfway through a tab or other character that occupies more than |
| 890 | one screen character. {not in Vi} |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 891 | WARNING: When inserting or deleting text Vim does not automatically |
Bram Moolenaar | de934d7 | 2005-05-22 22:09:40 +0000 | [diff] [blame] | 892 | update highlighted matches. This means Syntax highlighting quickly |
| 893 | becomes wrong. |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 894 | Example, to highlight all the characters after virtual column 72: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 895 | /\%>72v.* |
| 896 | < When 'hlsearch' is set and you move the cursor around and make changes |
| 897 | this will clearly show when the match is updated or not. |
| 898 | To match the text up to column 17: > |
| 899 | /.*\%17v |
Bram Moolenaar | 8f3f58f | 2010-01-06 20:52:26 +0100 | [diff] [blame] | 900 | < Column 17 is included, because that's where the "\%17v" matches, |
| 901 | even though this is a |/zero-width| match. Adding a dot to match the |
| 902 | next character has the same result: > |
| 903 | /.*\%17v. |
| 904 | < This command does the same thing, but also matches when there is no |
| 905 | character in column 17: > |
| 906 | /.*\%<18v. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 907 | < |
| 908 | |
| 909 | Character classes: {not in Vi} |
| 910 | \i identifier character (see 'isident' option) */\i* |
| 911 | \I like "\i", but excluding digits */\I* |
| 912 | \k keyword character (see 'iskeyword' option) */\k* |
| 913 | \K like "\k", but excluding digits */\K* |
| 914 | \f file name character (see 'isfname' option) */\f* |
| 915 | \F like "\f", but excluding digits */\F* |
| 916 | \p printable character (see 'isprint' option) */\p* |
| 917 | \P like "\p", but excluding digits */\P* |
| 918 | |
| 919 | NOTE: the above also work for multi-byte characters. The ones below only |
| 920 | match ASCII characters, as indicated by the range. |
| 921 | |
| 922 | *whitespace* *white-space* |
| 923 | \s whitespace character: <Space> and <Tab> */\s* |
| 924 | \S non-whitespace character; opposite of \s */\S* |
| 925 | \d digit: [0-9] */\d* |
| 926 | \D non-digit: [^0-9] */\D* |
| 927 | \x hex digit: [0-9A-Fa-f] */\x* |
| 928 | \X non-hex digit: [^0-9A-Fa-f] */\X* |
| 929 | \o octal digit: [0-7] */\o* |
| 930 | \O non-octal digit: [^0-7] */\O* |
| 931 | \w word character: [0-9A-Za-z_] */\w* |
| 932 | \W non-word character: [^0-9A-Za-z_] */\W* |
| 933 | \h head of word character: [A-Za-z_] */\h* |
| 934 | \H non-head of word character: [^A-Za-z_] */\H* |
| 935 | \a alphabetic character: [A-Za-z] */\a* |
| 936 | \A non-alphabetic character: [^A-Za-z] */\A* |
| 937 | \l lowercase character: [a-z] */\l* |
| 938 | \L non-lowercase character: [^a-z] */\L* |
| 939 | \u uppercase character: [A-Z] */\u* |
Bram Moolenaar | f1568ec | 2011-12-14 21:17:39 +0100 | [diff] [blame] | 940 | \U non-uppercase character: [^A-Z] */\U* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 941 | |
| 942 | NOTE: Using the atom is faster than the [] form. |
| 943 | |
| 944 | NOTE: 'ignorecase', "\c" and "\C" are not used by character classes. |
| 945 | |
| 946 | */\_* *E63* */\_i* */\_I* */\_k* */\_K* */\_f* */\_F* |
| 947 | */\_p* */\_P* */\_s* */\_S* */\_d* */\_D* */\_x* */\_X* |
| 948 | */\_o* */\_O* */\_w* */\_W* */\_h* */\_H* */\_a* */\_A* |
| 949 | */\_l* */\_L* */\_u* */\_U* |
| 950 | \_x Where "x" is any of the characters above: The character class with |
| 951 | end-of-line added |
| 952 | (end of character classes) |
| 953 | |
| 954 | \e matches <Esc> */\e* |
| 955 | \t matches <Tab> */\t* |
| 956 | \r matches <CR> */\r* |
| 957 | \b matches <BS> */\b* |
| 958 | \n matches an end-of-line */\n* |
| 959 | When matching in a string instead of buffer text a literal newline |
| 960 | character is matched. |
| 961 | |
| 962 | ~ matches the last given substitute string */~* */\~* |
| 963 | |
| 964 | \(\) A pattern enclosed by escaped parentheses. */\(* */\(\)* */\)* |
| 965 | E.g., "\(^a\)" matches 'a' at the start of a line. *E51* *E54* *E55* |
| 966 | |
| 967 | \1 Matches the same string that was matched by */\1* *E65* |
| 968 | the first sub-expression in \( and \). {not in Vi} |
| 969 | Example: "\([a-z]\).\1" matches "ata", "ehe", "tot", etc. |
| 970 | \2 Like "\1", but uses second sub-expression, */\2* |
| 971 | ... */\3* |
| 972 | \9 Like "\1", but uses ninth sub-expression. */\9* |
| 973 | Note: The numbering of groups is done based on which "\(" comes first |
| 974 | in the pattern (going left to right), NOT based on what is matched |
| 975 | first. |
| 976 | |
| 977 | \%(\) A pattern enclosed by escaped parentheses. */\%(\)* */\%(* *E53* |
| 978 | Just like \(\), but without counting it as a sub-expression. This |
| 979 | allows using more groups and it's a little bit faster. |
| 980 | {not in Vi} |
| 981 | |
| 982 | x A single character, with no special meaning, matches itself |
| 983 | |
| 984 | */\* */\\* |
| 985 | \x A backslash followed by a single character, with no special meaning, |
| 986 | is reserved for future expansions |
| 987 | |
| 988 | [] (with 'nomagic': \[]) */[]* */\[]* */\_[]* */collection* |
| 989 | \_[] |
Bram Moolenaar | 13fcaaf | 2005-04-15 21:13:42 +0000 | [diff] [blame] | 990 | A collection. This is a sequence of characters enclosed in brackets. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 991 | It matches any single character in the collection. |
| 992 | Example matches ~ |
| 993 | [xyz] any 'x', 'y' or 'z' |
| 994 | [a-zA-Z]$ any alphabetic character at the end of a line |
| 995 | \c[a-z]$ same |
Bram Moolenaar | c81e5e7 | 2007-05-05 18:24:42 +0000 | [diff] [blame] | 996 | */[\n]* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 997 | With "\_" prepended the collection also includes the end-of-line. |
| 998 | The same can be done by including "\n" in the collection. The |
| 999 | end-of-line is also matched when the collection starts with "^"! Thus |
| 1000 | "\_[^ab]" matches the end-of-line and any character but "a" and "b". |
| 1001 | This makes it Vi compatible: Without the "\_" or "\n" the collection |
| 1002 | does not match an end-of-line. |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 1003 | *E769* |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1004 | When the ']' is not there Vim will not give an error message but |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 1005 | assume no collection is used. Useful to search for '['. However, you |
| 1006 | do get E769 for internal searching. |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1007 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1008 | If the sequence begins with "^", it matches any single character NOT |
| 1009 | in the collection: "[^xyz]" matches anything but 'x', 'y' and 'z'. |
| 1010 | - If two characters in the sequence are separated by '-', this is |
| 1011 | shorthand for the full list of ASCII characters between them. E.g., |
Bram Moolenaar | 0bc380a | 2010-07-10 13:52:13 +0200 | [diff] [blame] | 1012 | "[0-9]" matches any decimal digit. Non-ASCII characters can be |
| 1013 | used, but the character values must not be more than 256 apart. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1014 | - A character class expression is evaluated to the set of characters |
| 1015 | belonging to that character class. The following character classes |
| 1016 | are supported: |
| 1017 | Name Contents ~ |
| 1018 | *[:alnum:]* [:alnum:] letters and digits |
| 1019 | *[:alpha:]* [:alpha:] letters |
| 1020 | *[:blank:]* [:blank:] space and tab characters |
| 1021 | *[:cntrl:]* [:cntrl:] control characters |
| 1022 | *[:digit:]* [:digit:] decimal digits |
| 1023 | *[:graph:]* [:graph:] printable characters excluding space |
| 1024 | *[:lower:]* [:lower:] lowercase letters (all letters when |
| 1025 | 'ignorecase' is used) |
| 1026 | *[:print:]* [:print:] printable characters including space |
| 1027 | *[:punct:]* [:punct:] punctuation characters |
| 1028 | *[:space:]* [:space:] whitespace characters |
| 1029 | *[:upper:]* [:upper:] uppercase letters (all letters when |
| 1030 | 'ignorecase' is used) |
| 1031 | *[:xdigit:]* [:xdigit:] hexadecimal digits |
| 1032 | *[:return:]* [:return:] the <CR> character |
| 1033 | *[:tab:]* [:tab:] the <Tab> character |
| 1034 | *[:escape:]* [:escape:] the <Esc> character |
| 1035 | *[:backspace:]* [:backspace:] the <BS> character |
| 1036 | The brackets in character class expressions are additional to the |
| 1037 | brackets delimiting a collection. For example, the following is a |
| 1038 | plausible pattern for a UNIX filename: "[-./[:alnum:]_~]\+" That is, |
| 1039 | a list of at least one character, each of which is either '-', '.', |
| 1040 | '/', alphabetic, numeric, '_' or '~'. |
| 1041 | These items only work for 8-bit characters. |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1042 | */[[=* *[==]* |
| 1043 | - An equivalence class. This means that characters are matched that |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 1044 | have almost the same meaning, e.g., when ignoring accents. This |
| 1045 | only works for Unicode, latin1 and latin9. The form is: |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 1046 | [=a=] |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1047 | */[[.* *[..]* |
| 1048 | - A collation element. This currently simply accepts a single |
| 1049 | character in the form: |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 1050 | [.a.] |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1051 | */\]* |
| 1052 | - To include a literal ']', '^', '-' or '\' in the collection, put a |
| 1053 | backslash before it: "[xyz\]]", "[\^xyz]", "[xy\-z]" and "[xyz\\]". |
| 1054 | (Note: POSIX does not support the use of a backslash this way). For |
| 1055 | ']' you can also make it the first character (following a possible |
| 1056 | "^"): "[]xyz]" or "[^]xyz]" {not in Vi}. |
| 1057 | For '-' you can also make it the first or last character: "[-xyz]", |
| 1058 | "[^-xyz]" or "[xyz-]". For '\' you can also let it be followed by |
Bram Moolenaar | 0bc380a | 2010-07-10 13:52:13 +0200 | [diff] [blame] | 1059 | any character that's not in "^]-\bdertnoUux". "[\xyz]" matches '\', |
| 1060 | 'x', 'y' and 'z'. It's better to use "\\" though, future expansions |
| 1061 | may use other characters after '\'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1062 | - The following translations are accepted when the 'l' flag is not |
| 1063 | included in 'cpoptions' {not in Vi}: |
| 1064 | \e <Esc> |
| 1065 | \t <Tab> |
| 1066 | \r <CR> (NOT end-of-line!) |
| 1067 | \b <BS> |
Bram Moolenaar | c81e5e7 | 2007-05-05 18:24:42 +0000 | [diff] [blame] | 1068 | \n line break, see above |/[\n]| |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 1069 | \d123 decimal number of character |
| 1070 | \o40 octal number of character up to 0377 |
| 1071 | \x20 hexadecimal number of character up to 0xff |
| 1072 | \u20AC hex. number of multibyte character up to 0xffff |
| 1073 | \U1234 hex. number of multibyte character up to 0xffffffff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1074 | NOTE: The other backslash codes mentioned above do not work inside |
| 1075 | []! |
| 1076 | - Matching with a collection can be slow, because each character in |
| 1077 | the text has to be compared with each character in the collection. |
| 1078 | Use one of the other atoms above when possible. Example: "\d" is |
| 1079 | much faster than "[0-9]" and matches the same characters. |
| 1080 | |
| 1081 | */\%[]* *E69* *E70* *E369* |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 1082 | \%[] A sequence of optionally matched atoms. This always matches. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1083 | It matches as much of the list of atoms it contains as possible. Thus |
| 1084 | it stops at the first atom that doesn't match. For example: > |
| 1085 | /r\%[ead] |
| 1086 | < matches "r", "re", "rea" or "read". The longest that matches is used. |
| 1087 | To match the Ex command "function", where "fu" is required and |
| 1088 | "nction" is optional, this would work: > |
| 1089 | /\<fu\%[nction]\> |
| 1090 | < The end-of-word atom "\>" is used to avoid matching "fu" in "full". |
| 1091 | It gets more complicated when the atoms are not ordinary characters. |
| 1092 | You don't often have to use it, but it is possible. Example: > |
| 1093 | /\<r\%[[eo]ad]\> |
| 1094 | < Matches the words "r", "re", "ro", "rea", "roa", "read" and "road". |
Bram Moolenaar | c81e5e7 | 2007-05-05 18:24:42 +0000 | [diff] [blame] | 1095 | There can be no \(\), \%(\) or \z(\) items inside the [] and \%[] does |
| 1096 | not nest. |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 1097 | To include a "[" use "[[]" and for "]" use []]", e.g.,: > |
| 1098 | /index\%[[[]0[]]] |
| 1099 | < matches "index" "index[", "index[0" and "index[0]". |
Bram Moolenaar | db84e45 | 2010-08-15 13:50:43 +0200 | [diff] [blame] | 1100 | {not available when compiled without the |+syntax| feature} |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1101 | |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 1102 | */\%d* */\%x* */\%o* */\%u* */\%U* *E678* |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 1103 | |
| 1104 | \%d123 Matches the character specified with a decimal number. Must be |
| 1105 | followed by a non-digit. |
| 1106 | \%o40 Matches the character specified with an octal number up to 0377. |
| 1107 | Numbers below 040 must be followed by a non-octal digit or a non-digit. |
| 1108 | \%x2a Matches the character specified with up to two hexadecimal characters. |
| 1109 | \%u20AC Matches the character specified with up to four hexadecimal |
| 1110 | characters. |
| 1111 | \%U1234abcd Matches the character specified with up to eight hexadecimal |
| 1112 | characters. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1113 | |
| 1114 | ============================================================================== |
| 1115 | 7. Ignoring case in a pattern */ignorecase* |
| 1116 | |
| 1117 | If the 'ignorecase' option is on, the case of normal letters is ignored. |
| 1118 | 'smartcase' can be set to ignore case when the pattern contains lowercase |
| 1119 | letters only. |
| 1120 | */\c* */\C* |
| 1121 | When "\c" appears anywhere in the pattern, the whole pattern is handled like |
| 1122 | 'ignorecase' is on. The actual value of 'ignorecase' and 'smartcase' is |
| 1123 | ignored. "\C" does the opposite: Force matching case for the whole pattern. |
| 1124 | {only Vim supports \c and \C} |
| 1125 | Note that 'ignorecase', "\c" and "\C" are not used for the character classes. |
| 1126 | |
| 1127 | Examples: |
| 1128 | pattern 'ignorecase' 'smartcase' matches ~ |
| 1129 | foo off - foo |
| 1130 | foo on - foo Foo FOO |
| 1131 | Foo on off foo Foo FOO |
| 1132 | Foo on on Foo |
| 1133 | \cfoo - - foo Foo FOO |
| 1134 | foo\C - - foo |
| 1135 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1136 | Technical detail: *NL-used-for-Nul* |
| 1137 | <Nul> characters in the file are stored as <NL> in memory. In the display |
| 1138 | they are shown as "^@". The translation is done when reading and writing |
| 1139 | files. To match a <Nul> with a search pattern you can just enter CTRL-@ or |
| 1140 | "CTRL-V 000". This is probably just what you expect. Internally the |
| 1141 | character is replaced with a <NL> in the search pattern. What is unusual is |
| 1142 | that typing CTRL-V CTRL-J also inserts a <NL>, thus also searches for a <Nul> |
| 1143 | in the file. {Vi cannot handle <Nul> characters in the file at all} |
| 1144 | |
| 1145 | *CR-used-for-NL* |
| 1146 | When 'fileformat' is "mac", <NL> characters in the file are stored as <CR> |
Bram Moolenaar | e37d50a | 2008-08-06 17:06:04 +0000 | [diff] [blame] | 1147 | characters internally. In the text they are shown as "^J". Otherwise this |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1148 | works similar to the usage of <NL> for a <Nul>. |
| 1149 | |
| 1150 | When working with expression evaluation, a <NL> character in the pattern |
| 1151 | matches a <NL> in the string. The use of "\n" (backslash n) to match a <NL> |
| 1152 | doesn't work there, it only works to match text in the buffer. |
| 1153 | |
| 1154 | *pattern-multi-byte* |
| 1155 | Patterns will also work with multi-byte characters, mostly as you would |
| 1156 | expect. But invalid bytes may cause trouble, a pattern with an invalid byte |
| 1157 | will probably never match. |
| 1158 | |
| 1159 | ============================================================================== |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 1160 | 8. Composing characters *patterns-composing* |
| 1161 | |
| 1162 | */\Z* |
| 1163 | When "\Z" appears anywhere in the pattern, composing characters are ignored. |
| 1164 | Thus only the base characters need to match, the composing characters may be |
| 1165 | different and the number of composing characters may differ. Only relevant |
| 1166 | when 'encoding' is "utf-8". |
| 1167 | |
| 1168 | When a composing character appears at the start of the pattern of after an |
| 1169 | item that doesn't include the composing character, a match is found at any |
| 1170 | character that includes this composing character. |
| 1171 | |
| 1172 | When using a dot and a composing character, this works the same as the |
| 1173 | composing character by itself, except that it doesn't matter what comes before |
| 1174 | this. |
| 1175 | |
| 1176 | The order of composing characters matters, even though changing the order |
Bram Moolenaar | c81e5e7 | 2007-05-05 18:24:42 +0000 | [diff] [blame] | 1177 | doesn't change what a character looks like. This may change in the future. |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 1178 | |
| 1179 | ============================================================================== |
| 1180 | 9. Compare with Perl patterns *perl-patterns* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1181 | |
| 1182 | Vim's regexes are most similar to Perl's, in terms of what you can do. The |
| 1183 | difference between them is mostly just notation; here's a summary of where |
| 1184 | they differ: |
| 1185 | |
| 1186 | Capability in Vimspeak in Perlspeak ~ |
| 1187 | ---------------------------------------------------------------- |
| 1188 | force case insensitivity \c (?i) |
| 1189 | force case sensitivity \C (?-i) |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 1190 | backref-less grouping \%(atom\) (?:atom) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1191 | conservative quantifiers \{-n,m} *?, +?, ??, {}? |
| 1192 | 0-width match atom\@= (?=atom) |
| 1193 | 0-width non-match atom\@! (?!atom) |
| 1194 | 0-width preceding match atom\@<= (?<=atom) |
| 1195 | 0-width preceding non-match atom\@<! (?<!atom) |
| 1196 | match without retry atom\@> (?>atom) |
| 1197 | |
| 1198 | Vim and Perl handle newline characters inside a string a bit differently: |
| 1199 | |
| 1200 | In Perl, ^ and $ only match at the very beginning and end of the text, |
| 1201 | by default, but you can set the 'm' flag, which lets them match at |
| 1202 | embedded newlines as well. You can also set the 's' flag, which causes |
| 1203 | a . to match newlines as well. (Both these flags can be changed inside |
| 1204 | a pattern using the same syntax used for the i flag above, BTW.) |
| 1205 | |
| 1206 | On the other hand, Vim's ^ and $ always match at embedded newlines, and |
| 1207 | you get two separate atoms, \%^ and \%$, which only match at the very |
| 1208 | start and end of the text, respectively. Vim solves the second problem |
| 1209 | by giving you the \_ "modifier": put it in front of a . or a character |
| 1210 | class, and they will match newlines as well. |
| 1211 | |
| 1212 | Finally, these constructs are unique to Perl: |
| 1213 | - execution of arbitrary code in the regex: (?{perl code}) |
| 1214 | - conditional expressions: (?(condition)true-expr|false-expr) |
| 1215 | |
| 1216 | ...and these are unique to Vim: |
| 1217 | - changing the magic-ness of a pattern: \v \V \m \M |
| 1218 | (very useful for avoiding backslashitis) |
| 1219 | - sequence of optionally matching atoms: \%[atoms] |
| 1220 | - \& (which is to \| what "and" is to "or"; it forces several branches |
| 1221 | to match at one spot) |
| 1222 | - matching lines/columns by number: \%5l \%5c \%5v |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 1223 | - setting the start and end of the match: \zs \ze |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1224 | |
| 1225 | ============================================================================== |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 1226 | 10. Highlighting matches *match-highlight* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1227 | |
| 1228 | *:mat* *:match* |
| 1229 | :mat[ch] {group} /{pattern}/ |
| 1230 | Define a pattern to highlight in the current window. It will |
| 1231 | be highlighted with {group}. Example: > |
| 1232 | :highlight MyGroup ctermbg=green guibg=green |
| 1233 | :match MyGroup /TODO/ |
| 1234 | < Instead of // any character can be used to mark the start and |
| 1235 | end of the {pattern}. Watch out for using special characters, |
| 1236 | such as '"' and '|'. |
Bram Moolenaar | fd2ac76 | 2006-03-01 22:09:21 +0000 | [diff] [blame] | 1237 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1238 | {group} must exist at the moment this command is executed. |
Bram Moolenaar | fd2ac76 | 2006-03-01 22:09:21 +0000 | [diff] [blame] | 1239 | |
| 1240 | The {group} highlighting still applies when a character is |
Bram Moolenaar | 6ee1016 | 2007-07-26 20:58:42 +0000 | [diff] [blame] | 1241 | to be highlighted for 'hlsearch', as the highlighting for |
| 1242 | matches is given higher priority than that of 'hlsearch'. |
| 1243 | Syntax highlighting (see 'syntax') is also overruled by |
| 1244 | matches. |
Bram Moolenaar | fd2ac76 | 2006-03-01 22:09:21 +0000 | [diff] [blame] | 1245 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1246 | Note that highlighting the last used search pattern with |
| 1247 | 'hlsearch' is used in all windows, while the pattern defined |
| 1248 | with ":match" only exists in the current window. It is kept |
| 1249 | when switching to another buffer. |
Bram Moolenaar | fd2ac76 | 2006-03-01 22:09:21 +0000 | [diff] [blame] | 1250 | |
Bram Moolenaar | fd2ac76 | 2006-03-01 22:09:21 +0000 | [diff] [blame] | 1251 | 'ignorecase' does not apply, use |/\c| in the pattern to |
| 1252 | ignore case. Otherwise case is not ignored. |
| 1253 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 1254 | 'redrawtime' defines the maximum time searched for pattern |
| 1255 | matches. |
| 1256 | |
Bram Moolenaar | c81e5e7 | 2007-05-05 18:24:42 +0000 | [diff] [blame] | 1257 | When matching end-of-line and Vim redraws only part of the |
| 1258 | display you may get unexpected results. That is because Vim |
| 1259 | looks for a match in the line where redrawing starts. |
| 1260 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 1261 | Also see |matcharg()| and |getmatches()|. The former returns |
Bram Moolenaar | 6ee1016 | 2007-07-26 20:58:42 +0000 | [diff] [blame] | 1262 | the highlight group and pattern of a previous |:match| |
| 1263 | command. The latter returns a list with highlight groups and |
| 1264 | patterns defined by both |matchadd()| and |:match|. |
| 1265 | |
| 1266 | Highlighting matches using |:match| are limited to three |
| 1267 | matches (aside from |:match|, |:2match| and |:3match|are |
| 1268 | available). |matchadd()| does not have this limitation and in |
| 1269 | addition makes it possible to prioritize matches. |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 1270 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1271 | Another example, which highlights all characters in virtual |
| 1272 | column 72 and more: > |
| 1273 | :highlight rightMargin term=bold ctermfg=blue guifg=blue |
| 1274 | :match rightMargin /.\%>72v/ |
| 1275 | < To highlight all character that are in virtual column 7: > |
| 1276 | :highlight col8 ctermbg=grey guibg=grey |
| 1277 | :match col8 /\%<8v.\%>7v/ |
| 1278 | < Note the use of two items to also match a character that |
| 1279 | occupies more than one virtual column, such as a TAB. |
| 1280 | |
| 1281 | :mat[ch] |
| 1282 | :mat[ch] none |
| 1283 | Clear a previously defined match pattern. |
| 1284 | |
Bram Moolenaar | fd2ac76 | 2006-03-01 22:09:21 +0000 | [diff] [blame] | 1285 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 1286 | :2mat[ch] {group} /{pattern}/ *:2match* |
Bram Moolenaar | fd2ac76 | 2006-03-01 22:09:21 +0000 | [diff] [blame] | 1287 | :2mat[ch] |
| 1288 | :2mat[ch] none |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 1289 | :3mat[ch] {group} /{pattern}/ *:3match* |
Bram Moolenaar | fd2ac76 | 2006-03-01 22:09:21 +0000 | [diff] [blame] | 1290 | :3mat[ch] |
| 1291 | :3mat[ch] none |
| 1292 | Just like |:match| above, but set a separate match. Thus |
| 1293 | there can be three matches active at the same time. The match |
| 1294 | with the lowest number has priority if several match at the |
| 1295 | same position. |
| 1296 | The ":3match" command is used by the |matchparen| plugin. You |
| 1297 | are suggested to use ":match" for manual matching and |
| 1298 | ":2match" for another plugin. |
| 1299 | |
| 1300 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1301 | vim:tw=78:ts=8:ft=help:norl: |