Vimgrep not matching regular expression properly - vim

If I do this:
:vimgrep /do_action\([ ]?'init'/ **/*.php
I get
E54: Unmatched \(
But I know that there are files with matching text. Why?
I would also be very grateful if someone could help me create a key mapping for cmd+l so that this search is initiated with "init" replaced with the word under the cursor, which I understand from various sources is possible.

Building on kev's answer, here is your mapping:
nnoremap <D-i> :vimgrep //do_action([ ]\?'<c-r>=expand('<cword>')<cr>'/ **/*.php<cr>
But I'd advise you to not use the Command key in a mapping: it works only in MacVim and it's a good habit to seek portability everywhere possible.
The <leader> key is perfect for such things.
nnoremap <leader>i :vimgrep //do_action([ ]\?'<c-r>=expand('<cword>')<cr>'/ **/*.php<cr>
See :help c_ctrl-r_= and :help leader.
edit
I went a little too fast on this one. Not only I've added unnecessary cruft to kev's reworking of your command (silly //) but I didn't notice that it could be simplified. This one is tested and working:
nnoremap <leader>i :vim do_action(\s*'<c-r>=expand('<cword>')<cr> **/*.php<cr>
Sorry for the mess.

You don't need to escape ( to match it. But you need to escape ? to match 0~1 previous item. Try:
:vimgrep /do_action([ ]\?'init'/ **/*.php

Related

How to disable/remove ctags from vim

I don't how to remove ctags from vim. Each time I type 'Ctrl+]' it tries to find the tags. I want to map this key for other usage. How can I do it?
Ctags is an external program that is not part of Vim. It can't be removed from Vim because it isn't there in the first place.
Vim's various ctags-related commands can't be removed either. If you want to map <C-]> to something else then there is nothing stopping you.
This normal mode mapping, for example, will delete the current word when you press <C-]>, effectively overriding the original meaning of <C-]>:
nnoremap <C-]> diw
See :help 05.4 for a gentle introduction to mappings.

When does expand need to be used when mapping a <cword> command in vim?

I can create a mapping that will search for the word under the cursor like so:
noremap <leader>/ :grep <cword> **<cr>
Why is it that unite mappings like unite (I doubt this is a unite specific issue) requires <C-r>=expand('<cword>')<CR> or <c-r><c-w> instead of <cword>?
nnoremap g/ :Unite -input=<C-r>=expand('<cword>')<CR> tag<CR>
The special characters like % and <cword> are explained under :help cmdline-special.
In Ex commands, at places where a file name can be used, the following
characters have a special meaning. These can also be used in the expression
function expand().
That explains why :edit <cword> works, but :echo "<cword>" does not: the latter does not take a file name. :grep just fuzzily refers to [arguments], but naturally some of them have to be files. The :Unite command is universal (and custom), so you need to use expand() there. When in doubt, just try it out :-)

Two key maps conflict in vim, need to know why

from cscope i got this nmap:
nmap <C-\>s :cs find s <C-R>=expand("<cword>")<CR><CR>
and i had these maps originally:
nmap <tab> v>
nmap <s-tab> v<
vmap <tab> >gv
vmap <s-tab> <gv
Now if i type s, then it will jump to the definition with one side effect:
it will tab the target line or target function/class content.
I can't find out where is this conflict. It seems that these two maps have no relationship.
I have tried out your mappings w/o any issue, so I assume there might be some kind of trailing charter issue like #glts suggested in his comment. Use :set list to show invisible characters.
However there are somethings you may want to consider:
Unless you are mapping to a plugin or a very special need you should almost always use nnoremap over nmap to prevent recursive mappings.
The vim way to indent/unindent is to use >> / << (or > / < in visual mode). If you want to repeat the command then use .. The dot command will redo the last change. You can use u to undo if you have gone to far. Although I understand the want for mappings such as these, learning and making a habit to use . will help you in your journey to vim nirvana.
Example of nice usage of the . command by showing off quick and dirty substitution.
Search for a common pattern in you file /pat
Change text to something else via c. e.g. cgnfoo<esc>
The gn motion will select the current pattern
repeat with . command.
For more help see:
:h mapping
:h .
:h u
:h >>

Where to find a list of special character representations?

For example backspace is <BS> and tab is <Tab>, but where can I look them up if I don't know or remember the sequence for, say, the up or down arrow?
Two :helpful tricks:
In insert mode and in the command line, hitting <C-v> followed by some key inserts that key's internal notation.
For example,
:helpCtrl+v↑
produces
:help <Up>
The :help command supports completion so you can type a keyword related to what you want and hit <Tab> or <C-d>:
:help key<Tab>
I was going to ask this, but then I found the answer by random luck in the help files. So here it is for those who are looking for the same:
:help keycodes

Searching vim history for matching first characters

In bash, adding the lines
"\e[B": history-search-forward
"\e[A": history-search-backward
to my .inputrc, allows me to search the history for expressions that begin with the characters in front of my cursor by using the <page-up>/<page-down> keys.
Can I achieve something similar in vim?
I already know about the possibility of opening a history window with q: and performing even complex searches there, but I am looking for a simple solution for the simplest case of history search.
Thank you!
This is built-in as <Up> and <Down>.
Of course, you can customize this, e.g.:
:cnoremap <PageUp> <Up>

Resources