[英]How do I add a vim command generating a class source and header files for C++?
當我從VIM 命令行鍵入:Class Toto
,我想獲取標頭和源文件及其模板,就像我們創建新類時任何編輯器所做的那樣。 因此,如果
輸入:Class Toto
然后
輸出:
toto.h
#ifndef TOTO_H
#define TOTO_H
class toto
{
public:
toto();
virtual ~toto();
protected:
private:
};
#endif // TOTO_H
toto.cpp
#include "toto.h"
toto::toto()
{
//ctor
}
toto::~toto()
{
//dtor
}
我得到:
./src/toto.c
./include/toto.h
自動生成(使用src
和include
文件夾就完美了)
在一個函數下面,我添加到我的~/.vimrc
文件中
"C++ Class Generator
function! Class(ClassName)
"================== editing header file =====================
let header = a:ClassName.".h"
:vsp %:h/.h
call append(0,"#ifndef ".toupper(a:ClassName)."_H")
call append(1,"#define ".toupper(a:ClassName)."_H")
call append(2," ")
call append(3,"class ".a:ClassName )
call append(4, "{")
call append(5, " public:")
call append(6, " ".a:ClassName."();")
call append(7, " virtual ~".a:ClassName."();")
call append(8, " protected:")
call append(9, " private:")
call append(10, "};")
call append(11,"#endif // ".toupper(a:ClassName)."_H")
:execute 'write' header
"================== editing source file ========================
let src = a:ClassName.".cpp"
:vsp %:h/.cpp
call append(0,"#include ".a:ClassName.".h")
call append(1," ")
call append(2,a:ClassName."::".a:ClassName."()")
call append(3,"{")
call append(4,"//ctor ")
call append(5,"}")
call append(6," ")
call append(7," ")
call append(8,a:ClassName."::~".a:ClassName."()")
call append(9,"{")
call append(10,"//dtor ")
call append(11,"}")
:execute 'write' src
endfunction
打開vim並輸入
:call Class("toto")
你的 vim 將被分成 3 部分:
如果你想將命令
:call Class("toto")
為:Class toto
在你的~/.vimrc
添加這一行:
command! -nargs=1 Class call Class(<f-args>)
結果 :
這是賽義夫·法迪(Saif Faidi)作品的細微變化。
它帶來的唯一的東西:
"C++ CLASS GENERATOR: OPENING 2 NEW FILES
function! ClassNew(ClassName)
"================== editing source file =================================
execute "vsp %:h/" . a:ClassName . ".class.cpp"
"At this stage the autocomands for this filetype are done.
" example: inserting the header, and the ifndef... Then:
:execute "normal! a#include \"" . a:ClassName . ".class.hpp\"\<cr>\<cr>"
:execute "normal! a" . a:ClassName . "::" . a:ClassName ."(void)\<cr>{\<cr>"
:execute "normal! a\<tab>return ;\<cr>"
:execute "normal! a}\<cr>\<cr>"
:execute "normal! a" . a:ClassName . "::~" . a:ClassName ."(void)\<cr>{\<cr>"
:execute "normal! a\<tab>return ;\<cr>"
:execute "normal! a}"
"Comment this line if you dont want to save files straight away.
":execute 'write'
"================== editing header file =================================
execute "vsp %:h/" . a:ClassName . ".class.hpp"
"At this stage the autocomands for this filetype are done.
" example: inserting the header, and the ifndef... Then:
:execute "normal! a" . "class " . a:ClassName ."\<cr>{\<cr>"
:execute "normal! a\<tab>public:\<cr>"
:execute "normal! a\<tab>\<tab>" . a:ClassName . "(void);\<cr>"
:execute "normal! a\<tab>\<tab>~" . a:ClassName . "(void);\<cr>\<cr>"
:execute "normal! a\<tab>protected:\<cr>\<cr>"
:execute "normal! a\<tab>private:\<cr>\<cr>"
:execute "normal! a};"
:execute "normal! ka\<tab>\<tab>"
"Comment out this line if you dont want to start in insert mode
:startinsert!
"Comment this line if you dont want to save files straight away.
:execute 'write'
endfunction
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.