Run vim command when preview window is open - vim

In neovim, when my completion plugin is triggered, it will print out the methods signature in a preview window
The preview window though, contains line numbers which is something I want to disable. So far, I though this would work
function! Preview_func()
if &pvw
setlocal nonum
endif
endfunction
autocmd * BufCreate call Preview_func()
But no dice. Any ideas?

In autocommand definition file pattern ( here: * ) should come after event ( BufCreate ); also base on which autocompletion plugin you are using there may be an entrance to preview window or not so also check with WinEnter event:
autocmd WinEnter * call Preview_func()

Related

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.

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.

Highlight Normal only for filetype in vim

So, I'm pretty happy that I've discovered how to set highlighting when I open a particular file in vim. I'm doing this for JavaScript files, so most of them are 'hi jsStringD' and 'hi jsFunction' etc.
I use this to trigger it:
if has("autocmd")
augroup JavaScript
au!
au BufReadPost *.js call SetUpJavaScript()
augroup END
...
I also want to change the color of 'Normal' or foreground text, but only for JavaScript files.
This works to set the color:
execute 'hi Normal ctermfg=' normalText
But how do I limit it only to a certain filetype? This results in all files having 'Normal' highlighted to that color.
EDIT: Just to clarify, I open a JavaScript file and it works. I open another file (say a .jade file) and 'Normal' color remains the 'normalText' value.
Use an autocmd that only fires for the javascript file type
autocmd FileType javascript execute 'hi Normal ctermfg=' normalText
However I think this will have problems if you switch between multiple files in one session since highlighting is global.
To work around this you could add another FileType autocmd that fires on BufEnter (or something like that) that sets a default Normal highlighting.
Something like this would work (Although it will change the highlighting in splits when you change focus)
autocmd BufEnter * if (&filetype ==# 'javascript') | execute 'hi Search ctermfg=' . normalText | else | execute 'hi Search ctermfg=' . defualtText | endif

sourcing vimrc when opening a file in tab page?

I have a the following in my vimrc to highlight any lines that go past 80 columns:
highlight ColorColumn ctermfg=red ctermbg=bg
call matchadd('ColorColumn', '\%81v.\+', 100)
It works great in most cases. However, I've noticed that if i open a file in a new tab, it doesn't work at all. I'm able to fix this by :source $MYVIMRC. But the issue is, when I source my vimrc I lose my indentLines plugin. I've done a little testing, and I have found that the indentLines goes away anytime the vimrc is sourced in an open instance of vim. Still, I am unable to determine why the 2 lines shown above are not being called when I open a file in a new tab. Any ideas?
my vimrc
The matchadd() only affects the current window. In order to have it on all windows you can add the following to your .vimrc:
if exists("*matchadd")
augroup colorColumn
au!
au BufEnter * call matchadd('ColorColumn', '\%81v.\+', 100)
augroup END
endif
Edit: As pointed by Ingo on the comments, the BufEnter will trigger many times when it is not necessary. The lines below correct this issue:
if exists("*matchadd")
augroup colorColumn
au!
au VimEnter,WinEnter * call matchadd('ColorColumn', '\%81v.\+', 100)
augroup END
endif

gvim popup menu item only in Nerdtree window

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.

Resources