I have au BufNewFile *.cpp 0r /Users/<my_username>/.vim/template/cpp.template in my .vimrc which was such that every .cpp file I created would have the template from the cpp.template file. But now for some reason it has stopped working. My .vimrc is short too, so I'm hoping someone could figure that out?

set nocompatible
set cul
filetype off

set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

Plugin 'VundleVim/Vundle.vim'
Plugin 'bling/vim-airline'
Plugin 'vim-syntastic/syntastic'
Plugin 'jiangmiao/auto-pairs'
Plugin 'bogado/file-line'
Plugin 'scrooloose/nerdcommenter'

call vundle#end()

set laststatus=2 "for airline
filetype plugin indent on "all kinds of indentation
syntax enable "syntax higlighting
set mouse=a "mouse as cursor too
set backspace=indent,eol,start "backspacing on empty lines
set nu "number lines
set ignorecase " ignore case when searching
set smartcase " ignore case if search pattern is all lowercase

set expandtab "for use in vim insert mode
set tabstop=4 "tab = 4 columns
set shiftwidth=4 "for use with >> or << operators
set softtabstop=4 "weird stuff

"Moving cursor to other windows with shift up/down/right/left
nnoremap <s-down> <c-w>w
nnoremap <s-up> <c-w>W
nnoremap <s-right> <c-w>h
nnoremap <s-left> <c-w>l

"For the Solarized theme in Vim
set background=dark
colorscheme solarized

"For Syntastic
set statusline+=%#warningmsg#
set statusline+=%{SyntasticStatuslineFlag()}
set statusline+=%*
"For Syntastic again
let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 1
let g:syntastic_check_on_open = 1
let g:syntastic_check_on_wq = 0
let g:syntastic_cpp_compiler = "g++"

"Add any future templates here
au BufNewFile *.cpp 0r /Users/<my_username>/.vim/template/cpp.template

EDIT: Okay I managed to find out that bogado/file-line was the reason the template wasn't working. Any clue of why?


EDIT: I though that it was likely that this plugin incorrectly registers autocommands, as you did or starts with an un-scoped :au !. Indeed :au! clears all autocommands in the context of the current group. If there are none, it'll clear everything in the global unnamed context. As you haven't defined your autocommand in a group, it means it's global, and that unscoped use of :au! will remove it.

However, after reading the plugin code, I indeed see registration of unscoped autocommands, but I see none is cleared. However nested autocommands are used. I don't know why it conflicted with your declaration.

The good practice is to always register autocommands in autocommand groups. This should prevent name conflicts

aug SomeGroupWithAnUniqueName
  au whatever you wish
aug END
|improve this answer|||||
  • Thanks! This worked! If possible could you explain in an easier manner why this worked (I'm quite new to vimscripting)? Also, unrelated question: Is it possible to have a template like I just did above such that every time I open a .cpp file, the cursor is already at a specific point in the template file? Thanks again! – Ind Oubt Nov 30 '16 at 7:48
  • I've expanded a little bit on the subject, but I've no idea why your declaration conflicted with the nested autocommands defined in that plugin. – Luc Hermitte Nov 30 '16 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.