I have a tabularize command like this :Tabularize /^[^:]*\()\s\?\|#\)\#<!\zs:\ze.*[^:]/l1 which work for my need,
but I need to enter it as a mapping.
However :noremap <Leader>a: :Tabularize /^[^:]*\()\s\?\|#\)\#<!\zs:\ze.*[^:]/l1<CR> fails.
I think it is because of \|.
How can I fix it?
Romain, as always, had the answer.
It was simply a matter of doubling the backquote, as in \\|.
Related
I am using the plugin ag.vim in Vim, and I want to do a simple re-mapping.
When I run ag with :Ag I want remap it to :ag so I don't have to use shift.
example: :ag string_to_find instead of :Ag string_to_find
How can I do this?
You shouldn't. Lowercase commands are reserved for built-in commands, and the norm/rules are to start custom commands with an uppercase character (just like with functions).
See Is it possible to remap an Ex command in Vim (remap :Ack to :ack)?
This isn't exactly what you asked for, but dding the following will allow you to just type 'ag' to search:
map ag :Ag
EDIT: The downvoters are right and this is a bad answer. I'll leave it here so future searchers know it's wrong.
Try:
command: Ag ag
See :help command for more.
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
I usually do a search and replace in vim that looks like this:
:%s/\([\.!?]\|[\.!?]"\)\s\s/\1text /g
So it looks for either a period, exclamation point, question mark, or any of the above followed by a quote and two spaces. It works fine.
But when I map this in my vimrc like so
map <F4> :%s/\([\.!?]\|[\.!?]"\)\s\s/\1text /g<CR>
and then press F4 I get this error message:
E486: Pattern not found: \([\.!?]|[\.!?]"\)\s\s
It is removing the backslash before the pipe for some reason and I have no idea why. Does anyone know how to correct this behavior?
In a :mapping, the pipe character is special. Use <Bar> instead:
:noremap <F4> :%s/\([\.!?]\<Bar>[\.!?]"\)\s\s/\1text /g<CR>
Also, you should use :noremap; it makes the mapping immune to remapping and recursion.
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>
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