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>
Related
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 [[.
I have the following mappings in my .vimrc file that I use to move between windows, but when in an explore window :e of netrw plugin the shift down key will produce a warning window instead of respecting my mappings. I am assuming this mapping must be hard coded into the plugin itself. How can I remove the shift-up and shift-up mappings in the plugin.
nnoremap <silent> <S-Up> :wincmd k<CR>
nnoremap <silent> <S-Down> :wincmd j<CR>
The window that shows up when trying to do a shift-up or shift-down is below, which shows up in a new split which is very annoying:
**warning** (netrw) using Nexplore or <s-down> improperly; see help for netrw-starstar
As a result, my question is how can I shut off this behavior in the netrw plugin so that it respects my mappings shown above instead.
A buffer map for that is created by the plugin, so you could overwrite it again after created with an autocmd:
autocmd filetype netrw nnoremap <buffer> <s-down> :wincmd j<cr>
If using multiple times, it might be useful to create a group:
augroup netrw_maps
autocmd!
autocmd filetype netrw call ApplyNetrwMaps()
augroup END
function ApplyNetrwMaps()
nnoremap <buffer> <s-up> :wincmd k<cr>
nnoremap <buffer> <s-down> :wincmd j<cr>
" ...
endfunction
Background: I'm using skwp's dotfiles, and his recently changes is breaking some functionalities I use on daily basis.
Instead of set up the mappings globally, I'm trying to nnoremap two shortcuts upon quickfix enters and nunmap after quickfixes quits.
BTW, I think syntastic is used for linting, which invokes the quickfix/location lists.
Here's the code:
augroup quickfixShortcutsGroup
autocmd!
autocmd BufWrite * :echom "Foo"
" au BufReadPost quickfix nnoremap <silent> <C-z> :cp<CR>
" au BufReadPost quickfix nnoremap <silent> <C-x> :cn<CR>
au BufWinEnter quickfix nnoremap <silent> <C-z> :cp<CR>
au BufWinEnter quickfix :echo '1'
au BufWinLeave quickfix nnoremap <silent> <C-z> :cp<CR>
au BufWinLeave quickfix :echo 'BufWinLeave'
au BufLeave qf :echo 'BufLeave'
au BufUnload qf :echo 'unload qf'
" au BufLeave qf noremap <silent> <C-z> :cb<CR>
" au BufLeave quickfix noremap <silent> <C-z> :cb<CR>
" au BufWinLeave quickfix noremap <silent> <C-z> :cb<CR>
" au BufWinLeave quickfix nunmap <C-z>
" au BufWinLeave quickfix :echom 'Hello'<cr>
" BufWinEnter
augroup END
After read reference:
http://vimdoc.sourceforge.net/htmldoc/autocmd.html#BufWinLeave
http://vimdoc.sourceforge.net/htmldoc/autocmd.html#autocmd-patterns
I still could not get unmap events working, i.e. BufWinLeave, BufUnload, BufLeave are not invoked.
Can Vimers tell me which event(s) I should be using and help me out on this? Thank you in advance for the help.
As :help BufWinLeave explains, the current buffer "%" may be different from the buffer being unloaded "". So you need a global autocmd, and resolve the buffer number that has been left, and then check for the quickfix 'buftype':
autocmd! BufWinLeave * if getbufvar(bufnr(expand('<afile>')), '&buftype') ==# 'quickfix' | echo "leaving quickfix" | endif
But in general, I'd advise against such tricks and especially conditional mappings. Your <C-z> / <C-x> mappings are still global, now just depending on whether the quickfix list is visible. That's bad for muscle memory, and the overload of the key combos is mentally taxing. I'd rather get rid of the mappings completely, or assign different (if potentially longer) keys.
And there's the next complication: Vim "distributions" and other people's dotfiles lure you with a quick install and out-of-the-box settings, but you pay the price with increased complexity (you need to understand both Vim's runtime loading scheme and the arbitrary conventions of the distribution) and inflexibility (the distribution may make some things easier, but other things very difficult). Vim is incredibly customizable, using someone else's customization makes no sense.
If you would like to nnoremap these two mappings upon quickfix enters and nunmap after quickfix quits, you could
" map silently upon entering Quickfix
autocmd BufWinEnter * if &buftype == 'quickfix'
\| nnoremap <silent> <C-x> :cn<CR>
\| nnoremap <silent> <C-z> :cp<CR>
\| endif
" unmap upon leaving Quickfix
autocmd BufWinLeave * if &buftype == 'quickfix'
\| nunmap <C-x>
\| nunmap <C-z>
\| endif
Or you can make use of local buffer mapping to make your code shorter
" map silently upon entering Quickfix, and only for Quickfix
autocmd BufWinEnter * if &buftype == 'quickfix'
\| nnoremap <buffer><silent> <C-x> :cn<CR>
\| nnoremap <buffer><silent> <C-z> :cp<CR>
\| endif
These autocmd's are invoked every time you enter or leave a buffer. So it is really better just, as Sato Katsura suggested, add in your ~/.vim/ftplugin/qf.vim
nnoremap <buffer><silent> <C-x> :cn<CR>
nnoremap <buffer><silent> <C-z> :cp<CR>
You may consider to read these:
:h ftplugins
:h map-local
:h buftype
:h line-continuation
By default I have turned off the NERDTree and I use F2 to toggle it.
" autocmd VimEnter * NERDTree
" autocmd VimEnter * wincmd p
nmap <silent> <special> <F2> :NERDTreeToggle<RETURN>
After turning on the NERDTree by pressing F2, the cursor is then focused on the NERDTree window. My question is how to redesign the F2 shortcut so the focus window NOT to the NERDTree window?
This should do it:
nnoremap <silent> <special> <F2> :NERDTreeToggle <Bar> if &filetype ==# 'nerdtree' <Bar> wincmd p <Bar> endif<CR>
After toggling, it checks whether it now is in the NERDTree buffer. If it is, it jumps back to the previous window.
PS: You should use :noremap; it makes the mapping immune to remapping and recursion.
I'm trying this:
inoremap <F2> :!rspec %
But it doesn't work. Can anyone help?
The "i" in inoremap means "insert mode": your mapping is an insert mode mapping so it obviously won't work in normal mode.
Try nnoremap ("n" for "normal mode") instead and add <CR> at the end to actually execute the command:
inoremap <F2> :!rspec %<CR>
Since we are at it, you could also modify it to "write and run":
nnoremap <F2> :update<bar>!rspec %<CR>
Maybe this is a better solution to check the file with rspec (put these lines in to your .vimrc, it needs nocompatible to be set, works in insert mode):
" Open quickfix window after :make if there was errors.
autocmd QuickFixCmdPost * botright cwindow
inoremap <F2> :call Rspec()<CR>
" Check the file with rspec, don't forget to save it before calling.
function Rspec()
let save_makeprg = &makeprg
compiler rspec
let &makeprg = 'rspec "' . expand( '%' ) . '"'
echo expand( &makeprg )
silent make
let &makeprg = save_makeprg
redraw!
endfunction
It will list the errors in quickfix window.