LoginSignup
1
3

More than 3 years have passed since last update.

21行のシンプルなvimプラグイン管理

Posted at

Vim標準のパッケージマネージャを使いやすくするスニペットです。

特長

  • コマンド:PackGetを実行すると、GitHub上のプラグインをインストールできます。
    • 実行例: :PackGet 'tpope/vim-surround'
    • .vimrcPackGet 'tpope/vim-surround'のように書いておけば、起動時にプラグインを自動インストールしてくれます。
  • パッケージ管理プラグインを導入する手間がありません。
    • dotfilesにvimの設定を書きやすくなります。

スニペット

.vimrc
function! InstallGithubPlugin(id, kind)
    let l:idary = split(a:id, '/')
    let l:local = split(&rtp, ',')[0] . '/pack/github/' . a:kind . '/' . l:idary[1]
    if !isdirectory(l:local)
        call mkdir(l:local, 'p')
        silent exec ':!git clone --depth 1 https://github.com/' . a:id . '.git ' . l:local
        packloadall!
        redraw!
    endif
endfunction
function! UpdatePlugins()
    for d in split(glob(split(&rtp, ',')[0] . '/pack/github/*/*'), '\n')
        silent exec ':!git --git-dir=' . d . '/.git reset --hard HEAD'
        silent exec ':!git --git-dir=' . d . '/.git pull'
    endfor
    packloadall!
    redraw!
endfunction
command! -nargs=1 PackGet     call InstallGithubPlugin(<args>, 'start')
command! -nargs=1 PackGetLazy call InstallGithubPlugin(<args>, 'opt')
command! -nargs=0 PackPullAll call UpdatePlugins()

  • GitHub上にあるプラグインのみ対応しています。
  • Gitのブランチやコミットは指定できません。
  • インストール後に操作が必要なプラグインは、手動でしないといけません。
    • たとえばjunegunn/fzf.vimの場合、インストール後に fzf#install()を手動で実行する必要あり。
  • Ubuntu, Windowsで動作確認してます。

その他の機能

  • コマンド:PackPullAllを実行すると、インストール済みのパッケージを最新にします。
  • プラグインの遅延読み込みは、コマンド:PackGetLazyでできます。
    • 例えば goファイルを開いたときにfatih/vim-goを読み込みたい時など

.vimrc
" 起動時に読み込むプラグインは、以下のように一行で書けます。
PackGet 'tpope/vim-repeat'
PackGet 'tpope/vim-surround'

" 遅延読み込みするプラグインは、以下のように必要に応じて設定してください。
PackGetLazy 'fatih/vim-go'
augroup au_PackGetLazy_go
  autocmd!
  autocmd FileType go packadd vim-go
augroup END
1
3
1

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
1
3