I'm using vimgrep with cw to open the search results in the quickfix buffer. Every time I select a line in the quickFix list the cursor shifts to the new file buffer. How do I avoid this and keep my cursor in the quickFix list always?
Assuming you are using <CR> to "select a line in the quickFix list", you can simply remap <CR> in the quickfix window so that you come back to the previous window after jumping to the matching line.
Make sure you have filetype plugin indent on and add the line below to ~/.vim/ftplugin/qf.vim:
nnoremap <buffer> <CR> <CR><C-w>p
You can use :cnext and :cprevious combined with a <C-w>p (focus on previous window).
To bind them to e.g. <Leader>cn and <Leader>cp, you could do:
nnoremap <silent> <Leader>cn :cnext<CR><C-w>p
nnoremap <silent> <Leader>cp :cprevious<CR><C-w>p
Related
I'd like to map NERDTree's tab opening key, 't', to do multiple things. Namely, I'd like it to open the tab then do the following list of commands: TlistToggle Ctrl W, Ctrl T, Ctrl W, Shift K, 30, Ctrl W, minus-sign. So that I open the taglist for the file, then horizontally split the list and the file, then resize the tag list.
I've tried the following:
nnoremap <t> NERDTree-t TlistToggle <C-W><C-T><C-W><S-K>30<C-W> -
but this doesn't seem to do anything.
Thoughts? Am I just completely doing this wrong. Is this even possible?
The NERDTree mapping is not a global one, but only exists (and makes sense) in the plugin's sidebar. That makes it more difficult to override, but you can hook into NERDTree setting its 'filetype', and then define a buffer-local mapping to override NERDTree's:
:autocmd FileType nerdtree nnoremap t ...
While normal mode commands (like the <C-w>... stuff) can indeed be concatenated, that's not true for the plugin invocations. You can find out NERDTree's via :nmap <buffer> t:
:call nerdtree#invokeKeyMap("t")
The Taglist's is also an Ex command; you can combine both with | (written as <Bar> in mappings):
:call nerdtree#invokeKeyMap("t")<Bar>TlistToggle<CR>
So, something like this should work (I didn't test it):
:autocmd FileType nerdtree nnoremap <buffer> t :call nerdtree#invokeKeyMap("t")<Bar>TlistToggle<CR><C-W><C-T><C-W>K30<C-W>-
Here is the final solution I used to open a file in new tab from NERDTree and then split and resize the file & TlistToggle:
autocmd FileType nerdtree nnoremap <buffer> t :call nerdtree#ui_glue#invokeKeyMap("t")<CR> :TlistToggle<CR> <C-w><C-t><C-w>K :exe "resize " . ((winheight(0) + winheight(1)) * 3/20)<CR>
And this resizes the tag list that was opened in a horizontal tab by 15% of the total amount of lines in the whole window.
There is this cool tip in Vim wikia to work with buffers:
nnoremap <F5> :buffers<CR>:buffer<Space>
How can I turn it into a toggle? That is, first press of <F5> shows the list of buffers, press it again, and it closes the list of buffers (for me it's more intuitive than pressing ESC) Also, I would like to do the same for :Hex with, say <F2>, that is, press <F2>, opens file explorer in a split, repress <F2>, closes file explorer.
I do not know what you mean by toggling buffer list. You can dismiss the command line via <esc>. I guess you could do cnoremap <f5> <esc>. This works because pressing <f5> in normal mode brings up the buffer list and keeps you in command-line mode. You map <f5> in command-line mode to <esc> to dismiss the prompt.
:Hex opens a split an netrw explorer buffer. You can simply close the buffer any number of ways: <c-w>c, :q, :close, and so on. To make an <f2> mapping that closes a netrw buffer you can add the following to you ~/.vimrc file:
augroup ToggleNetrw
autocmd!
autocmd FileType netrw nnoremap <buffer> <f2> :<c-u>close<cr>
augroup END
Your <f2> mapping does :Hex and we map a buffer specific <f2> mapping for netrw filetypes to close the buffer via :close.
:Lexplore will toggle the explorer in the current buffer's directory.
You can map to something like:
nnoremap <F2> :Lex<CR>
alternatively you can give a size for the opening netrw:
nnoremap <F2> :20Lex<CR>
In the above example, 20 is the percentage drawn from the current buffer size.
More information can be found by the vim command: :h Lexplore among other resources.
------- EDIT about toggling buffer list.
You can toggle the buffer list after executing the mapping you have by pressing the tab key, if that's what you mean.
I like highlighting while searching in vim. Here's what I want:
I search for a word with /
Then, all of the results are highlighted. If I press any key other than n or N, I want the highlighting to be toggled off.
If I press n or N again after any number of commands, I want to toggle on the highlighting.
Where do I start? I'm not even sure what to google.
I have this in my .vimrc
nnoremap <CR> :noh<CR>
so that when I'm done seeing the highlighting, I just hit enter to remove it. It stays gone until I hit n or N again.
Note: If you want to keep the functionality of enter, add another <CR> on the end of the command.
I remap control-l (lower case L) so that it clears the search result as well as repaints the screen. This line in .vimrc does it:
nnoremap <silent> <C-l> :nohl<CR><C-l>
You can manually disable the last highlight with nohl.
I will let you know if I can figure out how to automate this.
One method is to setup a toggle mapping. These are some toggle mappings I have in my .vimrc:
let mapleader="\\"
noremap <silent> <Leader>th :set invhls hls ?<CR>
noremap <silent> <Leader>tn :set invnumber number ?<CR>
noremap <silent> <Leader>ts :set invspell spell ?<CR>
noremap <silent> <Leader>tw :set invwrap wrap ?<CR>
To toggle highlighting just type \th for toggle hls. The others are line number, spell checking, line wrapping. The final hls ? will display the new mode.
I prefer this, because to me it is nothing but natural.
Start searching with /<pattern> and once done, simply type <Leader>/ to stop the highlighting.
nnoremap <silent> <Leader>/ :set nohl<CR>
Right now I am using:
nnoremap <cr> :nohlsearch<cr><cr>k
But after I press Enter my cursor goes to beginning of line.
I have additional <cr>k because I want to use default <Enter> behaviour for example when I try to open file in Ack results quickview.
If you want Enter to keep the default behavior, then this mapping should take care of it.
nnoremap <CR> :nohlsearch<CR><CR>
It turns off search highlighting, moves the cursor down, and only applies to normal mode.
An alternative approach is to locally override the new behavior in the quickfix window and the command-line window, where <CR> has special meaning:
:nnoremap <CR> :nohlsearch<CR>
:autocmd BufReadPost quickfix nnoremap <buffer> <CR> <CR>
:autocmd CmdwinEnter * nnoremap <buffer> <CR> <CR>
I am a long time emacs user learning Vim. Emacs lets me navigate in the mini-buffer (where I issue commands like C-x C-s) using the same navigation keyboard shortcuts as in any other buffer. For example, I can navigate forward one character using C-f, even while in the mini-buffer. I could also use the arrow keys, but they are too far away.
Is there any keyboard shortcut to navigate in Vim's command mode (:), without using the arrow keys -- equivalent to emacs C-f, C-b? Thanks.
Adding to Greg Hewgill's answer, you can use q: to open the command-line window, where you have any Vim editing power at your hand.
Some from the Vim help:
CTRL-B or <Home>
cursor to beginning of command-line
CTRL-E or <End>
cursor to end of command-line
CTRL-H
<BS> Delete the character in front of the cursor (see |:fixdel| if
your <BS> key does not do what you want).
<Del> Delete the character under the cursor (at end of line:
character before the cursor).
CTRL-W Delete the |word| before the cursor. This depends on the
'iskeyword' option.
CTRL-U Remove all characters between the cursor position and
the beginning of the line.
I have these in my .vimrc
cnoremap <C-a> <Home>
cnoremap <C-e> <End>
cnoremap <C-p> <Up>
cnoremap <C-n> <Down>
cnoremap <C-b> <Left>
cnoremap <C-f> <Right>
cnoremap <M-b> <S-Left>
cnoremap <M-f> <S-Right>
With the default key bindings, vim does not offer non-arrow-key navigation of the command line editing. However, see :help cmdline-editing for an example of how to use the :cnoremap command to set up alternate key bindings.
I achieved that with <C-p> and <C-n> to navigate previous and next commands respectively.
P.S I'm not making any custom binding like Tassos did.