how to disable some features in some specific window in vim? - vim

here is my problem:
i'm using taglist and nerdtree.and set quickfix window displayed no matter if it has context.
and in my vimrc, i set them toggled in a fixed order, so i can get a layout i want.
but when i use C-o, C-i, C-], it will jump to a file, and if i want the features of taglist and nerdtree i should quit it and open it again.
but it will break the layout i want. so i have to quit all and open the file again.
so, is it possible to desable some features in some specific window?
thanks for any help.:)

You can disable certain commands for buffers via :map <buffer>; for sidebar windows like from NERDTree, that's good enough, as they always display the same (scratch) buffer. For example, to disable <C-O> in NERDTree:
:autocmd FileType nerdtree nnoremap <buffer> <C-o> <Nop>
I don't fully understand your question, but another approach (as it is hard to fully control where Vim places new buffer contents) would be to extend your "build your window layout" function from your .vimrc to first clean up any existing NERDTree / TagBar windows, so that you can call it later on (e.g. via a mapping) to "fix up" your layout again.

Related

How to bring back NERDTree tab if I accidentally closed it?

I have a NERDTree setup nicely on my Rails project:
Sometimes, when I do fuzzy finding (Ctrl + P), I would accidentally still be on left tab; when I went to the file, it would replace the nerdtree's left tab. See screenshot below:
Super simple question, but how can I bring up the Nerdtree display back up like the first screnshot?
You should just be able to run :NERDTree again.
Additionally, I have this setting in my .vimrc, to make it less likely I'll delete the NERDTree buffer by accident with I'm compulsively typing :bd
autocmd FileType nerdtree cnoreabbrev <buffer> bd :echo "No you don't"<cr>
Deleting the buffer will permanently put NERDTree in hell (Vim plugins are usually brittle). If you do that, you have to restart Vim.
Also, if you use tabs in Vim, you should get NERDTreeTabs (dead but it works) which keeps NERDTree open / closed / in the same state across all tabs you have open, which is a standard design practice in all editors except Vim. I have a mapping set up to toggle NERDTree:
nnoremap <Leader>nt :NERDTreeTabsToggle<cr>
steal from my my vimrc!
nnoremap <F2> :NERDTreeToggle<CR>
Press F2 twice. The first one will close the NERDTree panel and second one will bring it up again.
F2 can be any key of your choice.

Directly switch to NERDTree window

Consider I have a NERDTree window and more then 2 file view windows are opened in vim. In this case a common way to switch to NERDTree (navigating with several Ctrl+w g/h/j/k) is not very handy as it is not universal for all the open windows.
Is there a way to switch to NERDTree (or any other, may be) window directly?
<C-w>t
should do what you want.
You can make a mapping to do this:
nnoremap <silent> <Leader>t :NERDTreeFocus<CR>
Depending on your window layout, if NERDTree is near the top-left corner, it may have a fixed window number (:echo winnr() will tell you). You can then use [N]<C-w><C-w> to go to window number [N].
If your layout is more dynamic, it might make sense to include the window number in your 'statusline':
:set statusline+=\ %{winnr()}

How to focus a particular window?

I use NERDTree plugin, and I want to create a mapping that focuses NERDTree window and enters search mode (to easily select files, of course). The difficult part here is focusing NERDTree window. I want the mapping to work from any window - even from the NERDTree window itself. So how can I focus that window using vimscript?
I found out that NERDTree's buffer has name "NERD_tree_1" (if only one NERDTree buffer exists, but that's enough for me). Can I somehow use it to focus a window containing that buffer?
use the :NERDTreeFocus command. You can bind it to a key, for example:
noremap <F2> :NERDTreeFocus <BAR> call feedkeys('/') <CR>

Getting VIM Tagbar to launch when opening certain file types

I'd like to have the tagbar VIM plugin launch when I open certain filetypes, so I added the following to my .vimrc:
if has("gui_running")
autocmd BufEnter *.hs nested TagbarOpen
However, this isn't working as I expected. It opens a side window, but the side window displays nothing and my cursor is trapped within it. I cannot switch windows with a click or with the CTRL-W movement commands.
However, when I run TagbarOpen manually, it works just fine.
Anyone else tried this, or is the above the wrong command to issue?
Interesting, that's a side effect of a convenience functionality that I hadn't anticipated. What happens is this: If TagbarOpen is called while the window is already open, Tagbar makes the cursor jump to its window instead of just doing nothing (for convenience like I said). So every time you try to leave the window by switching to the Haskell window, the autocommand causes it to jump right back. I've pushed a change that removes this functionality -- it probably wasn't that useful to begin with. So if you give the development version on GitHub a try (https://github.com/majutsushi/tagbar), it should work.
That the window is empty has a different reason: Haskell is not supported by Exuberant Ctags by default. But someone wrote a nice alternative for Haskell that works with Tagbar here: https://github.com/bitc/lushtags.
I actually have this exact configuration set up my vimrc for php files. Tagbar opens with function / variables loaded, cursor stays in php source file when Vim loads:
autocmd FileType php call SetPHPOptions()
function! SetPHPOptions()
setlocal shiftwidth=4 tabstop=4 softtabstop=4 expandtab makeprg=php-xdebug\ %
:call tagbar#autoopen(0)
endfunction
substitute 'php' for 'hs' or any other file type you want. List of filetypes at:
$VIMRUNTIME/filetype.vim
Have it running on MacVim (snapshot 72, Vim 7.4), and latest build of tagbar from https://github.com/majutsushi/tagbar

vim / NERDtree / folding - can it remember the state of folds?

Is there a way to make NERDtree remember the state of folds when switching from buffer to buffer?
Here the my complete .vimrc:
set ignorecase
set scs
let perl_fold=1
hi Folded cterm=bold ctermfg=yellow ctermbg=lightblue
set modeline
cabbr N NERDTree
Here is what I am observing:
start NERDTree
select a file and use spacebar to open it in a new buffer (all folds are closed)
open some folds in the buffer
C-w w back to NERDTree
select a different file, use spacebar to open it
C-w w back to NERDTree
select the first file, hit spacebar
The folds I had opened originally are now closed.
I am editing perl files,so the perl_fold=1 is in force.
I'd like the state of the folds to be remembered as I bounce around from file to file.
Are you sure about the <Space> mapping? I don't see it listed in NERDTree's help.
Anyway, NERDTree has nothing to do with your buffers content or state, it's only a file explorer.
Without some mechanism to keep state of your buffers your folds are lost when you open a new file. Luckily you can add set hidden to your .vimrc.
With it buffers are kept around until you explicitly delete them with :bd. This means that you still have your folds when going back to your previous file, either by using NERDTree or by using :b <Tab>.
The Vim wiki has nice pages about buffers.

Resources