Skip to content
Search

Vim:Regex

lastmod 2025-04-17created 2016-01-095 edits

Magic mode

magic
Only a few symbols such as ., $, and * keep their special meaning. (default)
nomagic
All symbols except $ are interpreted literally.
very magic
Almost all symbols have a special meaning, and must be escaped to be interpreted literally.
vary no magic
All symbols are interpreted literally, and must be escaped to have a special meaning.

Substitute Command

:range s[ubstitute]/pattern/string/cgiI

For each line in the range replace a match of the pattern with the string where:

  • c - Confirm each substitution
  • g - Replace all occurrences in the line (without g - only first).
  • i - Ignore case for the pattern.
  • I - Don't ignore case for the pattern.

Case conversion

  • & - The entire text matched by the pattern.
  • \U - Convert all following characters to uppercase
  • \u - Convert only the next character to uppercase
  • \E - End conversion

Examples

:%s/pattern/\U&/g
Uppercase the entire matched text
:%s/\(pattern\)/\U\1/g
Convert only a specific matched group to uppercase

Global Command

:range g[lobal][!]/pattern/cmd

Execute the Ex command cmd (default ":p") on the lines within [range] where pattern matches. If pattern is preceded with a ! - only where match does not occur.

Escaped characters or metacharacters

  • . - any character except new line
  • \s - whitespace character
  • \S - non-whitespace character
  • \d - digit
  • \D - non-digit
  • \x - hex digit
  • \X - non-hex digit
  • \o - octal digit
  • \O - non-octal digit
  • \h - head of word character (a,b,c...z,A,B,C...Z and _)
  • \H - non-head of word character
  • \p - printable character
  • \P - like \p, but excluding digits
  • \w - word character
  • \W - non-word character
  • \a - alphabetic character
  • \A - non-alphabetic character
  • \l - lowercase character
  • \L - non-lowercase character
  • \u - uppercase character
  • \U - non-uppercase character

To paste yanked text

Ctrl + r -> "

See also

Favorite site

References


  1. Vim_Regular_Expressions_101.pdf ↩

author Zer0 sitemap