Reputation: 1397
My ~/.vimrc contains only
so ~/config/vim/vimrc
~/config/vim/vimrc contains usual options, few mappings and source files for different filetype, I've got :
autocmd FileType cpp so ~/config/vim/filetype/cpp.vimrc
And in that file, I have defined the following function, which I want to call every time I open a new cpp header in order to avoid double inclusion :
python import vim
function! s:insert_gates()
python << endPython
hpp = vim.current.buffer.name
hpp = hpp[hpp.rfind('/') + 1:]
hpp = hpp.upper()
hpp = hpp.replace('.', '_')
vim.current.buffer.append("#ifndef " + hpp)
vim.current.buffer.append("# define " + hpp)
vim.current.buffer.append("")
vim.current.buffer.append("#endif")
endPython
endfunction
autocmd BufNewFile *.hpp call <SID>insert_gates()
And then, if I ask my shell for:
vim -O3 t1.hpp t2.hpp t3.hpp
I got:
| |#ifndef T2_HPP |#ifndef T3_HPP |
| |# define T2_HPP |# define T3_HPP |
| | | |
| |#endif |#endif |
| | |#ifndef T3_HPP |
| | |# define T3_HPP |
| | | |
| | |#endif |
| | | |
|_____________________|_____________________|_____________________|
|t1.h |t2.h |t3.h |
That's not exactly what I want... Do you see my mistake ? Thanks.
Upvotes: 2
Views: 925
Reputation: 2266
As referenced here, Vim creates a new autocmd
each time you open a new file. To prevent this, replace that section of your .vimrc
with:
python import vim
function! s:insert_gates()
python << endPython
hpp = vim.current.buffer.name
hpp = hpp[hpp.rfind('/') + 1:]
hpp = hpp.upper()
hpp = hpp.replace('.', '_')
vim.current.buffer.append("#ifndef " + hpp)
vim.current.buffer.append("# define " + hpp)
vim.current.buffer.append("")
vim.current.buffer.append("#endif")
endPython
endfunction
augroup insertgates
autocmd!
autocmd BufNewFile *.hpp call <SID>insert_gates()
augroup END
Upvotes: 2