I am using quickfix and I have it set up so that a cwindow is run right after a Quick fix command. Also, I have a template file for my C++ source files which I want to be copied at the beginning of my new source files. Here is my .vimrc:
augroup SourceTemplate
autocmd!
autocmd bufnewfile *.c,*.cpp,*.hpp,*.h so /home/username/.vim/template.cpp
augroup QuickFix
autocmd!
" #1 autocmd QuickFixCmdPre * augroup! SourceTemplate
" #2 autocmd! SourceTemplate bufnewfile
autocmd QuickFixCmdPost [^l]* nested cwindow
autocmd QuickFixCmdPost l* nested lwindow
augroup END
My template.cpp file has a :insert in the beginning and a '.' at the end with some text in between that I want to insert in the beginning of a new source file. The two augroups above work well if I use only one of them!
The problem is, whenever I run :make, the quickfix command opens a window at the bottom, as expected, but I am also presented with a new buffer with my template text in it (instead of the file I was editing). On doing Ctrl-G the file name displayed was (in one case) "In file included from...", which was also the highlighted line in the cwindow. So, the bufnewfile event is being triggered for some reason and I need to disable the autocmds corresponding to the bufnewfile in case of quickfix. I was able to think of a couple of solutions to this. But none of them work as I want them to.
If I uncomment #2 above, the SourceTemplate group is deleted forever. It's commands are not even executed on a fresh new file. And if I uncomment #1, the SourceTemplate commands are not disabled at all and the problem persists.
How do I achieve this?