LoginSignup
7
10

More than 5 years have passed since last update.

vim に python用プラグイン導入 [CentOS 7.4]

Last updated at Posted at 2018-11-10

はじめに

python の勉強を始めて、これまで jupyter notebook を利用していましたが、
より一層、開発補助の効くエディタを調べている内、vimカスタマイズに興味を持ちました。

これまでデフォルトのvimを利用していましたが、
便利なプラグインも多いと知り、軽い気持ちで導入を試みました。

が、えらくハマった為、ここに記録を残します。

環境

OS:CentOS 7.4
Python:2.7 & 3.6
vim:7.4.160(OSバンドル) & 7.4.884 (再コンパイル)
vimプラグイン:dein, vimproc, unite, jedi

手順

1.vim の状態を確認する。→ pythonが無効であれば再ビルドする
2.dein 導入
3.tomlファイルの編集
4..vimrc の編集
5.vim を起動してプラグインの自動導入

これまで vimカスタマイズの経験がなかった為、
色々なページを参考にて、手順2から始めてしまいました。
手順1の必要性を認識していなった点がハマった原因です。

導入作業

事前確認

vim のビルド情報を参照してpython の状態を確認します。

vi --version | grep python

-comments        -langmap         -python          -viminfo
-conceal         -libcall         -python3         -vreplace

「python」 と 「python3」の頭文字部分が「-」になっています。

「-」の状態だと vim でpython実行が無効。ビルドが必要です。
「+」であれば、有効を意味します。

python が無効であると、関連プラグインは当然動きません。

vimを再ビルドする

以下の記事を参考ください。

vim8.0をインストールする

vimのソースをダウンロードする

wget https://github.com/vim/vim/archive/v7.4.884.tar.gz
tar -xfv https://github.com/vim/vim/archive/v7.4.884.tar.gz
cd  vim-7.4.884

Makefile を作成する

python2 と python3 の混在環境を用意するのであれば、
--with からconfigライブラリのパスを指定する必要があります。

 ./configure \
 --enable-fail-if-missing \
 --with-features=huge \
 --disable-selinux \
 --enable-luainterp \
 --enable-pythoninterp=dynamic \
 --with-python-config-dir=/usr/lib64/python2.7/config \
 --enable-python3interp=dynamic \
 --with-python3-config-dir=/usr/lib64/python3.6/config-3.6m-x86_64-linux-gnu \
 --enable-cscope \
 --enable-fontset \
 --enable-multibyte \
 vi_cv_path_python3=/usr/bin/python3.6

また、lua や python でエラーが発生するようであれば、
開発パッケージが足りていない可能性があります。
必要に応じて、インストールします。

yum install python-devel lua-devel

インストールする

make install

deinプラグインを導入する

以下の記事が参考になります。

dein.vimでプラグイン管理のはじめ方

tomlファイルを編集する

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

[[plugins]]
repo = 'Shougo/vimproc.vim'
~/.vim/bundles/userconfig/plugin_lazy.toml
[[plugins]]                                                                 
repo   = 'Shougo/unite.vim'

# ファイルタイプ=python の際に読み込む
[[plugins]]
repo = 'davidhalter/jedi-vim'
on_ft = ['python']

[[plugins]]
repo = 'vim-syntastic/syntastic'
on_ft = ['python']
hook_post_source = 'let g:syntastic_python_checkers = ["flake8"]'

以下の記事が、参考になります。

NeoBundle から dein.vim に乗り換えたら爆速だった話
dein.vimによるプラグイン管理のマイベストプラクティス

.vimrc を編集する

ホームディレクトリに「.vimrc」を作成して編集することで、
オリジナルのvim環境が整備できます。
以下のように設定すると、上手く動きました。

.vimrc
set ignorecase "大文字/小文字を区別せず検索
set hlsearch   "検索結果をハイライト
set number     "行番号を表示
set showmatch  "相関するカッコを強調表示
syntax on      "シンタックスハイライトを有効に
set tabstop=4  "インデントをスペース4つ分に

autocmd BufRead,BufNewFile *.py setfiletype python
autocmd FileType python setlocal completeopt-=preview

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

" Required:
set runtimepath+=/home/manage/.vim/bundles/repos/github.com/Shougo/dein.vim

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

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

  let g:config_dir = s:dein_path . '/userconfig'
  let s:toml       = g:config_dir . '/plugin.toml'
  let s:lazy_toml  = g:config_dir . '/plugin_lazy.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 を起動してプラグインの自動導入

vim を起動すると、dedi が自動ダウンロードしてくれます。

[root@localhost]# vim
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                              VIM - Vi IMproved                                
~                                                                               
~                               version 7.4.884                                 
~                            by Bram Moolenaar 他.                              
~                 Vim はオープンソースであり自由に配布可能です                  
~                                                                               
~                         Vimの開発を応援してください!                          
~                詳細な情報は           :help sponsor<Enter>                    
~                                                                                                                                                             
~                終了するには           :q<Enter>                               
~                オンラインヘルプは     :help<Enter> か <F1>                    
~                バージョン情報は       :help version7
<Enter>
[dein] (1/3) |jedi-vim            | ['git', 'clone', '--recursive', 'https://github.com/davidhalter/jedi-vim.git','/home/manage/.vim/bundles/repos/github.com/davidhalter/jedi-vim']
[dein] (1/3) [======              ] jedi-vim
[dein] (2/3) |vimproc.vim         | ['git', 'clone', '--recursive', 'https://github.com/Shougo/vimproc.vim.git', '/home/manage/.vim/bundles/repos/github.com/Shougo/vimproc.vim']
[dein] (2/3) [=============       ] vimproc.vim
[dein] (3/3) |unite.vim           | ['git', 'clone', '--recursive', 'https://github.com/Shougo/unite.vim.git', '/home/manage/.vim/bundles/repos/github.com/Shougo/unite.vim']
[dein] (3/3) [====================] unite.vim
[dein] Updated plugins:
[dein]   jedi-vim(1 change)
[dein]   vimproc.vim(1 change)
[dein]   unite.vim(1 change)
[dein] Done: (201y/mm/dd hh:mm:ss)
続けるにはENTERを押すかコマンドを入力してください

動作確認する

test.py
print('test')

pythonファイルを補助してくれる vim にできました。

振り返り

ハマった原因は、OSバンドル版 vim で
pythonライブラリを読む設定にビルドしていなかったこと。
これに気づけていませんでした。

vim の設定をしっかり見ていれば察しもついたのかな。反省。

7
10
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
7
10