tags:

views:

107

answers:

2

I love smartcase, but there are times that I'd like to turn it off and search for just lowercase. Is there a builtin that will toggle smartcase, or do I need to write a function to toggle it?

+5  A: 

If you want to toggle if off completely, just do

:set nosmartcase

But if you want to toggle the mode of one-two searches, use special symbols in your search patterns:

  • \c makes the pattern ignore case, for example: /iGnOrEcAsE\c (matches "ignorecase");
  • \C makes the pattern match case, for example: /matchcase\C (doesn't march "MatchCase").
Pavel Shved
+6  A: 

If you add a bang after the option name in the set command, like this:

:set smartcase!

...it will toggle the option. You can create a key mapping to do this:

:map \s :set smartcase!<CR>

<CR> stands for carriage return. It represents the Return key.

Now just press \s and it toggles.

I usually write the mapping to also show in the bottom line whether the option is on or off, like this:

:map \s :set smartcase!<CR>:set smartcase?<CR>
Martinho Fernandes
Prefer <cr>, it's much simplier to maintain, and explain, than ^M.
Luc Hermitte
@Luc: I wasn't sure you could use that, and I didn't have vim handy to try, so I went with what I knew for sure. Will edit.
Martinho Fernandes