Automatic syntax/headers in vim for c++ files

2020-03-31 03:05发布

I want that whenever i open a new c++ file in vim in linux ( mandriva 2010 ) the following code gets inserted in it automatically :

Default code :

#include <iostream>

using namespace std;

int main()
{
       return 0;
}

Is there any way to get it done. also can i bind .py, .pl, .java files with similar things. Moreover i should be able to change the default code for a file.

标签: linux vim
3条回答
做自己的国王
2楼-- · 2020-03-31 03:14

For customizable headers, code completion, as well as a host of other features specific to C++, try c.vim

查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-03-31 03:27

One common method for doing this is described at :help template.

Another option would be to use a snippets plugin (like snipMate or UltiSnips). These don't automatically insert the code when you open a new file, but you can create various snippets that will expand to portions of the template you describe and let you fill in the portions that vary (like the header in an #include <...> statement).

查看更多
We Are One
4楼-- · 2020-03-31 03:34

Regarding C&C++, muTemplate goes a step further. When creating a new source file (.cpp, .c, ...), if a header file with the same base name is detected in the vicinity, it is automatically included -- in the case the alternate plugin (a.vim) is installed, its detection heuristic is automatically exploited (in some projects, source files and header files are not in the same directory).

NB: files headers (i.e. copyright/VCS stuff can be overridden)

查看更多
登录 后发表回答