LoginSignup
14
13

More than 5 years have passed since last update.

VimでTextMateのようなコードスニペット(コード補完)を実装する

Last updated at Posted at 2013-11-19

MacのエディタといえばTextMateやSublimetextが有名ですが、コードスニペット(コード補完)をVimでも簡単に再現できます。

snipMate

まずsnipMateのサイトから最新版の「snipMate.zip」を適当な場所(今回は、~/Downloads)にダウンロードします。

次にターミナルで、以下のコマンドを打つと~/.vim/以下にsnipMateが展開されコードスニペット機能が使えるようになります。

$ unzip ~/Downloads/snipMate.zip -d ~/.vim

スニペットの追加

snipMateは最初からいくつかの言語に対応していますが、足りないものは直接編集して追加します。

$ cd ~/.vim/snippets/
$ ls
_.snippets      mako.snippets       snippet.snippets
autoit.snippets     objc.snippets       tcl.snippets
c.snippets      perl.snippets       tex.snippets
cpp.snippets        php.snippets        vim.snippets
html.snippets       python.snippets     zsh.snippets
java.snippets       ruby.snippets
javascript.snippets sh.snippets

PHPの場合は、php.snippetsを編集します。

echoのコードスニペットは「ec」と設定されていますね。

$ vim php.snippets
snippet ec
    echo "${1:string}"${2};

例えば、printのスニペットとして、こんな感じに追記すれば、

snippet pr
    print "${1:string}"${2};

pr<tab>でprintのコードスニペットができるようになります。

スクリーンショット 2013-11-19 12.53.43.png

14
13
0

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
14
13