I'd like to search for an upper case word, for example COPYRIGHT in a file. I tried performing a search like:
/copyright/i # Doesn't work
but it doesn't work. I know that in Perl, if I give the i flag into a regex it will turn the regex into a case-insensitive regex. It seems that Vim has its own way to indicate a case-insensitive regex.
You can use the \c escape sequence anywhere in the pattern. For example:
/\ccopyright or /copyright\c or even /copyri\cght
To do the inverse (case sensitive matching), use \C (capital C) instead.
As well as the suggestions for \c and ignorecase, I find the smartcase very useful. If you search for something containing uppercase characters, it will do a case sensitive search; if you search for something purely lowercase, it will do a case insensitive search. You can use \c and \C to override this:
:set ignorecase
:set smartcase
/copyright " Case insensitive
/Copyright " Case sensitive
/copyright\C " Case sensitive
/Copyright\c " Case insensitive
See:
:help /\c
:help /\C
:help 'smartcase'
You can set the ic option in Vim before the search:
:set ic
To go back to case-sensitive searches use:
:set noic
ic is shorthand for ignorecase
You can issue the command
:set ignorecase
and after that your searches will be case-insensitive.
You can use in your vimrc those commands:
set ignorecase - All your searches will be case insensitive
set smartcase - Your search will be case sensitive if it contains an uppercase letter
You need to set ignorecase if you want to use what smartcase provides.
I wrote recently an article about Vim search commands (both built in command and the best plugins to search efficiently).
To switch between case sensitive and insensitive search I use this mapping in my .vimrc
nmap <F9> :set ignorecase! ignorecase?
As others suggested:
:set ic
But the cool stuff is You can toggle such modes with:
:set ic!
The good old vim[grep] command..
:vimgrep /example\c/ &
\c for case insensitive
\C for case sensitive
% is to search in the current buffer
By default, all searches in vi are case-sensitive. To do a case-insensitive search, go into command mode (press Escape), and type-
:set ignorecase
You can also type -
:set ic as an abbreviation.
To change back to case-sensitive mode, type-
:set noignorecase or :set noic in command mode
put this command in your vimrc file
set ic
always do case insensitive search
I prefer to use \c at the end of the search string:
/copyright\c
As #huyz mention sometimes desired behavior is using case-insensitive searches but case-sensitive substitutions. My solution for that:
nnoremap / /\c
nnoremap ? ?\c
With that always when you hit / or ? it will add \c for case-insensitive search.
Vim have 2 modes
1.edit mode
normal mode( Esc )
Search will work for normal mode
/\c for case sensitive
/\csearch
You can set ignorecase by default, run this in shell
echo "set ic" >> ~/.vimrc
Note it is a difference where you place modifiers such as "\c" in your expresion:
You can use the \c escape sequence anywhere in the pattern
Regardless from the accepted answers, which states that it is no difference of where to place modyfier in a regex pattern, its looks like it actually does matter.
example text:
asdasdasdasdasd wiktor asdasdasdasd
adasdasdasd wiktor asdasda ahjkjlkhjkl
asdasd asd asdasdasdasd iuuuu -
asdjkkkkkkkaopbsdasda
wiktor ----(---------------------)--
Match
\c^.*A?.*$
^\c.*A?.*$
^.*\cA?.*$
^.*A\c?.*$
will output:
No match
^.\c*A?.*$
^.*A?\c.*$
^.*A?.\c*$
^.*A?.*$\c
will output:
vim -version VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Jun 1 2020 06:42:35)
Included patches: 1-869
Some important information, if u want to find out more about the commands of vim, as mentioned below u can give a try the following steps :
invoke the command "help" follow by a space and then complete the word with TAB key, once u find the right command press return key.
:help ignorecase
information like the following will be displayed :
you will be able to move forward and backward and also watch the short command, such as the case of "ignorecase" ( 'ic' ). In addition, another short example could be the case of 'smartcase' ('scs' and some more) :
In order to leave of the documentation just type ":q" as usual and you will return to "command mode" .
:q
I really hope the information provided would be helpful for someone.
Best regards,
Related
I like to use strict mode. When I am typing lower case variable (e.g. "path") and press <C-N> I do not want to see constants or class names with this prefix (e.g. "PATH_TO_ROOT" or "PathGenerator"). Also, constants are usually at the top of completion and I have to press extra keys. That is why I have disabled ignorecase option. On the other hand, I almost newer pay attention to case when I am searching the buffer. As far as I know there is no dedicated option for searching or insert completion. I can place \c in the pattern, but again it requires extra keystrokes each time. Okay we can use mapping:
cnoremap <expr><CR> getcmdtype() =~ "[/?]" && getcmdline() !~ '\\c$' ? "\<End>\\c\<CR>" : "\<CR>"
But this reduces readability a little bit. I am wonder is there a better solution? A dedicated option I am not aware of? Following options are also declined:
Setting the option when entering command line mode and disabling when entering insert mode. This will cause to redraw the screen if there are highlighted matches from previous search. Highlighted Foo and FOO will be disappeared when entering insert mode and will be highlighted again when entering command line mode.
Setting #/ register.
cnoremap <expr><CR> getcmdtype() =~ "[/?]" ? "\<Esc>:set #/ = histget("search")" : "\<CR>"
It is not working in visual mode (correct me if I am wrong).
It also does only forward searching.
The question
How to do case sensitive insert (cmd-line) completion with ignorecase set, or how to do case insensitive search when ignorecase is not set.
Requirements
It works both in visual/normal mode.
Forward/bakward search.
The pattern is present in history.
Please keep in mind the solution must be simple. I would rather do case sensitive search all the time than keeping a plugin for searching.
Why not just KISS it?
noremap / /\c
noremap ? ?\c
Yes, it only saves two keystrokes.
In vim, I have to change someText.someTextAfterDot string to someText.somethingDifferent. The cursor is at the t in After. What should be the most efficient way to do this?
If I use 'cw' command then that removes the complete someText.someTextAfterDot including someText which I don't think is efficient because now I have to type the someText again.
Normally, I use 'F.ce' (find the last . and edit till end of current word) command and then type somethingDifferent. This seems like a lot of work as in other editors (like WebStorm or Sublime) a simple double click on someTextAfterDot can do the trick.
Is there any other more efficient way to do this in vim?
First of all cw should only go forward so only remove terDot. If cw is doing anything more then I assume you have some bad mappings.
The "inner" word text object, iw, is what you need. e.g. ciw. However if you have added . to 'iskeyword' then word's will include . and the behavior of ciw will be incorrect.
To find out where 'iskeyword' is last set use the following command:
:verbose set iskeyword?
For more help see:
:h 'iskeyword'
:h iw
:h w
:h cw
:h :verbose
My intuition would be to type bdeisomethingDifferent. I dunno that it's a great improvement, but it at least doesn't make you type the . again the way F.ce does.
I normally use change inner word for that. You simply have to type
ciwsomethingDifferent
Check the documentation for text object selection too.
I often find myself doing
:s/foo/bar/g
*move to different line*
:s/bar/foo/g
on different lines. Is there an easy way to swap them around so that I can execute the second version quickly?
You could try the Abolish plugin (git homepage):
:Subvert/{foo,bar}/{bar,foo}/g
Without plugin:
:%s/foo\|bar/\=submatch(0) ==# 'foo' ? 'bar' : 'foo'/g
The quick use once only option is to do the following
:s/~/<c-r>//g<cr>
~ matches the last substitution and <c-r>/ will insert the current search string from the "/ register. Therefore flipping the substitution. A word of warning is that ~ can only be used once because after the substitution it will be changed. Also doing a search between substitutions will result in the "/ register changing.
As an alternative you could try to use the command-line window to edit the command like text in any other window.
Use q: to open the command-line window from normal mode or press ctrl-f from the command line (assuming the default setting for 'cedit').
Drew Neil has a vimcasts episode that deals with refining search patterns via the command-window which is similar.
:h /~
:h c_CTRL-R
:h quote/
:h cmdwin
:h q:
:h 'cedit'
You can use dict in vim
:%s/foo\|bar/\={'foo':'bar', 'bar':'foo'}[submatch(0)]/g
In vim, there is the ** wild card that can be used for file opening using :e for instance. The wildcard allows to open for downward search of a file. Is there a way to tweak vim so that the search is case insensitive?
As Carpetsmoker point out:
type :tabe /path/to/dir to browse the path (use . if already in that dir)
type /filename\c to search you file ignorecase
press Enter to open your FileName
There are a two options which control case matching while searching, ignorecase and smartcase.
Excerpt from From :help ignorecase
If the 'ignorecase' option is on, the case of normal letters is
ignored. 'smartcase' can be set to ignore case when the pattern
contains lowercase letters only.
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.
Additionally, from :help *
'ignorecase' is used, 'smartcase' is not.
So, setting ignorecase (with or without combining it with smartcase) should work.
You may also want to combine them in a keybind. For example:
:nmap i* :set ignorecase<CR>*
:nmap c* :set noignorecase<CR>*
does anybody know how to perform a case insensitive file search with vim's plugin CtrlP? I can't seem to find that in the instructions anywhere. Ideally it would replace the standard CTRL+P shortcut.
Thank you!
The file search relies on the match function in VIM. To make the search case insensitive, simply issue the following command:
:set ignorecase
If you want this to be the default behaviour, you can add the command to your .vimrc file.