默认文本
我想要一些默认的文本,如:默认文本
#include <iostream>
using namespace std;
int main()
{
// code
return 0;
}
来,每当我打开一个.cpp
文件中的Vim或Atom。我在各种论坛上搜索了很多,但除了Visual Studio之外找不到答案,但是不能找到Vim或Atom的答案。
Vim有多个事件,其中如果创建一个新的文件或文件被打开,我们可以发现:
if has("autocmd")
au BufNewFile,BufRead *.cpp "Do something
endif
我们要检查我们打开的文件是否为空检查,如果一个文件中有一个文件系统位置,让我们把这个功能,以便更容易阅读:
function! EmptyFile()
if (line('$') == 1 && getline(1) == '')
"Do something
endif
endfunction
我们想要做的最后一件事是插入根据现有文件中的一些模板:
function! LoadTemplate(template)
let fl = readfile(template)
call append('', fl)
endfunction
现在一切都在一起(你应该把它添加到您的.vimrc
):
function! AddEmptyTemplate(template)
if (line('$') == 1 && getline(1) == '')
let fl = readfile(a:template)
call append('', fl)
endif
endfunction
if has("autocmd")
au BufNewFile,BufRead *.cpp call AddEmptyTemplate("cpp-template.cpp")
au BufNewFile,BufRead *.c call AddEmptyTemplate("c-template.c")
endif
这里假设你有在文件夹中的文件Vim可以找到。
谢谢,这有很大的帮助。 –
虽然我遇到了一个问题....如果我把CPP模板放在某个文件夹(比如说家),那么我无法访问那个模板打开其他文件夹中的.cpp文件,除非我复制粘贴相同该文件夹中的模板 –
用'BufRead,BufNewFile'试一试。每当你切换缓冲区时调用BufEnter,这绝对不是你想要的。 –
您可能想阅读[为什么我不应该#include?](http://*.com/questions/31816095/why-should-i-not-include-bits-stdc -h) –
@波佩尔松..谢谢你的链接....更新 –