LoginSignup
26
35

More than 5 years have passed since last update.

dein.vimを導入してjedi-vimを入れる

Last updated at Posted at 2017-09-03

今まで素の Vim だったがようやくプラグイン管理を導入した。時のメモ

dein.vim

https://github.com/Shougo/dein.vim
NeoBundle はバグフィックスのみの対応らしいので dein.vim を使う。

インストール

$ curl https://raw.githubusercontent.com/Shougo/dein.vim/master/bin/installer.sh > installer.sh
$ sh ./installer.sh {インストールディレクトリ}

installer.sh を実行すると以下のコードが表示されるので vimrc 追記すればOK

Please add the following settings for dein to the top of your vimrc (Vim) or init.vim (NeoVim) file:


"dein Scripts-----------------------------
if &compatible
  set nocompatible               " Be iMproved
endif

" Required:
set runtimepath+=~/.vim/dein/repos/github.com/Shougo/dein.vim

" Required:
if dein#load_state('~/.vim/dein/')
  call dein#begin('~/.vim/dein/')

  " Let dein manage dein
  " Required:
  call dein#add('~/.vim/dein/repos/github.com/Shougo/dein.vim')

  " Add or remove your plugins here:
  call dein#add('Shougo/neosnippet.vim')
  call dein#add('Shougo/neosnippet-snippets')

  " You can specify revision/branch/tag.
  call dein#add('Shougo/vimshell', { 'rev': '3787e5' })

  " Required:
  call dein#end()
  call dein#save_state()
endif

" Required:
filetype plugin indent on
syntax enable

" If you want to install not installed plugins on startup.
"if dein#check_install()
"  call dein#install()
"endif

"End dein Scripts-------------------------


Done.
Complete setup dein!

あとは " Add or remove your plugins here: のコメント下の様に
call dein#add('プラグインのGitHubリポジトリ')
とプラグインを書いていけばOKみたいだが、プラグインをtomlファイルで管理で出来るみたいなのでvimrcと別でプラグインを管理するファイルを作成し、読み込むことにする。

プラグインリストの作成

今回は dein.vim のインストールディレクトリを ~/.vim/dein/ にしたので、この配下にuserconfigを作り、その下にtomlを書いていく。

$ cd ~/.vim/dein
$ mkdir userconfig

plugins.toml

起動時に読み込むプラグインを指定する。

~/.vim/dein/userconfig/plugins.toml
[[plugins]]
 repo = 'Shougo/dein.vim'

[[plugins]]
repo = 'Shougo/vimproc.vim'

plugins_lazy.toml

特定の条件でプラグインを読み込む。
例えばpythonを書く時だけpythonのプラグインを読み込むなどが出来る

~/.vim/dein/userconfig/plugins_lazy.toml
[[plugins]]
repo   = 'Shougo/unite.vim'

# unite.vim を読み込んだら一緒に読み込む
[[plugins]]
repo      = 'Shougo/neomru.vim'
on_source = ['unite.vim']

[[plugins]]
repo = "davidhalter/jedi-vim"
on_ft = ['python']

[[plugins]]
repo = 'fatih/vim-go'
on_ft = ['go']

条件指定などの詳細は vim を起動して:h dein-options で確認できる。

vimrc の修正

プラグインをtomlから読み込めるように vimrc を修正。ついでに dein.vim が無ければ自動的にインストール出来るよにする。

vimrc
"dein Scripts-----------------------------
if &compatible
  set nocompatible               " Be iMproved
endif

let s:dein_path = expand('~/.vim/dein')
let s:dein_repo_path = s:dein_path . '/repos/github.com/Shougo/dein.vim'

" dein.vim がなければ github からclone
if &runtimepath !~# '/dein.vim'
  if !isdirectory(s:dein_repo_path)
    execute '!git clone https://github.com/Shougo/dein.vim' s:dein_repo_path
  endif
  execute 'set runtimepath^=' . fnamemodify(s:dein_repo_path, ':p')
endif

if dein#load_state(s:dein_path)
  call dein#begin(s:dein_path)

  let g:config_dir  = expand('~/.vim/dein/userconfig')
  let s:toml        = g:config_dir . '/plugins.toml'
  let s:lazy_toml   = g:config_dir . '/plugins_lazy.toml'

  " TOML 読み込み
  call dein#load_toml(s:toml,      {'lazy': 0})
  call dein#load_toml(s:lazy_toml, {'lazy': 1})

  call dein#end()
  call dein#save_state()
endif


" Required:
filetype plugin indent on
syntax enable

" インストールされていないプラグインがあればインストールする
" If you want to install not installed plugins on startup.
if dein#check_install()
  call dein#install()
endif
"End dein Scripts-------------------------

インストールリストの確認

vim 上でインストールされているプラグインを確認してみる。
test.py を開き、

test.py
# -*- coding: utf-8 -*-

print('hello')

:Unite deinを実行すると

  Shougo/dein.vim
# cespare/vim-toml
  Shougo/vimproc.vim
# fatih/vim-go
  Shougo/neomru.vim
  Shougo/unite.vim
  davidhalter/jedi-vim

このようにリストが表示される
plugins_lazy.toml で特定の条件で指定しているプラグインはコメントになる。 pythonファイルを開いているので、jedi-vimが起動され、vim-govim-tomlはコメントになっている。

jedi-vim

:h dedi などで以下の様なヘルプ画面が表示されればインストールされていることが確認できる。

jedi-vim.txt - For Vim version 7.3 - Last change: 2014/07/29
       __   _______  _______   __       ____    ____  __  .___  ___.
      |  | |   ____||       \ |  |      \   \  /   / |  | |   \/   |
      |  | |  |__   |  .--.  ||  |  _____\   \/   /  |  | |  \  /  |
.--.  |  | |   __|  |  |  |  ||  | |______\      /   |  | |  |\/|  |
|  `--'  | |  |____ |  '--'  ||  |         \    /    |  | |  |  |  |
 \______/  |_______||_______/ |__|          \__/     |__| |__|  |__|

          jedi-vim - awesome Python autocompletion with Vim

==============================================================================
Contents                                jedi-vim-contents

1. Introduction                         jedi-vim-introduction
2. Installation                         jedi-vim-installation
    2.0. Requirements                   jedi-vim-installation-requirements
    2.1. Manually                       jedi-vim-installation-manually
    2.2. Using Pathogen                 jedi-vim-installation-pathogen
    2.3. Using Vundle                   jedi-vim-installation-vundle
    2.4. Installing from Repositories   jedi-vim-installation-repos
3. Supported Python features            jedi-vim-support
4. Usage                                jedi-vim-usage
5. Mappings                             jedi-vim-keybindings
    5.1. Start completion               g:jedi#completions_command
    5.2. Go to definition               g:jedi#goto_command
    5.3. Go to assignment               g:jedi#goto_assignments_command
    5.4  Go to definition (deprecated)  g:jedi#goto_definitions_command
    5.5. Show documentation             g:jedi#documentation_command

※僕の場合はヘルプ画面が表示されなかったので、 helptags で runtimepath を指定して実行した。
ヘルプはdavidhalter/jedi-vim/doc/jedi-vim.txtを参照しているので、doc配下にtagsを置く。
:helptags ~/.vim/dein/repos/github.com/davidhalter/jedi-vim/doc/を実行したら参照出来るようになった。

ハマりどころ

pyenv でバージョンを切り替えていたせいなのか、Vim起動時にjedi-vimがうまくインストールされなかった。
調べたところ、pythonを再コンパイルする必要がある模様
https://rcmdnk.com/blog/2014/12/25/computer-vim/

対処法

## pythonのバージョンを system に戻す
$ pyenv global system
$ pyenv versions
* system (set by /Users/suyong/.pyenv/version)
  3.6.2

## vim の再インストール
$ brew reinstall vim --HEAD

参考サイト

NeoBundle から dein.vim に乗り換えたら爆速だった話
dein.vimにお引越し
【Python環境整備】脱NeoBundle。超便利補完プラグインjedi-vimの環境をdeinで整えて快適になる設定までやる
Vimメモ : jedi-vimでPythonの入力補完
Dein.vimをインストール、使ってみる。
dein.vimでvimのplugin管理
Mac で vim を Python の開発環境にする。

26
35
2

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
26
35