5

ignorecase is a global option, so it's not possible to locally set ic

:setlocal ic

This is a bit unfortunate, since I work on buffers in which I need to search case insensitively and other buffers in which I need to search case sensitively.

So, is there a trick to set ic locally in a buffer.

3 Answers 3

6

I guess the usual trick is to set ignorecase and smartcase and depending on what you need, search using only lower case (which means to ignore the case for that particular search) or search using mixed case, if you need to have the search match the provided case.

The other alternative is to use the '\c' and '\C' atoms when searching (see the help at :h /\c and :h /\C but this is usually a hassle to remember.

3
  • The problem with smartcase is that it is also a global option. Sep 29, 2015 at 2:43
  • Yes and a buffer/window local option smartcase wouldn't make sense. I fail to see, why this is a problem however Sep 29, 2015 at 5:58
  • Yes, you're right! Sep 29, 2015 at 7:54
9

You can always prefix your search expression with \c to enforce case insensitivity (and \C for case sensitivity). From :h ignorecase:

                                                        /\c /\C
When "\c" appears anywhere in the pattern, the whole pattern is handled like
'ignorecase' is on.  The actual value of 'ignorecase' and 'smartcase' is
ignored.  "\C" does the opposite: Force matching case for the whole pattern.
{only Vim supports \c and \C}
Note that 'ignorecase', "\c" and "\C" are not used for the character classes.

So, for example, to have case-sensitive search for name:

/\Cname

And for case-insensitive:

/\cname

You could perhaps map these to some shortcut, say:

nnoremap <leader>c/ /\c
0
2

You could modify ignorecase when you move in or out of a window or buffer, so it will always have the desired value.

Automatically by file type

autocmd WinEnter * let &ignorecase = match( bufname("%"), '\.php$' ) != -1
autocmd BufEnter * let &ignorecase = match( bufname("%"), '\.php$' ) != -1

This would set ignorecase on *.php files, and unset it otherwise.

You need to write the same command for both WinEnter and BufEnter, but they could call out to a function if you want to avoid repetition.


Manually set for each buffer (like setlocal)

Since you asked how to set ignorecase locally, this solution might give you something close to what you wanted:

command SetLocalIgnoreCase    set ignorecase   | let b:ignorecase = 1
command SetLocalNoIgnoreCase  set noignorecase | let b:ignorecase = 0

autocmd WinEnter * let &ignorecase = get(b:, 'ignorecase', 0)
autocmd BufEnter * let &ignorecase = get(b:, 'ignorecase', 0)

In the above, b:ignorecase is a buffer-local variable, which the autocmds can pick up when switching focus. If you wanted window-local, use w: instead.

We use get() to avoid errors in buffers where the variable does not exist, and default to 0 in that case.

(With liberal use of eval this approach could even be generalised to other global options that you want to act as locals.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.