vimでスニペットが使えるように設定

よく使うテキストパターンを一発で出せるようにしたいと考えて
IMEの辞書登録などを調べたけど改行に対応しておらず、
改行も含めて高度な設定ができるように思い切って作業。

準備

vimのpluginを管理するツールを入れる

% git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle

スニペットを使えるようにするプラグインを入れる

% git clone git://github.com/Shougo/neobundle.vim ~/.vim/bundle/neobundle.vim

ついでに、各言語のスニペット集を入れる

% git clone https://github.com/honza/snipmate-snippets.git ~/.vim/bundle/snipmate-snippets

.vimrcに下記を追記
最後の行にあるのがスニペットを設定しているファイルを見に行く部分。
~/.vim/mysnippets
が自分用のファイルを置く所にした。


"--------------------------------------------------------------------------
" neobundle
set nocompatible               " Be iMproved
filetype off                   " Required!

if has('vim_starting')
  set runtimepath+=~/.vim/bundle/neobundle.vim/
  endif

  call neobundle#rc(expand('~/.vim/bundle/'))

  filetype plugin indent on     " Required!

  " Installation check.
  if neobundle#exists_not_installed_bundles()
	  echomsg 'Not installed bundles : ' .
	          \ string(neobundle#get_not_installed_bundle_names())
	  echomsg 'Please execute ":NeoBundleInstall" command.'
	    "finish
endif
NeoBundle 'Shougo/neocomplcache.git'
NeoBundle 'Shougo/neosnippet.git'

" Plugin key-mappings.
imap      (neosnippet_expand_or_jump)
smap      (neosnippet_expand_or_jump)

" SuperTab like snippets behavior.
imap  neosnippet#expandable_or_jumpable() ? "\(neosnippet_expand_or_jump)" : pumvisible() ? "\" : "\"
smap  neosnippet#expandable_or_jumpable() ?"\(neosnippet_expand_or_jump)" : "\"

" For snippet_complete marker.
if has('conceal')
  set conceallevel=2 concealcursor=i
endif
" Tell Neosnippet about the other snippets
let g:neosnippet#snippets_directory='~/.vim/bundle/snipmate-snippets/snippets, ~/.vim/mysnippets'

vimを起動させて、下記のコマンドを打つことでインストールされる

:BundleInstall

自分用の設定ファイル

.vimrcで設定したディレクトリ、
ここでは
~/.vim/mysnippets
の中に設定ファイルを作る。

ここで、

拡張子名.snippets

というファイルを設定すると、その拡張子のファイルをvimで開いた時に
設定したスニペットが適用される仕組み。

例えば、

php.snippets

というファイルを作るとphpファイルに適用される。

しかし、今回実施したかったのはtxtファイルで

txt.snippets

というファイルを作ってもどうしても動作しなかった。
(誰かやり方わかる方いれば教えてください)

しかたがないので

_.snip

というファイルを作成。これは拡張子に関わらず、
全てのファイルに適用される。

スニペットの設定

あとは自分の便利なように設定する。

snippet ahaha
 1gyoume
 2gyoume
 3gyoume

と設定しておけば、vimでahahaと入力した後でCtrl+kと打つと
1gyoume
2gyoume
3gyoume
と出力される。

他にも、かなり高度なことも可能。

snippet hogef
  function hogef {
        ${1:code}
  }

などとすると、入力された上に指定場所にカーソルが合った状態で
インサートモードになっているため
すぐに中身を入力することができる。

応用

.vimrcの中身を変更することでスニペットの適用方法も変えれるみたい。
色々と使ってみながら、自分の使いやすいように設定を変更していく。


Categories:

Tags:


Comments

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA