I copied this function to visually search with * and #:
function! s:VSetSearch(cmdtype)
let temp = #s
norm! gv"sy
let #/ = '\V' . substitute(escape(#s, a:cmdtype.'\'), '\n', '\\n', 'g')
let #s = temp
endfunction
xnoremap * :<C-u>call <SID>VSetSearch('/')<CR>/<C-R>=#/<CR><CR>
xnoremap # :<C-u>call <SID>VSetSearch('?')<CR>?<C-R>=#/<CR><CR>
The # mapping works fine but the * mapping doesn't exit visual selection (it extends the range of the visual selection until the next searched word). I don't understand why this is happening. Is there a solution?
EDIT: To reproduce the problem save the code snippet, download the MS Installer, open cmd.exe and start vim vim -u NONE, then do :set nocp and finally source the saved code. In fact, the following simple mapping doesn't work either:
nnoremap * *<C-o>
EDIT 2: Can someone else reproduce this issue? Should it be reported?
EDIT 3: I believe that the problem (bug?) is that the * (star) key cannot be remapped: if I start vim with vim -N -u NONE (Vim 7.4 with patches 1-274) and run the command :noremap * :echo "star"<CR> and press *, vim tries to perform a search. I also reported this to the vim dev group.
The following is what I use :
function! s:Vword()
return getline('.')[col("'<")-1:col("'>")-1]
endfunction
xnoremap <silent> * <Esc>/\v<<C-R>=<SID>Vword()<CR>><CR>
xnoremap <silent> g* <Esc>/\v<C-R>=<SID>Vword()<CR><CR>
xnoremap <silent> # o<Esc>?\v<<C-R>=<SID>Vword()<CR>><CR>
xnoremap <silent> g# o<Esc>?\v<C-R>=<SID>Vword()<CR><CR>
nnoremap <silent> g// :grep -w <cword> <C-R>=getcwd()<CR><CR>
nnoremap <silent> g/* :grep <cword> <C-R>=getcwd()<CR><CR>
xnoremap <silent> g// :<C-U>grep -w <C-R>=<SID>Vword()<CR> <C-R>=getcwd()<CR><CR>
xnoremap <silent> g/* :<C-U>grep <C-R>=<SID>Vword()<CR> <C-R>=getcwd()<CR><CR>
I have also additionally added nice mappings for g* & similarly g# and also a bunch of mappings for invoking grep that I find very useful.
Edit: minor fixes to code.
Mapping <kMultiply> instead of * solved the problem. Really strange since I do not use the keypad multiply key.
Related
I have the following on my .vimrc
au FileType ruby nnoremap <expr> <leader>t ':!rspec ' . expand('%') . ':' . line('.')
This executes rspec on the line specified, and gives me option to edit the line before pressing enter. But I have to be on the test file so it get the file name and line number correctly.
While developing I run nnoremap <leader>t :!rspec some/spec/file.rb:123 manually to run the test I want from anywhere in the code. So I can code and fire the test without need to visit the test file.
The problem is that if I visit another ruby file the mapping in .vimrc runs again and I loose the nnoremap command I used before. Is there a command to only map (in normal mode) if there isn't already a map for that sequence?
Regards,
This should be a buffer-local mapping. Use <buffer>:
au FileType ruby nnoremap <buffer> <expr> <leader>t ':!rspec ' . expand('%') . ':' . line('.')
We can do better!
Use an augroup and make is self clearing to make it safe to re-:source.
augroup ruby_testing
autocmd!
autocmd FileType ruby nnoremap <buffer> <expr> <leader>t ':!rspec ' . expand('%') . ':' . line('.')
augroup END
Even better forgo the autocmd and put this mapping in the after directory. Add the following line to ~/.vim/after/ftplugin/ruby.vim:
nnoremap <buffer> <expr> <leader>t ':!rspec ' . expand('%') . ':' . line('.')
For more help see:
:h :map-local
:h :augroup
:h after-directory
Vim has the :help :map-<unique> modifier which makes the mapping fail if such mapping already exists.
au FileType ruby nnoremap <unique> <expr> <leader>t ...
You can suppress the error message with :silent!:
au FileType ruby silent! nnoremap <unique> <expr> <leader>t ...
Alternatively (and this is slightly better because it doesn't suppress any other errors in the mapping definition, like syntax errors), you could explicitly check for the mapping's existence via maparg():
au FileType ruby if empty(maparg('<leader>t', 'n')) | nnoremap <expr> <leader>t ... | endif
Note that this literally implements that (I understand) you're asking for; i.e. the first Ruby file will define the mapping, and any subsequent Ruby files are ignored; the mapping will always use the first file's name and line number. If you instead want to have different right-hand sides for the same mapping, depending on the currently edited file, the solution is a buffer-local mapping as per #PeterRincker's answer. But with that, you have to be inside the original Ruby buffer to trigger the correct mapping.
A remedy for that might be to recall the executed command-line from the command-line history (should happen automatically for your incomplete mapping, else via histadd()), so that you can easily recall the command from another file.
I would like Vim to place my cursor in the middle of screen after search. I have achieved for *, #, n and N commands with the following lines in .vimrc
nmap * *zz
nmap # #zz
nmap n nzz
nmap N Nzz
My question is: how to map / and ? the same way? I.e. I would like to position cursor after some text has been found using
/some-text-to-find-forward
?some-text-to-find-backward
Edit: Threw away my initial answer as it was too much of a kludge. Here's a much better solution.
function! CenterSearch()
let cmdtype = getcmdtype()
if cmdtype == '/' || cmdtype == '?'
return "\<enter>zz"
endif
return "\<enter>"
endfunction
cnoremap <silent> <expr> <enter> CenterSearch()
The way this works is to remap Enter in command-line-mode to a custom expression.
The function performs the current search followed by zz if the command-line is currently in a search. Otherwise it just executes whatever command was being done.
It's not very pretty, but
:nnoremap / :execute "normal! /\<lt>cr>zz"<c-left><right>
will get the job done. (Puts an :execute "normal! /" command on the commandline, then adds a <cr>zz to the end to it so that you automatically zz when you issue the command. The final <c-left><right> just steps into the search pattern at the right spot
Solution from Randy Morris but as a oneliner:
cnoremap <silent><expr> <enter> index(['/', '?'], getcmdtype()) >= 0 ? '<enter>zz' : '<enter>'
I ask this question generally, but I will put it in terms of the specific problem I'm having.
I'm using the vim-lawrencium plugin for a project under Mercurial source control. I use the :Hgstatus command to open the status buffer.
The status buffer comes with some nice keymaps to make it easy to add files to the commit, look at diffs, and finalize the commit.
nnoremap <buffer> <silent> <cr> :Hgstatusedit<cr>
nnoremap <buffer> <silent> <C-N> :call search('^[MARC\!\?I ]\s.', 'We')<cr>
nnoremap <buffer> <silent> <C-P> :call search('^[MARC\!\?I ]\s.', 'Wbe')<cr>
nnoremap <buffer> <silent> <C-D> :Hgstatustabdiff<cr>
nnoremap <buffer> <silent> <C-V> :Hgstatusvdiff<cr>
nnoremap <buffer> <silent> <C-U> :Hgstatusdiffsum<cr>
nnoremap <buffer> <silent> <C-H> :Hgstatusvdiffsum<cr>
nnoremap <buffer> <silent> <C-A> :Hgstatusaddremove<cr>
nnoremap <buffer> <silent> <C-S> :Hgstatuscommit<cr>
nnoremap <buffer> <silent> <C-R> :Hgstatusrefresh<cr>
nnoremap <buffer> <silent> q :bdelete!<cr>
Most of these seem to work. I've successfully tried diffing, adding, and q to close the status, but the <C-S> shortcut doesn't work at all. I can manually run the :Hgstatuscommit command it's mapped to. I've also looked at :map to verify the key is actually mapped for that buffer, and it does show up in the list.
What is my next step in debugging this? I want to fix this specific problem, but I also want to know how to fix it in the event I run across broken shortcuts in the future. I'm new to Vim, so I'm at a bit of a loss at this point.
UPDATE: Output of :verbose map <C-S>
v <C-S> *#:Hgstatuscommit<CR>
Last set from ~/.spf13-vim-3/.vim/bundle/vim-lawrencium/plugin/lawrencium.vim
n <C-S> *#:Hgstatuscommit<CR>
Last set from ~/.spf13-vim-3/.vim/bundle/vim-lawrencium/plugin/lawrencium.vim
SOLUTION: Turned out the problem was that my shell was intercepting Ctrl-S and it was never getting to Vim.
I added a Vim alias to my .zshrc to fix:
alias vim="stty stop '' -ixoff ; vim"
ttyctl -f
Found the fix on the Vim Wikia which also has a solution for bash shells.
Software Flow Control
If you are using using a terminal then it is often the case that <c-s> is being used for terminal's software flow control (XON/XOFF). Which makes <c-s> a trickier key to map.
Turn off flow control by adding the following to some startup script (e.g. ~/.bash_profile or ~/.bashrc):
stty -ixon
If you have frozen your terminal then you can unfreeze it by pressing <c-q>.
Generic map debuging
You can debug pretty much any custom vim mapping via the following command:
:verbose map
This will list out each key/chord ({lhs}) maps to what command ({rhs}), mode, and file the mapping was sourced from. For more information on this listing see :h map-listing and :h :map-verbose.
We can filter this list in a few ways:
Supplying a mode. e.g. :verbose nmap for normal mode and :verbose imap for insert mode.
Proving the key we want to query for. e.g :verbose nmap <c-s>
Can also see buffer specific mappings by adding <buffer>. e.g. :verbose nmap <buffer> <c-s>
So for your question the best way to debug what your mapping is set to would be to run the following query:
:verbose nmap <buffer> <c-s>
Note: Vim's native command are not listed via :verbose map. The best way to find one of Vim's native commands is to help. See :h for more.
First, check that <C-S> still mapped to :Hgstatuscommit
map <C-S>
Hgstatuscommit calls s:HgStatus_Commit. Open its definition on line 1134 and put some debugging print outs:
echom a:linestart
echom a:lineend
echom a:bang
echom a:vertical
After using the mapping, check :messages.
I’d suspect that <C-S> is mapped to something else. You can use :map
<C-S> to check how (or if) its mapping is configured. Even better, you can
add the prefix to see where the mapping was set from, e.g., when I run
:verbose map <C-L>, the following is displayed:
<C-L> * :noh<CR><C-L>
Last set from ~/.vimrc
By contrast, I haven’t set a mapping for <C-S> so when I run, :map <C-S>,
I get:
No mapping found
Prepending verbose to a command is a useful general debugging technique as it can show where any Vim option was set, e.g., :verbose set background? shows what the background option is currently set to and which Vim configuration file it was set in:
background=dark
Last set from ~/.vimrc
I have created the following mapping in my vimrc file:
noremap <C-p> ! firefox -new-tab http://php.net/<cword><C-m>
The problem is that it proceeds to delete the line under the cursor.
How would I make this mapping work correctly?
If I understood what you're trying to do the following should work:
nmap <c-p> :!firefox -new-tab http://php.net/<c-r>=expand('<cword>')<CR><CR>
See docs for further information.
You are using noremap which means that it triggers:
in normal mode
in visual mode
in operator-pending mode.
You should use nnoremap or xnoremap instead. Also, note that ! is a shortcut for :.! in normal mode (acts on current line) and for :'<,'>! in visual mode (acts on current visually-selected lines). If you don't want that:
nnoremap <c-p> :!firefox ...
xnoremap <c-p> :<c-u>!firefox ...
In vim I have this nmap
nmap <silent> ,mu : marks ABCDEFGHIJKLMNOPQRSTUVWXYZ<CR>
If I don´t have Upper marks and try ,mu I get
E283: No marks matching "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
why don't show the Error output ?
Try
nnoremap <silent> ,mu :execute "try\nmarks ABCDEFGHIJKLMNOPQRSTUVWXYZ\ncatch /\\V\\^Vim(marks):E283:/\nendtry"<CR>
By the way, is there a reason for writing :nmap instead of :nnoremap? You should not do this if you don't have a reason unless you want to run in the situation where you can't predict what will be the result of adding another mapping (directly to vimrc or by installing a plugin).
Edit (sehe)
To make things more readable, I'd suggest using a snippet like this in your $MYVIMRC:
function! ShowGlobalMarks()
try
marks ABCDEFGHIJKLMNOPQRSTUVWXYZ
catch /E283:/
endtry
endfu
nnoremap <silent> ,mu :call ShowGlobalMarks()<CR>