4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

カレントファイルにインクルードガードを書き込むvimスクリプト

4
Last updated at Posted at 2014-02-18

概要

C言語の場合,ヘッダーファイルにインクルードガードを書くと思います.

hoge.h
# ifndef HOGE_H_INCLUDED_
# define HOGE_H_INCLUDED_

void hoge(void);


# endif  //HOGE_H_INCLUDED_

いちいち書くのが面倒なのでカレントファイルに書き込んでくれるスクリプトを書きました.

スクリプト

.vimrcに追記.特に説明はいらないと思います.

.vimrc
"----------------------------------------------------
" Insert include guard to the current file
"----------------------------------------------------
command!  -nargs=0 IncGuard call IncludeGuard()
function! IncludeGuard()
    "カレントファイル名を取得
    let name = fnamemodify(expand('%'),':t')

    "大文字にする
    let name = toupper(name)

    "がーど
    let included = substitute(name,'\.','_','g').'_INCLUDED__'

    "書き込み
    let res_head = '#ifndef '.included."\n#define ".included."\n\n"
    let res_foot = "\n".'#endif //'.included."\n"
    silent! execute '1s/^/\=res_head'
    silent! execute '$s/$/\=res_foot'
endfunction

最後に

本当はファイルの先頭,末尾に書きたかったけどやり方がわからなかったです.
gg,Gで移動してから書けばいいのかもしれないけどあんまり美しくないし.

↑修正しました.

Qiitaってvimスクリプトはsyntax ハイライトしてくれないんですね・・・

↑修正しました.

ご指摘いただいた @uasi さん,ありがとうございました.

4
4
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?