LoginSignup
35
34

More than 5 years have passed since last update.

VimにPythonコードの補完機能を追加して簡単に編集できるようにする

Posted at

はじめに

vimのインストールから設定までを書いている記事がなかなか無かったので、
自分で書きます。
vim初心者がとりあえずPythonの補完機能を入れられるように書いています。
以下のサイトを参考にさせていただきました。

Vimインストール

sudo apt-get update
sudo apt-get install vim

Vimの設定

vimのプラグイン管理を行う dein.vim というものを使用してjedi-vimをインストールします。
今回は、 ~/.cache/dein にインストールします。

cd ~
mkdir ~/.cache
mkdir ~/.cache/dein
cd .cache/dein/
wget https://raw.githubusercontent.com/Shougo/dein.vim/master/bin/installer.sh
sh ./installer.sh ~/.cache/dein/

次に、プラグインの管理のファイル( plugin.tomlplugin_lazy.toml )を作成します。
plugin.tomlplugin_lazy.toml の使い分けは以下の通りです。
特定の条件とは、Pythonの場合やマークダウンの場合などの開くファイルの種類に応じて
読み込むプラグインが指定できるようになっています。

  • plugin.toml    … 起動時に読み込むプラグインを指定する。
  • plugin_lazy.toml … 特定の条件でプラグインを指定する。
cd ~/.cache/
mkdir userconfig
cd userconfig/
vi plugin.toml
plugin.toml
[[plugins]]
 repo = 'Shougo/dein.vim'

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

以下のファイルにPythonのプラグインの設定を行います。

vi plugin_lazy.toml
plugin_lazy.toml
[[plugins]]
repo   = 'Shougo/unite.vim'

[[plugins]]
repo      = 'Shougo/neomru.vim'
on_source = ['unite.vim']

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

最後に、vimの設定ファイルにプラグインの設定を書き込みます。

cd ~
vi ~/.vimrc
~/.vimrc
"dein Scripts-----------------------------
if &compatible
  set nocompatible               " Be iMproved
endif

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

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('~/.cache/userconfig')
  let s:toml        = g:config_dir . '/plugin.toml'
  let s:lazy_toml   = g:config_dir . '/plugin_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を開くと、プラグインのダウンロードが始まります。
プラグインがダウンロードし終わった後に、適当なpythonファイルを開きます。

vi test.py

コマンドモードで、以下のコマンドを打つと、jediのヘルプ画面が表示されます。
これでプラグインが読み込まれていることが確認できます。

:help jedi

test.png

ヘルプ画面を閉じるときは、 :q で閉じます。

補完機能も確認します。

test2.png

ちゃんと機能していれば、ドットを打った時に一覧が表示されます。

まとめ

なるべくvim初心者でも設定できるように書いたつもりですが。
わからないところがあればコメントをお願いします
(僕自身が初心者なのでご容赦を・・・)。

また、jedi以外にも便利なプラグインがあれば追記していこうと考えています。

35
34
3

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