LoginSignup
8
7

More than 5 years have passed since last update.

Fish ShellでNeovim導入指南

Posted at

DjangoやPythonを普段書くのでfishの良さとか、Django使う人にNeoVimを活用していく人が増えればいいですねっと。。

Mac上でインストールの方法

shellで

brew tap neovim/neovim

完了したら

brew install --HEAD neovim

~/.config/nvim/ を作成

mkdir ~/.config/nvim/

~/.config/nvim/init.vimが設定ファイルになるので作成。

vim ~/.config/nvim/init.vim

Python3の設定

brew install pyenv
pyenv install python 3.7.0

## Fish Shellの設定
```~/.config/fish/config.fish
# Path to Oh My Fish install.
set -gx OMF_PATH $HOME/.local/share/omf


" (中略)

" ↓ -xがないとエクスポートできないので注意。
set -x XDG_CONFIG_HOME ~/.config/ XDG_CONFIG_HOME
export $XDG_CONFIG_HOME

" 一番最後はこれになっている
source $OMF_PATH/init.fish

プラグインを書き込むファイルの事前作成

~/.config/nvim/ 上に下記ファイルを作成しておく。
・dein.toml
・dein_lazy.toml

ここから設定に入る。

let g:python3_host_prog = expand('~/.pyenv/versions/neovim3/bin/python')を入れることで
python3の設定ができるのでdeoplete.vimなどpython3が必要なものが入る。

~/.config/nvim/init.vim
" 行数
set number

set noswapfile

" insertモードから抜ける
inoremap <silent> jj <ESC>
inoremap <silent> <C-j> j
inoremap <silent> kk <ESC>
inoremap <silent> <C-k> k
noremap w b
noremap b w
" vを二回で行末まで選択
vnoremap v $h
noremap j gj
noremap k gk
" ft+hで左端まで移動
noremap <S-h> ^
"shift+lで右端まで移動
noremap <S-l> $


filetype indent on
" ファイルタイプ別のプラグイン/インデントを有効にする
filetype plugin indent on


set expandtab
set hlsearch            " 検索マッチテキストをハイライト
set ignorecase          " 大文字小文字を区別しない
set incsearch           " インクリメンタルサーチ
set matchtime=3         " 対応括弧のハイライト表示を3秒にする
set tabstop=2
set scrolloff=5
set smartcase           " 検索文字に大文字がある場合は大文字小文字を区別
set smarttab
set showmatch           " 対応する括弧などをハイライト表示する
set shiftwidth=2
set softtabstop=4
set expandtab
set foldcolumn=4
set foldmethod=indent "(インデントで折りたたみを自動作成してくれる)
set list                " 不可視文字の可視化
" ESCを二回押すことでハイライトを消す
nmap <silent> <Esc><Esc> :nohlsearch<CR>


" プラグインがインストールされるディレクトリ
let s:dein_dir = expand('~/.cache/dein')
" dein.vim 本体
let s:dein_repo_dir = s:dein_dir . '/repos/github.com/Shougo/dein.vim'

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

" 設定開始
if dein#load_state(s:dein_dir)
  call dein#begin(s:dein_dir)

  " プラグインリストを収めた TOML ファイル
  " 予め TOML ファイルを用意しておく
  let g:rc_dir    = expand("~/.config/nvim/")
  let s:toml      = g:rc_dir . '/dein.toml'
  let s:lazy_toml = g:rc_dir . '/dein_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

" もし、未インストールものものがあったらインストール
if dein#check_install()
  call dein#install()
endif

あとはお好みでプラグインを足す。

~/.config/nvim/dein.vim
[[plugins]]
repo = 'Shougo/dein.vim'

[[plugins]] # toml syntax
repo = 'cespare/vim-toml'
on_ft = 'toml'

[[plugins]] # カラースキーマ
repo = 'w0ng/vim-hybrid'
hook_add = '''
  set background=dark
'''

[[plugins]] # NerdTree
repo = 'scrooloose/nerdtree'


[[plugins]] # NerdTree
repo = 'jistr/vim-nerdtree-tabs'
hook_add = 'map <silent><C-e> <plug>NERDTreeTabsToggle<CR>'

[[plugins]] # ステータスバー改造プラグイン
repo = 'vim-airline/vim-airline'
hook_add = '''

let g:airline_mode_map = {
    \ '__' : '-',
    \ 'n'  : 'N',
    \ 'i'  : 'I',
    \ 'R'  : 'R',
    \ 'c'  : 'C',
    \ 'v'  : 'V',
    \ 'V'  : 'V',
    \ '' : 'V',
    \ 's'  : 'S',
    \ 'S'  : 'S',
    \ }

" パワーラインでかっこよく
let g:airline_powerline_fonts = 1

" カラーテーマ指定してかっこよく
let g:airline_theme = 'badwolf'

" タブバーをかっこよく
let g:airline#extensions#tabline#enabled = 1

" 選択行列の表示をカスタム(デフォルトだと長くて横幅を圧迫するので最小限に)
let g:airline_section_z = airline#section#create(['windowswap', '%3p%% ', 'linenr', ':%3v'])

" virtulenvを認識しているか確認用に、現在activateされているvirtualenvを表示(vim-virtualenvの拡張)
let g:airline#extensions#virtualenv#enabled = 1

" gitのHEADから変更した行の+-を非表示(vim-gitgutterの拡張)
let g:airline#extensions#hunks#enabled = 0

" Lintツールによるエラー、警告を表示(ALEの拡張)
let g:airline#extensions#ale#enabled = 1
let g:airline#extensions#ale#error_symbol = 'E:'
let g:airline#extensions#ale#warning_symbol = 'W:'
'''

[[plugins]] # vimから離れないでGitが使える
repo = 'tpope/vim-fugitive'
hook_add = '''
nmap [figitive] <Nop>
map <Leader>g [figitive]
nmap <silent> [figitive]s :<C-u>Gstatus<CR>
nmap <silent> [figitive]d :<C-u>Gdiff<CR>
nmap <silent> [figitive]b :<C-u>Gblame<CR>
nmap <silent> [figitive]l :<C-u>Glog<CR>
'''

[[plugins]] # vimから離れないでGitが使える
repo = 'airblade/vim-gitgutter'

[[plugins]] # liner実行プラグイン
repo = 'w0rp/ale'
hook_add = '''
" エラー行に表示するマーク
let g:ale_sign_error = '⨉'
let g:ale_sign_warning = '⚠'
" エラー行にカーソルをあわせた際に表示されるメッセージフォーマット
let g:ale_echo_msg_format = '[%linter%] %s [%severity%]'
" エラー表示の列を常時表示
let g:ale_sign_column_always = 1

" ファイルを開いたときにlint実行
let g:ale_lint_on_enter = 1
" ファイルを保存したときにlint実行
let g:ale_lint_on_save = 1
" 編集中のlintはしない
let g:ale_lint_on_text_changed = 'never'

" lint結果をロケーションリストとQuickFixには表示しない
" 出てると結構うざいしQuickFixを書き換えられるのは困る
let g:ale_set_loclist = 0
let g:ale_set_quickfix = 0
let g:ale_open_list = 0
let g:ale_keep_list_window_open = 0

" 有効にするlinter
let g:ale_linters = {
\   'python': ['flake8'],
\}

" ALE用プレフィックス
nmap [ale] <Nop>
map <C-k> [ale]
" エラー行にジャンプ
nmap <silent> [ale]<C-P> <Plug>(ale_previous)
nmap <silent> [ale]<C-N> <Plug>(ale_next)
'''

[[plugins]] # pep8準拠のインデントにしてくれる。
repo = 'Vimjas/vim-python-pep8-indent'

[[plugins]] # コメントアウトが楽になる
repo = 'tomtom/tcomment_vim'
add_hook = 'let g:tcommentMapLeader1 = <C-=>'

[[plugins]] # 囲える。
repo = 'tpope/vim-surround'

[[plugins]] # HTMLタグの生成
repo = 'mattn/emmet-vim'

[[plugins]] # python等の言語の文法チェックを自動的にやってくれる
repo = 'scrooloose/syntastic'

[[plugins]] # カッコを自動で閉じる
repo = 'Townk/vim-autoclose'

[[plugins]] # ifとかの終了宣言を自動で挿入してくれる
repo = 'tpope/vim-endwise'
add_hook = '''
{
'autoload' : { 'insert' : 1,}
}
'''

[[plugins]] # 行末の半角空行を可視化
repo ='bronson/vim-trailing-whitespace'

[[plugins]] # ログに色付け
repo = 'vim-scripts/AnsiEsc.vim'

[[plugins]] # フォントズーム
repo   = 'thinca/vim-fontzoom'
on_cmd = ['Fontzoom']
on_map = ['<Plug>(fontzoom-']
gui    = 1

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

[[plugins]]
repo = 'tell-k/vim-autopep8'
add_hook = '''
{'for': 'python'}
let g:autopep8_max_line_length=99
let g:autopep8_disable_show_diff=1
autocmd FileType python map <buffer> <F8> :call Autopep8()<CR>
'''

[[plugins]] # css3のシンタックス
repo = 'hail2u/vim-css3-syntax'

[[plugins]] # html5のシンタックス
repo = 'taichouchou2/html5.vim'

[[plugins]] # jsシンタックス
repo = 'billyvg/tigris.nvim'
on_ft = ['javascript', 'javascript.jsx']
hook_post_update = '''
let g:dein#plugin.build = './install.sh'
let g:tigris#enabled = 1
let g:tigris#on_the_fly_enabled = 1
let g:tigris#delay = 300
'''

[[plugins]]
repo = 'Shougo/denite.nvim'

[[plugins]]
repo = 'Shougo/deoplete.nvim'
add_hook = 'let g:deoplete#enable_at_startup = 1'

[[plugins]]
repo = 'Shougo/neosnippet-snippets'

dein_lazy.toml(こっちは遅延ロードのもの。)

~/.config/nvim/dein_lazy.vim
[[plugins]]
repo = 'Shougo/deoplete.nvim'
hook_source = '''
  let g:deoplete#enable_at_startup = 1
  inoremap <expr><tab> pumvisible() ? "\<C-n>" :
        \ neosnippet#expandable_or_jumpable() ?
        \    "\<Plug>(neosnippet_expand_or_jump)" : "\<tab>"
'''
on_i = 1

[[plugins]]
repo = 'Shougo/neosnippet'
hook_source = '''
  imap <C-k> <Plug>(neosnippet_expand_or_jump)
  smap <C-k> <Plug>(neosnippet_expand_or_jump)
  xmap <C-k> <Plug>(neosnippet_expand_target)
  if has('conceal')
    set conceallevel=2 concealcursor=niv
  endif
'''
on_i  = 1
on_ft = ['snippet']
depends = ['neosnippet-snippets']# dein_lazy.toml

[[plugins]]
repo = 'Shougo/deoplete.nvim'
hook_source = '''
  let g:deoplete#enable_at_startup = 1
  inoremap <expr><tab> pumvisible() ? "\<C-n>" :
        \ neosnippet#expandable_or_jumpable() ?
        \    "\<Plug>(neosnippet_expand_or_jump)" : "\<tab>"
'''
on_i = 1

[[plugins]]
repo = 'Shougo/neosnippet'
hook_source = '''
  imap <C-k> <Plug>(neosnippet_expand_or_jump)
  smap <C-k> <Plug>(neosnippet_expand_or_jump)
  xmap <C-k> <Plug>(neosnippet_expand_target)
  if has('conceal')
    set conceallevel=2 concealcursor=niv
  endif
'''
on_i  = 1
on_ft = ['snippet']
depends = ['neosnippet-snippets']

neovimを起動して:echo has('python3')で1が返ってきた場合、python3の設定はできてる。

参考になったサイト

NeovimでモダンなPython環境を構築する

Mac で Neovim を Python の開発環境にする。

[個人的NeoVim環境(Python編) - Diary over Finite Fields]

(https://blog.515hikaru.net/entry/2017/06/25/043035)

[今度こそVimを攻略したい:neovimをインストールして最高のdev UXを手に入れるまで(その1) - Studio Andy]

(http://studio-andy.hatenablog.com/entry/2018/02/04/120259)

[Neovimはじめました & 2016年vimrc大掃除]

(https://muunyblue.github.io/c95d62c68196b2d0c1c1de8c7eeb6d50.html)

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