How to map NERDtree's bookmark - vim

Every time I want to bookmark something in NERDtree, I need to type Bookmark <name> in normal mode.
Is there some mapping that could make bookmarking more smoothly?
For example:
when I press <leader><b> in the NERDtree window, the command line shows :Bookmark <name>, all I need to do is fill the <name> and enter.

Try this:
:autocmd Filetype nerdtree nnoremap <buffer> <leader>b :Bookmark
You need to have enabled filetypes using
:filetype plugin indent on
or similar.

Related

vim: Tagbar windows opened when typing <Enter> in normal mode

When typing <Enter> in normal mode in Vim with the Tagbar plugin installed, the Tagbar window is opened automatically. I want to disable this functionality. What must I do?
put the flowing code in you .vimrc
unmap <cr>
Your mapping for <C-m> is actually the cause of the Enter key opening Tagbar. If you remove that map from your vimrc, the enter key will no longer trigger :TagbarToggle.
Mappings for <C-m> and <CR> (Enter) are synonymous in Vim:
The following table shows the mapping between some of the keys on the keyboard and the equivalent Ctrl-key combination:
Ctrl-I Tab
Ctrl-[ Esc
Ctrl-M Enter
Ctrl-H Backspace
If you use one of the Ctrl-key combination in the above table in a map, the map also applies to the corresponding key. Both the keys produce the same key scan code. For example, if you create a map for CTRL-I, then you can invoke the map by pressing Ctrl-I or the Tab key.
This means when you set nmap <C-m> :TagbarToggle<CR>, it is the same as
also setting nmap <CR> :TagbarToggle<CR>.
You'll probably want to choose a new key instead of M. The alternative is to
change the key code sent by <C-m> at the operating system level with some
remapping program.
The terminal key bindings come from readline, the program that processes input text in
your terminal prompt. A full list of key bindings is in the readline
manual.
You can find more info about Vim key codes by typing :help keycodes in Vim, or reading the help docs here.
Try :help tagbar to open the documentation. It sounds like you might have a mapping in your vimrc file that says something like
nnoremap <silent> <CR> :TagbarToggle<CR>
or
nnoremap <silent> <CR> :TagbarOpen<CR>
if you find and remove that mapping will no longer open Tagbar

Switch cursorline in vim on demand

In vim editor, I want to switch cursorline on and off on demand.
I already have this in my .vimrc:
set nocursorline
noremap <F3> :set cursorline! <CR>
But this only works in normal mode. How to change, so the F3 key works in insert mode too?
I don't want to have cursorline at opening a new file directly, so "set nocursorline" is OK for me.
I'll assume by "edit mode" you mean "insert mode". The trick for running normal mode commands from insert mode is to prefix them vith CTRL-O. Try this:
set nocursorline
nnoremap <F3> :set cursorline!<CR>
inoremap <F3> <C-o>:set cursorline!<CR>
map imap cmap noremap ... all do not solve my problem. Generally I want to use a key mapping definition which works in ex mode and edit mode at once.

Is there a way to map plugin specific keys to do multiple keys in Vim?

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.

Toggle show list of buffers in vim

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.

vim quickfix avoid buffer change

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

Resources