Can VIM automatically close NERDTree when deleting a buffer? - vim

Problem:
In VIM, open NERDTree
Select a file and open it in a new buffer (Enter)
Do some work on buffer and save it
Delete buffer with :bd
Now NERDTree takes up the whole window - this isn't a problem per se, but...
Go to next buffer with :bnext
Continue working
Now try opening NERDTree again by hitting Ctrl-N
You see, NERDTree stays "maximised" and you have to resize it.
My attempt at solving this issue:
My approach was what is stated in the title: close NERDTree automatically when deleting a buffer, then reopen it when entering another.
function! g:CloseNERDTree()
if exists("g:NERDTree") && g:NERDTree.IsOpen()
NERDTreeClose
else
echo "already closed"
endif
endfunction
autocmd BufDelete * call CloseNERDTree()
function! g:OpenNERDTree()
if exists("g:NERDTree") && g:NERDTree.IsOpen()
echo "already open"
else
NERDTree
endif
endfunction
autocmd BufEnter * call OpenNERDTree()
This causes VIM to become unusable and my VimScript knowledge isn't enough to know what's going on.

I cannot reproduce your particular problem (for me, NERDTree always opens in a sidebar), but I think the (or one) problem with your approach is with the BufDelete event. :help BufDelete has the following caveat:
Don't change to another buffer, it will cause problems.
I think it would be better to trigger on WinEnter, and use the number of visible windows (winnr('$')) for the check, like this (untested):
function! OnWinEnter()
if winnr('$') == 1
if exists("g:NERDTree") && g:NERDTree.IsOpen()
NERDTreeClose
endif
else
if ! (exists("g:NERDTree") && g:NERDTree.IsOpen())
NERDTreeOpen
endif
endif
endfunction
autocmd WinEnter * call OnWinEnter()

Related

Close vim if no "unhidden" buffers open

Is there any script that closes vim if there are only utility buffers open? I have seen some scrips for NERDTree (Close vim NERDtree on close of file), but none that is more general.
An example, because I am unsure if it is clear what I mean.
Tagbar, Magit and a file are open
I close the file
only utility buffers are open (both buffers are hidden from :ls) -> vim closes
Is there an easy way to do this?
You can use the following script to do what you want:
augroup auto_close_win
autocmd!
autocmd BufEnter * call s:quit_current_win()
augroup END
" Quit Nvim if we have only one window, and its filetype match our pattern.
function! s:quit_current_win() abort
let quit_filetypes = ['qf', 'vista']
let buftype = getbufvar(bufnr(), '&filetype')
if winnr('$') == 1 && index(quit_filetypes, buftype) != -1
quit
endif
endfunction
We use autocmd BufEnter to check if we have only one window left and the filetype of the current buffer is in the blacklist.
Update the list quit_filetypes to include the filetypes you have in mind.

In VIM how to make NERDTree open at startup nicely when giving args

Let me explain my question, what I want to do is:
From the command line calling gvim without arguments, want NERDTree open by default in my /home/user/Documents folder.
From the command line calling gvim . want to open NERDTree with the directory set to the actual directory where the command was executed from. BUT I still want NERDTree on the left and a empty buffer in the right (not NERDTree being the only window just like normally happens).
From the command line calling gvim /some/path/to/folder want to open NERDTree with the directory set to the given directory. BUT I still want NERDTree on the left and a empty buffer in the right (not NERDTree being the only window just like normally happens).
When calling gvim with an argument:
If it is a file, don't open NERDTree just the file.
If it is a directory NERDTree should work as #3
To address #1 I have:
function! StartUp()
if 0 == argc()
NERDTree ~/Documents
endif
endfunction
autocmd VimEnter * call StartUp()
autocmd VimEnter * wincmd p
What I was thinking to address #2 and #3 was:
function! StartUp()
if 0 == argc()
NERDTree ~/Documents
else
if argv(0) == '.'
NERDTree expand(getcwd())
else
NERDTree expand(argv(0))
endif
endif
endfunction
autocmd VimEnter * call StartUp()
autocmd VimEnter * wincmd p
But it doesn't work, it gives me errors and vim freezes some times. What I can do to achieve the desired effect?
Thanks for your help.
Complete solution
Does not work exactly as I expected but it's very very close. So far so god.
function! StartUp()
if 0 == argc()
NERDTree ~/Documents
else
if argv(0) == '.'
execute 'NERDTree' getcwd()
else
execute 'NERDTree' getcwd() . '/' . argv(0)
endif
endif
endfunction
autocmd VimEnter * call StartUp()
autocmd VimEnter * wincmd p
I can't give you a complete solution, but here's a hint that should resolve the errors:
The :NERDTree command takes an (optional) directory; this doesn't resolve expressions. Vim's evaluation rules are different than most programming languages. You need to use :execute in order to evaluate a variable (or expression); otherwise, it's taken literally; i.e. Vim uses the variable name itself as the argument. So change this:
NERDTree expand(getcwd())
into:
execute 'NERDTree' getcwd()
I've also left out the expand(), as getcwd() already returns a full path.

Golang Formatter and Vim - How to destroy history record?

Go (Golang) programming language comes with a tool called go fmt. Its a code formatter, which formats your code automagically (alignments, alphabetic sorting, tabbing, spacing, idioms...). Its really awesome.
So I've found this little autocommand which utilizes it in Vim, each time buffer is saved to file.
au FileType go au BufWritePre <buffer> Fmt
Fmt is a function that comes with Go vim plugin.
This is really great, but it has 1 problem. Each time formatter writes to buffer, it creates a jump in undo/redo history. Which becomes very painful when trying to undo/redo changes, since every 2nd change is formatter (making cursor jump to line 1).
So I am wondering, is there any way to discard latest change from undo/redo history after triggering Fmt?
EDIT:
Ok, so far I have:
au FileType go au BufWritePre <buffer> undojoin | Fmt
But its not all good yet. According to :h undojoin, undojoin is not allowed after undo. And sure enough, it fires an error when I try to :w after an undo.
So how do I achieve something like this pseudo-code:
if lastAction != undo then
au FileType go au BufWritePre <buffer> undojoin | Fmt
end
If I get this last bit figured out, I think I have a solution.
I think this is almost there, accomplishes what you ask, but I see it's deleting one undo point (I think this is expected from undojoin):
function! GoFmt()
try
exe "undojoin"
exe "Fmt"
catch
endtry
endfunction
au FileType go au BufWritePre <buffer> call GoFmt()
EDIT
Based on MattyW answer I recalled another alternative:
au FileType go au BufWritePre <buffer> %!gofmt
:%!<some command> executes a shell command over the buffer, so I do it before writing it to file. But also, it's gonna put the cursor at top of file...
Here is my go at this. It seems to be working well both with read/write autocmds and bound to a key. It puts the cursor back
and doesn't include the top-of-file event in the undos.
function! GoFormatBuffer()
if &modifiable == 1
let l:curw=winsaveview()
let l:tmpname=tempname()
call writefile(getline(1,'$'), l:tmpname)
call system("gofmt " . l:tmpname ." > /dev/null 2>&1")
if v:shell_error == 0
try | silent undojoin | catch | endtry
silent %!gofmt -tabwidth=4
endif
call delete(l:tmpname)
call winrestview(l:curw)
endif
endfunction
I check modifiable because I use vim as my pager.
I attempted to use #pepper_chino's answer but ran into issues where if fmt errors then vim would undo the last change prior to running GoFmt. I worked around this in a long and slightly convoluted way:
" Fmt calls 'go fmt' to convert the file to go's format standards. This being
" run often makes the undo buffer long and difficult to use. This function
" wraps the Fmt function causing it to join the format with the last action.
" This has to have a try/catch since you can't undojoin if the previous
" command was itself an undo.
function! GoFmt()
" Save cursor/view info.
let view = winsaveview()
" Check if Fmt will succeed or not. If it will fail run again to populate location window. If it succeeds then we call it with an undojoin.
" Copy the file to a temp file and attempt to run gofmt on it
let TempFile = tempname()
let SaveModified = &modified
exe 'w ' . TempFile
let &modified = SaveModified
silent exe '! ' . g:gofmt_command . ' ' . TempFile
call delete(TempFile)
if v:shell_error
" Execute Fmt to populate the location window
silent Fmt
else
" Now that we know Fmt will succeed we can now run Fmt with its undo
" joined to the previous edit in the current buffer
try
silent undojoin | silent Fmt
catch
endtry
endif
" Restore the saved cursor/view info.
call winrestview(view)
endfunction
command! GoFmt call GoFmt()
I just have this in my .vimrc:
au BufWritePost *.go !gofmt -w %
Automatically runs gofmt on the file when I save. It doesn't actually reformat it in the buffer so it doesn't interrupt what I'm looking at, but it's correctly formatted on disk so all check ins are properly formatted. If you want to see the correctly formatted code looks like you can just do :e .
Doesn't do anything to my undo/redo history either
You can install the vim plugins from the default repository. Alternatively, a pathogen friendly mirror is here:
https://github.com/jnwhiteh/vim-golang
Then you can use the :Fmt command to safely do a go fmt!

How can I make NERDTree to open on the same drive that the file that I'm editing?

NERDTree shows in viewport disk c: regardless from which disk do I open the file.
When I use gvim in windows I open files using:
gvim.exe --remote-tab-silent [FILE]
I'm loading NERDTree with this line in _vimrc:
au VimEnter * NERDTree
Can NERDTree automaticaly change drive to correct drive somehow?
Actually, my last answer does not work because once the NERDTree have been opened, it does not open again in the new buffer dir. It must work similarly to NERDTreeFind but it does not have a Toggle feature.
I made a function and mapped it to my key and now it works perfectly even opening the Ruby project if you have the vim-rails plugin.
Add this to your vimrc:
function! NTFinderP()
"" Check if NERDTree is open
if exists("t:NERDTreeBufName")
let s:ntree = bufwinnr(t:NERDTreeBufName)
else
let s:ntree = -1
endif
if (s:ntree != -1)
"" If NERDTree is open, close it.
:NERDTreeClose
else
"" Try to open a :Rtree for the rails project
if exists(":Rtree")
"" Open Rtree (using rails plugin, it opens in project dir)
:Rtree
else
"" Open NERDTree in the file path
:NERDTreeFind
endif
endif
endfunction
"" Toggles NERDTree
map <silent> <F1> :call NTFinderP()<CR>
It should work now.
Previous answer below:
You could map the key you use to open
NERDTree like this(in .vimrc):
map <silent> <F1> :NERDTreeToggle %:p:h<CR>
This maps my F1 key to
toggle(open/close) NERDTree using the
path of the currently active buffer.
If no buffer is open, it opens in the
currently launched Macvim directory.
NERDTree provides several Ex commands to manipulate its buffer (see
:help NERDTreeGlobalCommands). Among them there is the :NERDTreeFind
command which behaves the same way as the :NERDTree command except it opens
the NERDTree buffer in the directory containing currently opened file.
So, in order to achieve the desired effect described in the question, you can
simply change the auto-command to read
:autocmd VimEnter * NERDTreeFind
I use mapping for NERDTree and in this way when I open it always opens in current dir
" NERDTree mappings
nnoremap <silent> <F9> :NERDTreeToggle <cr>
inoremap <silent> <F9> <Esc>:NERDTreeToggle <cr>
But if you open a file like gvim ~/other/dir/file NERDTree will open current dir from where gvim was called. So this is not a real solution to your problem.
Perhaps if you cd in working dir before calling gvim will solve your problem. In this case even your au VimEnter * NERDTree in _vimrc must work as you espect .
About changing directory and setting working dir set autochdir read here
Add
au VimEnter,BufWinEnter * NERDTreeFind
to your .vimrc.
VimEnter part makes it work on load.
BufWinEnter makes it happen you open a new file.
* tells it to do this with all files
NERDTreeFind is the command to run
srcs:
http://vimdoc.sourceforge.net/htmldoc/autocmd.html

Automatically quit vim if NERDTree is last and only buffer

I have the following in my .vimrc:
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Open NERDTree by default
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
autocmd VimEnter * NERDTree
autocmd VimEnter * wincmd p
So,
% vim file.txt
opens NERDTree and focuses the cursor in the file.txt buffer. I make my edits, and hit :q on the buffer, and I'm left with . . . NERDTree. This is annoying.
I could use :qa to close all buffers, and exit vim, but I'm used to the :q trope. So I'm wondering if there's a way to detect that the only remaining buffer is NERDTree, and "unify" the two buffers, for purposes of :q
Edit
Ask and ye shall receive: https://github.com/scrooloose/nerdtree/issues#issue/21
A script to do exactly this has been posted on the NERDTree issue list. Checkout issue-21 on GitHub for nerdtree.
This leads to the single line command for your vimrc here:
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
function! s:CloseIfOnlyControlWinLeft()
if winnr("$") != 1
return
endif
if (exists("t:NERDTreeBufName") && bufwinnr(t:NERDTreeBufName) != -1)
\ || &buftype == 'quickfix'
q
endif
endfunction
augroup CloseIfOnlyControlWinLeft
au!
au BufEnter * call s:CloseIfOnlyControlWinLeft()
augroup END
From my vimrc, based on a version from janus repo.
Enhancements: also close if only a quickfix window is left.
It uses the BufEnter autocommand instead, which is required for &bt to work properly.
An idea in need of implementation:
You could write a function which, when called, checks if the only buffer remaining (or perhaps the only non-help buffer, if you prefer) is a NERDTree buffer and, if so, deletes it (or just quits).
Then have an autocmd run it whenever a buffer is deleted / hidden / whatever actually happens when you :q (it shames me to admit I'm not entirely sure!).
You could :cabbrv q qa but I'd advise against that because you'll forget about it when you actually want q.
I like to do this: cmap bq :bufdo q<CR> to close all buffers with two keystrokes in command mode.

Resources