gvim popup menu item only in Nerdtree window - vim

I have a following menu item
nnoremenu 1.120 PopUp.Add\ &To\ &&Path :call NERDTreeAddNodeToPath()<CR>
but I want it to show only in Nerdtree window, how do I do it?

I think I've cracked this, but it's not as pretty as I had hoped. I have written a function NERDTreeMenuToggle() which adds the line to the popup menu if the current buffer is filetype "nerdtree", otherwise the line is removed from the menu:
function! NERDTreeMenuToggle()
if &ft == "nerdtree"
nnoremenu 1.120 PopUp.Add\ To\ Path :call NERDTreeAddNodeToPath()<CR>
else
silent! nunmenu PopUp.Add\ To\ Path
endif
endfunction
Note that nunmenu must be called silently to suppress errors if the menu item doesn't exist. Next we must set this function to be called on the MenuPopup event (i.e. just before the menu is displaued).
au! MenuPopup * :call NERDTreeMenuToggle()
That seems to do the trick (although I am not a regular user of NERDTree, so I may be missing some subtleties.
Another approach would be to add the line to menu in a new filetype plugin (just put the nnoremenu line in a file called nerdtree.vim in $VIM/vimfiles/ftplugin) but this still leaves the problem of removing the line for non-NERDTree buffers, so I think my solution above is slightly cleaner.

Related

vimrc: how to auto change window focus when vim is loaded

I've let tagbar and NERDtree autoload when vim is opening any file.
The window layout from left to right is:
NERDTree----My source code----TagBar
The problem is, each time vim is up, the NERDtree on the left gets the focus(keyboard). I wish to make the middle window(My source code) having the focus, so I can start coding immediately. Or else I have to C-w l to switch windows each time.
How to set this in ~/.vimrc?
Thanks a lot.
The plugins probably use :autocmd VimEnter to open their sidebars. You can define a similar autocmd, but it has to run after the plugins'.
Put the following into ~/.vim/after/plugin/middleWindow.vim:
autocmd VimEnter * 2wincmd w
This goes to the second available window.
It's a bit tricky, because I don't know when in your start-up sequence, all three buffers are loaded. If you use Vim8, you can do the wincmd on a timer. This works for me:
call timer_start(100, { -> execute( "wincmd l") })

Vim command Scroll Up and down - Does a plugin exist for this?

I have been using vim for a couple of months now however my current workflow involves continuously scrolling down and up. For that i use Ctrl+f and Ctrl+b
So whenever I use the Ctrl+f combination the new text appears on the center of the screen. I wanted to know if there was a way for vim to always have a light coloured line in the center of screen (so i could use the line as a reference to the new content whenever i press ctrl+f (scroll half screen)). I know this request sounds odd but I am curious if such a feature exists?
You can try this smooth scroll plugin: https://github.com/terryma/vim-smooth-scroll I think it may solve your problem.
Put this in your .vimrc or in a script:
augroup HlMid
autocmd!
autocmd CursorMoved * :call HighlightMiddle()
augroup END
function! HighlightMiddle()
normal! M
execute "match search /\\%".line('.')."l/"
normal! ^O
endfunction!
EDIT: As rgoliveira stated in the comment that could interfere with match command. You need only to remove the autocommand and make it back whenever you use it:
Remove:
:autocmd! HlMid CursorMoved *
Reload:
:autocmd HlMid CursorMoved * :call HighlightMiddle()
For the last command in the function normal! ^o you get ^o by typing ctrl+v ctrl+o

How to delete a buffer when the window is closed?

When I have a new temporary buffer/window and I just do :q, it quits the window but do not clean the buffer. When I quit Vim, it will always popup and tell me there is No write since last change for buffer [No Name].
I have the option for hidden and bufhidden
set hidden
set bufhidden=wipe
It closes the window without warning, but still popup when closing the whole Vim program.
I tried to add an autocmd:
au BufLeave * bw
It works when I quit a window, but will clean the buffer when I want to just open a new window/tab (as it does not distinguish switch window and close window). I also tried BufWinLeave and WinLeave, I did not achieve what I need.
I came up with something like:
function! OnBufHidden()
if expand("<afile>") == ""
execute ":bw! " . expand("<abuf>")
endif
endf
set hidden
autocmd BufHidden * call OnBufHidden()
It should work but it does not. The execute is executed because I tried with some echo inside, but not sure why bw! is not executed.
You can create a scratch buffer in a vertical scratch window with this command:
command! SC vnew | setlocal nobuflisted buftype=nofile bufhidden=wipe noswapfile
With :set bufhidden=wipe, the buffer's contents are lost without confirmation, but only if it is hidden (e.g. via :hide) first; :set hidden doesn't do this, it just enables hiding. If you use :q and the buffer is still visible, you'll get the confirmation.
To get what you want, also :setlocal buftype=nofile (as in #romainl's answer). Then, you'll never get a confirmation.

VIM: Open separate help/hint/text file automatically in a vsplit

I have the following problem, or lets say idea, with vim. When I am writing latex documents I want automatically open a file ~/.vim/latex_hints, where I collected some hints, shortcuts, workarounds,..., in vsplit on the right side. The hint file should be loaded read only and automatically closing when I close the latex document.
After a few experiments I added the following commands to my vimrc:
function Handletexfile()
setlocal cc=80
setlocal wrap
setlocal textwidth=80
belowright vsplit +setl\ ro\ nomodifiable ~/.vim/latex_hints
endfunction
autocmd BufRead,BufNewFile *.tex call Handletexfile()
and
function Handletexfileexit()
let tablist = []
call extend(tablist, tabpagebuflist(tabpagenr()))
for b in tablist
echo b . " ". bufname(b)
if bufname(b) =~ "vim/.*_hints"
echo "Close buffer..". b
execute "bdelete! ".b
endif
endfor
endfunction
autocmd BufWinLeave *.* call Handletexfileexit()
When I open a tex file, my hint file is displayed on the right side as read only and not modifiable. But when I close using :q or :wq the buffers open in the current tab are listed and the one matching to the hint file is selected by the if statement. But I get the following output
1 abstract.tex
2 ~/.vim/latex_hints
Close buffer..2
and my vim crashes with an segfault.
The first part of your requirement translates pretty straightforward into Vimscript:
autocmd BufWinEnter <buffer> belowright vsplit +setl\ ro ~/.vim/latex_hints
Put this into ~/.vim/after/ftplugin/tex.vim, or prepend :autocmd FileType tex to the above command.
The latter part is more complex; on BufWinLeave, you'd have to check all other windows for the opened cheat file with bufwinnr(), go to it (:wincmd w), and :close it.
As an alternative use the preview window with your hint file. The preview window gives you some advantages:
It is small and out of the way
Can close it easily via <c-w>z from any other window
Can jump to the preview window from any window easily via <c-w>P (jump back via <c-w>p)
You can do this via the :pedit command. For example:
autocmd BufWinEnter <buffer> pedit +setl\ ro ~/.vim/latex_hints
Personally I feel like it would be better to create command or mapping to open your hints files as you may eventually out grow the file as time goes on. I would also set the 'bufhidden' to wipe and un-list the buffer with 'nobuflisted'.
You may also want to look into getting a nice snippet plugin.

Quickfix ftplugin inclusion guard

For some reason the inclusion guard for quickfix filetype plugin doesn't works when its contents changes.
Inserting the following contents on ~/.vim/ftplugin/qf.vim
if exists("b:did_ftplugin")
finish
endif
call input("qf.vim!")
the message from the input() can be seem after issuing :copen.
But despite the include guard was set from the default filetype plugin on $VIMRUMTIME, as shown by :echo b:did_ftplugin and :1verbose setlocal stl, issuing :copen, :cold, :cnew or :helpg helpg also causes the message to be displayed.
This happens without additional plugins and no settings other than filetype plugin indent on
and set nocompatible.
My first guess was that any command refreshing the quickfix window closes the existing buffer and opens a new one. But including the let b:did_ftplugin = 1 after the endif above avoids the filetype plugin reload, as no message is displayed after the first :copen (but the default filetype plugin is completely skipped, as 'stl' isn't set anymore).
What could be the difference on between the b:did_ftplugin set on default filetype plugin and the one set from my home dir?
The quickfix window is mainly a view (window), though (for implementation reasons) it is backed by a Vim buffer. When I :cclose a quickfix window, and then :copen it again, the :ls! command shows an incremented buffer number. I think that explains the behavior you're seeing.

Resources