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.
Related
I have this mapping:
nnoremap <silent> <leader><space> :Files<CR>
This works fine, but when I have a changed file already open, I get an error when I switch to a new file.
Is it possible to save the file before starting fzf?
I tried something like this, but this doesn't work if I don't have a file open. (start vi without file and start fzf)
nnoremap <silent> <leader><space> :w<CR>:Files<CR>
First, use :update instead of :write. This will only write if there are indeed unpersisted changes.
You can check for an empty buffer with empty(bufname('')), but as there are other corner cases, I would rather just silence the error:
nnoremap <silent> <leader><space> :execute 'silent! update'<Bar>Files<CR>
Alternative
Alternatively, you can look into the 'hidden' option, as #romainl mentioned. With this, Vim won't complain if a buffer that has unsaved changes is not shown in a window any longer, and only confront you on quitting Vim. Many power users have :set hidden.
I have added this to my .vimrc in order to append a newline without leaving Normal mode:
nmap <Enter> o<Esc>
The problem is when I q: to the command history buffer. This binding clashes with the usage of Enter for selecting a command from history. A lesser problem is when reading a help page hitting Enter will display a warning that the file is read-only. How can I remap this key in a way that ignores these two contexts?
You can undo the global mapping for the command-line window via a buffer-local mapping to itself:
autocmd CmdwinEnter * nnoremap <buffer> <Enter> <Enter>
(My ingo-library plugin provides a generic ingo#window#cmdwin#UndefineMappingForCmdwin() function for that.)
For the help buffer, use the same approach, but trigger on the FileType event:
autocmd FileType help nnoremap <buffer> <Enter> <Enter>
You can set it with autocmd using this method https://stackoverflow.com/a/10410590/3627387
let blacklist = ['nofile', 'help']
autocmd FileType * if index(blacklist, &bt) < 0 | nmap <Enter> o<Esc>
Here we are checking for buffer type :help 'buftype'
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.
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
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.