Carlos A. Gómez
Carlos A. Gómez

Reputation: 362

Ignore the case in part of the search pattern in Vim

In the next search on Vim I would like to ignore the case of the first letter:

/[tlcp]omo

I'd like to know how the case can be ignored for only the first letter of the search pattern.

Vim has the following options to ignore the case in the search pattern:

:set ignorecase
:set smartcase [ignore case if no uppercase in search]

or use \c it at any position in the search pattern:

/hello\c => [find hello and HELLO]

But all of these options ignore the case in the entire pattern, not in part.

One option to ignore the case of a single letter in the search pattern is, using the [] collection of regular expression, to specifically capitalize each letter:

/[tTlLcCpP]omo

But, is there any way to ignore the case in a part of the search pattern without having to specify each and every upper and lower case character using regular expression?

Upvotes: 3

Views: 1301

Answers (2)

Ingo Karkat
Ingo Karkat

Reputation: 172540

My CmdlineSpecialEdits plugin has (among many others) a CTRL-G c mapping that converts a pattern in the search command-line in such a way that those alphabetic characters between \c...\C become case-insensive matches while the rest remains case-sensitive. In other words, it converts the pattern as if \c and \C would only apply to following atoms, and not the entire pattern.

Example

/My \cfoo\C is \cbad!/

becomes

/My [fF][oO][oO] is [bB][aA][dD]!/

or alternatively

/\c\%(\u\&M\)\%(\l\&y\) foo\%(\l\{2}\&is\)  bad!/

Upvotes: 0

Ingo Karkat
Ingo Karkat

Reputation: 172540

In general, this isn't possible in Vim. The /\c and /\C regexp modifiers unfortunately turn the whole pattern into case (in-)sensitive matching, regardless of where they are placed. (Introducing a new set of modifiers that only work from that position onwards would in my opinion be the best solution.)

Most people usually get around this by using lower/uppercase collections for the insensitive parts, /like [tT][hH][iI][sS]/.

You could also go the opposite route and instead force certain characters to a case (using /\l for lowercase and /\u for uppercase), /\c\%(\l\l\l\l\&like\) this/.

Upvotes: 2

Related Questions