use specific search with no highlight - vim

For example I'm in python code and want to jump between classes:
nnoremap <buffer> [c /^\s*class\ <CR>
How to prevent them from highlight in more elegant way than :nohl at the end of command each time?

You can avoid highlighting search matches by using the :help search() function or writing your own function.
With search()
nnoremap <buffer> <silent> [c :<C-u>call search('^\s*\zsclass\s')<CR>
With your own function
" with ':help :normal'
function! JumpToNextClass()
normal! /^\s*\zsclass\s
endfunction
" with ':help search()'
function! JumpToNextClass()
call search('^\s*\zsclass\s')
endfunction
nnoremap <buffer> <silent> [c :<C-u>call JumpToNextClass()<CR>
But none of that really matters since Vim already comes with ]] and [[.

Related

Prevent grep results opening in NerdTree window

I've just started using EasyGrep . Often my NERDTree window is in focus when I am doing a search and the first search result loads in this window.
Is there a way to force EasyGrep to open the first result in the main buffers window?
Thanks!
Here is what I added in my .vimrc:
function! SearchOutsideOfNerdtree()
if &filetype == 'nerdtree'
:wincmd l
endif
:execute input("", ":Grep\<Space>")
endfunction
function! ReplaceOutsideOfNerdtree()
if &filetype == 'nerdtree'
:wincmd l
endif
:execute input("", ":Replace\<Space>")
endfunction
nnoremap <Leader>f :call SearchOutsideOfNerdtree()<CR>
nnoremap <Leader>g :call ReplaceOutsideOfNerdtree()<CR>
Basically I created a function that checks if the current filetype is nerdtree and I move to the right buffer.
Here is what I added in my .vimrc and it works:
nnoremap <silent> ,g :<C-u>wincmd w<CR>:Unite grep:. -buffer-name=search-buffer<CR>

vim: visual star search not working as expected

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.

using variable and expressions in search and replace command

I am trying to set some key mapping/macros to comment/uncomment blocks of text in my .vimrc but I can't manage to make it work.
I define some variables:
let g:comment_id='~'
autocmd BufRead,BufNewFile *.c,*.cpp,*.cxx,*.h,*.hpp,*.hxx,*.ipp let b:comment_open='//'
autocmd BufRead,BufNewFile *.f90,*.F90 let b:comment_open='!'
And then I try differents ways to use them but everytime it fails:
noremap <silent> ,cc :s/^/<C-R>=(b:comment_open.g:comment_id)/<CR>:nohlsearch<CR>
noremap <silent> ,uc :s/<C-R>=(b:comment_open.g:comment_id)//<CR>:nohlsearch<CR>
->
E15: Invalid expression: /,
E15: Invalid expression: (b:comment_open.g:comment_id)//
noremap <silent> ,cc :s/^/<C-R>=(b:comment_open.g:comment_id)<CR>/<CR>:nohlsearch<CR>
noremap <silent> ,uc :s/<C-R>=(b:comment_open.g:comment_id)<CR>//<CR>:nohlsearch<CR>
->
E488: Trailing characters
noremap <silent> ,cc :s/^/\=(b:comment_open.g:comment_id)/<CR>:nohlsearch<CR>
noremap <silent> ,uc :s/\=(b:comment_open.g:comment_id)//<CR>:nohlsearch<CR>
->
cc: ok,
uc:
E64: \= follows nothing,
E476: Invalid command
I fact I can't understand how 'C-R' and \= work and mean...
Thanks for your help
Use:
nnoremap <silent> ,cc :s/^/\=b:comment_open.g:comment_id/<CR>:nohlsearch<CR>
nnoremap <silent> ,uc :s#\V<c-r>=escape(b:comment_open.g:comment_id,'\#')<cr>##<cr>:nohlsearch<cr>
Reference: :help sub-replace-expression
It is necessary in the pattern to turn verynomagic on to escape all special characters, and even with that you need to escape both the delimiter and the backslash.

vim bindkey-snipplet to shift a hole block of code around

I've just made a little snipplet on my keybindings.vim to shift a hole block of code around. It works basically the same way I do it in the old fashioned manual way ( on normal mode, {V} to select the hole block on visual mode, then > and < to move through indentation, and :m+ and :m-2 to move down or up ).
The problem I'm getting is that it works only to move around through indentation (horizontally), but for vertical move, it does not work. I just can't find the reason, as doing exactly the same sequence manually (as I'm used to), it works like a charm.
First of all: I've tested the snipplet on Vim 7.2 (on Linux) and Vim 7.3 (on MacOS).
Second: I know If I put a:
vnoremap < <gv
...and a:
vnoremap > >gv
...in my keymaps, I'll be able to move visual-selected blocks without loosing the visual selection... in spite of this, I'd like to get this working so I don't have to take care of the visual selection with a manual ESC {jV}k
Can anybody tell me what am I doing wrong? I thank you all in advance!
Regards!
"============================================================================
"Ctrl + Shift + > [normal or insert mode] - move entire block around
"============================================================================
nnoremap <silent> <C-S-Right> :let savecur=getpos(".")<CR>{V}><CR>
\:call setpos('.', savecur)<CR>4l
inoremap <silent> <C-S-Right> <Esc>:let savecur=getpos(".")<CR>{V}><CR>
\:call setpos('.', savecur)<CR>5li
nnoremap <silent> <C-S-Left> :let savecur=getpos(".")<CR>{V}<<CR>
\:call setpos('.', savecur)<CR>4h
inoremap <silent> <C-S-Left> <Esc>:let savecur=getpos(".")<CR>{V}<<CR>
\:call setpos('.', savecur)<CR>3hi
nnoremap <silent> <C-S-Up> :let savecur=getpos(".")<CR>{V}:m-2<CR>
\:call setpos('.', savecur)<CR>k
inoremap <silent> <C-S-Up> <Esc>:let savecur=getpos(".")<CR>{V}:m-2<CR>
\:call setpos('.', savecur)<CR>ki
nnoremap <silent> <C-S-Down> :let savecur=getpos(".")<CR>{V}:m+<CR>
\:call setpos('.', savecur)<CR>j
inoremap <silent> <C-S-Down> <Esc>:let savecur=getpos(".")<CR>{V}:m+<CR>
\:call setpos('.', savecur)<CR>ji
"============================================================================
Change the :move commands used in those mappings for moving a paragraph down
from :m+ to :m'>+.

vim : <silent> nmap

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>

Resources