Track Active Item in NERDTree - vim

NERDTree has :NERDTreeFind to find the current file in the tree.
How can I make NERDTree update the current file in tree automatically whenever I switch files?

This should do it, but be aware that the frequent updates make switching buffers quite slow:
:autocmd BufEnter * if &filetype !=# 'nerdtree' | NERDTreeFind | wincmd p | endif
The conditional avoids triggering a find if the NERDTree sidebar itself is entered.

Related

Quickfix Location List linter window stays open

I love the linters and the quickfix window, but on occasion I don't want to fix anything and simply want to exit the file. The current issue is that when I close the file, the quickfix window stays open. I know I can use :qa to exit both at the same time, but I often forget to.
I spent a few hours trying to figure out how to close the quickfix buffer if it was the only open window/buffer left, but had no luck.
Anyone else know how to handle this better?
That feature is part of my vim-qf plugin but you don't really need a full-fledged plugin for such a mundane task. Actually, there have been many snippets floating around the internet for many years. Here is one:
augroup MyGroup
autocmd!
if exists('##QuitPre')
autocmd QuitPre * if &filetype != 'qf' | silent! lclose | endif
endif
augroup END
Basically, it closes the current quickfix/location window if it is the last remaining window in the current tab page.
Reference:
:help :augroup
:help :autocmd
:help exists()
:help QuitPre
if version >= 700
" automatically close quickfix if it's the only window left
autocmd WinEnter * if winnr('$') == 1 && &buftype == "quickfix" | quit | endif
endif

How can I listen to a VIM event to run a command AFTER a buffer has been switched to?

When you open macvim with no files it shows a Nerdtree buffer. When you select a file, Nerdtree closes and the new file takes over the buffer. If you open Nerdtree, it shows the root. This only happens the first time.
I want Nerdtree to stay open and reflect the current file (what :NERDTreeFind does).
I have it mostly working:
autocmd BufWinEnter * call s:openAndFindNerdtree()
function! s:openAndFindNerdtree()
" This will only be false, until you open Nerdtree the 2nd time in this scenario
if exists("t:NERDTreeBufName") == 0
echo "Find my nerdtree"
" NERDTreeFind
endif
endfunction
Here's my problem:
Right now the BufWinEnter event gets called while NERDTree still has focus (before the buffer has been switched).
My question:
What event can I use that will fire AFTER the buffer has been switched (focused)?

How to setup an autocommand for the preview window's buffer?

How do I setup an autocommand to trigger for the preview window's buffer solely? I have one that works on the quickfix window because its filetype is qf, so I can setup a FileType autocommand for it. I've checked quickfix window's buffer FileType but it's empty.
There's no explicit event, but you can easily check for the preview window via the 'previewwindow' option, and combine that with another event, e.g. WinEnter:
:autocmd WinEnter * if &previewwindow | ... | endif
The currently accepted answer didn't solve the problem for me because it only triggered when the preview window was entered, not when it was created.
Thanks to this vim tip, I found that BufWinEnter does fire on preview window creation.
autocmd! BufWinEnter * if &previewwindow | call s:previewWinSettings() | endif
The only times it won't fire are if using :pedit on an already-loaded buffer.

detect, if ctrlp is opened/active

I like the relativenumbers, but they turn out to be slow if I move through files with very long lines. So I added
autocmd CursorMoved,CursorMovedI * if &relativenumber | set norelativenumber | endif
autocmd CursorHold,CursorHoldI * set relativenumber
set updatetime=500
to my vimrc. It disables relative numbers during cursor movement and enables them afterwards again. However, this also enables them in ctrlp. Especially since ctrlp disables number, this causes columns to jump as soon as I cursor. Is there a way to detect, if I'm currently in the curlp file browser?
The CtrlP scratch buffer probably has a distinct name (if bufname('') =~ 'ctrlp')) or filetype (if &filetype ==# 'ctrlp') that you can use in a condition on a similar autocmd BufEnter.
(Note: I don't use the plugin, so the above names are just for illustration.)

Vim put cursor in the middle of screen after buffer switch

When I switch buffer and then go back to it (when I get back to any buffer that was previously opened), cursor is placed in the middle of the screen, loosing previous screen position (e.g. cursor at top of the screen). Maybe this is the normal behaviour of vim, but is there any way to fix this?
This problem happens when I use :bn and bp: to switch buffers. However this behaviour does not happen when switching between tabs which is really strange.
See http://vim.wikia.com/wiki/Avoid_scrolling_when_switch_buffers
" When switching buffers, preserve window view.
if v:version >= 700
au BufLeave * if !&diff | let b:winview = winsaveview() | endif
au BufEnter * if exists('b:winview') && !&diff | call winrestview(b:winview) | endif
endif

Resources