denite.nvim の設定 備忘録

denite.nvimはvimでいうところのUnite

インストー

dein.tomlに以下を追加

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

#インストール後にNeoVimで以下を実行して再起動することで有効になる
:UpdateRemotePlugins

設定

まだまだ設定しきれてないが設定したのは下記

  • grepでagを使用する まずはagをインストー
$ brew install the_silver_searcher

neovim側の設定

"grepでagを使用するように設定
call denite#custom#var('grep', 'command', ['ag'])
"カレントディレクトリ内の検索もagを使用する
call denite#custom#var('file_rec', 'command', ['ag', '--follow', '--nocolor', '--nogroup', '-g', ''])
"その他のgrepの設定
call denite#custom#var('grep', 'default_opts',['-i', '--vimgrep'])
call denite#custom#var('grep', 'recursive_opts', [])
call denite#custom#var('grep', 'pattern_opt', [])
call denite#custom#var('grep', 'separator', ['--'])
call denite#custom#var('grep', 'final_opts', [])
"denite時に使用するキーマップ
"ESCキーでdeniteを終了
call denite#custom#map('insert', '<esc>', '<denite:enter_mode:normal>', 'noremap')
call denite#custom#map('normal', '<esc>', '<denite:quit>', 'noremap')
"C-N,C-Pで上下移動
call denite#custom#map('insert', '<C-n>', '<denite:move_to_next_line>', 'noremap')
call denite#custom#map('insert', '<C-p>', '<denite:move_to_previous_line>', 'noremap')
"C-J,C-Kでsplitで開く
call denite#custom#map('insert', '<C-j>', '<denite:do_action:split>', 'noremap')
call denite#custom#map('insert', '<C-k>', '<denite:do_action:vsplit>', 'noremap')

" 以下はdenite起動時に使用するキーマップ
" バッファ一覧
noremap <C-P> :Denite buffer<CR>
" ファイル一覧
noremap <C-N> :Denite -buffer-name=file file<CR>
" 最近使ったファイルの一覧
noremap <C-Z> :Denite file_old<CR>
" カレントディレクトリ
noremap <C-C> :Denite file_rec<CR>
"バッファ一覧
nnoremap sB :<C-u>Denite buffer -buffer-name=file<CR>

"Denite でバッファ内検索 
nnoremap <silent> <Leader><C-f> :<C-u>Denite line<CR>
nnoremap <silent> <expr><Space>l ":<C-u>DeniteWithCursorWord line<CR>"
  • 表示をカスタマイズ
" プロンプトの左端に表示される文字を指定
call denite#custom#option('default', 'prompt', '>')
" deniteの起動位置をtopに変更
call denite#custom#option('default', 'direction', 'top')

MacのSpotlightからneovimを起動する方法

そもそもSpotlightから起動するのが、しっくり来てない人もいると思いますが、
下のGIFを見て貰えればどういうことかわかると思います。

f:id:replicity:20170602015004g:plain

私は普段、Spotlightから様々なアプリを起動しており、 ちょっとメモが取りたいなーって時とかはターミナルでvimを開かず、ここから起動してました。
なので、neovimもここから起動できるように設定します。

neovimはMacVimのようにアプリケーションとしてMacにインストールされず、GUI版もないのでインストールをしただけだと、Spotlightからは起動できないです。

ではどうするかと言うと、neovimを起動するスクリプトを書いて、それをSpotlightから実行することでSpotlightからneovimを起動するのを実現します。
スクリプトをSpotlightから実行できるようにするには.command拡張子でファイルを書けばいいので、下記のファイルを適当な場所に作成しておきます。

今回はファイル名はneovim_run.commandにしてます。

!#/bin/sh
nvim

ファイルをおいたらもうSpotlightから起動できるはずです。

f:id:replicity:20170602015048g:plain

Neovim 補完設定

すでに前回までの設定が終わっている状態で設定を続けていきます。
neovimで補完を有効にするために設定をする。
neovimではvimで使っていた、neocomplete.vimではなくdeoplete.nvimを使用する。
また、スニペットの補完はvimと同じでneosnippet.vimを使用する

deoplete.nvimとneosnippet.vimのインストー

pluginはTOMLで管理しているのでTOMLに以下を追加

[[plugins]]
repo = 'Shougo/deoplete.nvim'
on_i = 1
[[plugins]]
repo = 'Shougo/neosnippet.vim.git'
on_i = 1
on_ft = 'snipppet'

追加したら、あとは起動時にダウンロードされる

補完用のポップアップが表示された時の設定

補完用のポップアップが表示されている時にで移動できるように設定をplugin毎の設定ファイルに追加
追加した設定は下記

"補完候補選択時は<TAB>で候補移動snipppet時は<TAB>で次の入力先へ
imap <expr><TAB> pumvisible() ? "\<C-N>" : neosnippet#jumpable() ?  "\<Plug>(neosnippet_expand_or_jump)" : "\<TAB>"
smap <expr><TAB> neosnippet#jumpable() ?  "\<Plug>(neosnippet_expand_or_jump)" : "\<TAB>"
inoremap <expr><S-TAB>  pumvisible() ? "\<C-p>" : "\<S-TAB>"

まとめ

これでneovimでも補完が表示されるようになったのでほぼvimと同じように使えるようになってきた。
deoplete.nvimは言語ごとに保管用のpluginを入れていくようなので、必要になった言語を都度追加していく予定

alacrittyインストール 備忘録

vimとneovimをiTerm2で使用中にスクロールが遅くなることがあり、調べてたらalacrittyというのが良さげだったのでインストールした
基本的にはこの記事を参考にやれば問題はない : http://qiita.com/zebult/items/0047b72916383a5c0acf
ただ、今回はbrewからインストールするとmakeで失敗してインストールできなかった

エラーメッセージ

$brew install --HEAD mscharley/homebrew/alacritty                                                                                                                                                                                   
==> Installing alacritty from mscharley/homebrew
==> Cloning git@github.com:jwilm/alacritty.git
Updating  ~/Library/Caches/Homebrew/alacritty--git
==> Checking out branch master
==> make app
Last 15 lines from ~/Library/Logs/Homebrew/alacritty/01.make:
   Compiling cgmath v0.7.0
   Compiling walkdir v0.1.8
   Compiling notify v2.6.3
error: custom derive attribute panicked
   --> src/ansi.rs:373:62
    |
373 | #[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
    |                                                              ^^^^^^^^^
    |
    = help: message: proc_macro::__internal::with_parse_sess() called before set_parse_sess()!

error: Could not compile `alacritty`.

To learn more, run the command again with --verbose.
make: *** [alacritty] Error 101

If reporting this issue please do so at (not Homebrew/brew or Homebrew/core):
https://github.com/mscharley/homebrew-homebrew/issues

おそらくこのissuesと同じような問題だと思われる
https://github.com/jwilm/alacritty/issues/413
alacrittyの問題というかrust周りの問題?
brewで入れるのはとりあえず諦めて自分でbuildすることにする

#rustのインストール
#rustはrustupで管理するのがよさげ : http://qiita.com/chikoski/items/b6461367e8c3875bb235
$curl https://sh.rustup.rs -sSf | sh
#zshrcにsource ~/.cargo/env を追加
$echo 'source ~/.cargo/env' >> .zshrc
#alacrittyのインストール
#これは公式の手順を参考に実施 :  https://github.com/jwilm/alacritty
$git clone https://github.com/jwilm/alacritty.git
$cd alacritty
$rustup override set stable
$rustup update stable
$cargo build --release
$sudo cp target/release/alacritty /usr/local/bin #alacrittyコマンドでalacrittyが起動する

とりあえず無事インストールできたので使用しながら設定は直していく

2017/04/24 追記 rustupさえ入れておけばbrewでのmakeに失敗しないようでした
https://github.com/cema-sp/homebrew-tap

$curl https://sh.rustup.rs -sSf | sh
$echo 'source ~/.cargo/env' >> .zshrc
$brew install --HEAD cema-sp/tap/alacritty

Neovim 入門3 plugin

neovimでもvimと同じようにpluginが使える。
vimで使用していたdein.vimはNeovimもサポートしているので、これをそのまま使う。
https://github.com/Shougo/dein.vim

deinのインストール

dein.vimのインストールは公式サイトを参考にinit.vimに追加
なお、pluginはTOMLファイルに記述していく。

let s:dein_cache_path = expand('~/.cache/nvim/dein')
let g:home = expand('~')
let g:nvim_home = g:home .'/.config/nvim'
let s:dein_dir = expand(g:home . '/.cache/dein')
let s:dein_repo_dir = s:dein_dir . '/repos/github.com/Shougo/dein.vim'

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

let g:rc_dir   = g:nvim_home . '/rc'
let g:dein#install_max_processes = 16
let g:dein#install_progress_type = 'title'
let g:dein#enable_notification = 1
let s:toml = g:rc_dir .  '/dein.toml'
let s:lazy_toml = g:rc_dir . '/dein_lazy.toml'

if dein#load_state(s:dein_dir)
    call dein#begin(s:dein_dir, [s:toml, s: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

if dein#check_install()
    call dein#install()
endif

#plugin毎の設定ロード
for plugin in glob(g:rc_dir . '/plugins/*', 1, 1)
    execute "source " . plugin
endfor

filetype plugin indent on

#plugin毎に設定ファイルを分け、それを読み込む
for plugin in glob(g:rc_dir . '/plugins/*', 1, 1)
    execute "source " . plugin
endfor

neoterm

せっかくなのでNeovimぽいpluginを入れてみる。
Neovimではvimの中のshellを実行できるので(TERMINALモード)、それを便利にするpluginを入れる。
今回入れるのはneotermというplugin。
https://github.com/kassio/neoterm

dein.tomlに下記を追加

[[plugins]]
repo = 'kassio/neoterm'

これでnvimを起動したタイミングでdein.vimがpluginをダウンロードして来てくれる。

neotermの設定を記述(公式サイトのexampleから必要そうなのだけをもってきただけ)

let g:neoterm_position = 'horizontal'

" Useful maps
" hide/close terminal
nnoremap <silent> ,th :call neoterm#close()<cr>
" clear terminal
nnoremap <silent> ,tl :call neoterm#clear()<cr>
" kills the current job (send a <c-c>)
nnoremap <silent> ,tc :call neoterm#kill()<cr>

" Git commands
command! -nargs=+ Tg :T git <args>

まとめ

とりあえずこれで、初期設定とpluginのインストールはできたので、あとは必要なpluginや設定を適宜追加していくだけまではできた。
フォルダ構成としては現状は以下になっている。

nvim
    ├── init.vim
    └── rc
        ├── color.vim
        ├── dein.toml
        ├── dein_lazy.toml
        ├── filetypes.vim
        ├── mapping.vim
        ├── plugins
        │   └── neoterm.vim
        └── settings.vim

neovim 入門2 CheckHealth

neovimには自己判断ツールが入ってるので、それを実行する。
https://github.com/neovim/neovim/wiki/FAQ#my-ctrl-h-mapping-doesnt-work

実行方法

:CheckHealt 

エラーと修正方法

## terminfo
  - ERROR: key_backspace (kbs) entry is ^H (ASCII DELETE): key_backspace=^H,

    - SUGGESTIONS:
      - Set key_backspace to \177 (ASCII BACKSPACE). Run these commands:
          infocmp $TERM | sed 's/kbs=^[hH]/kbs=\\177/' > $TERM.ti
          tic $TERM.ti
      - See https://github.com/neovim/neovim/wiki/FAQ

0.2では解決済みらしいが、今回いれたのが0.1.7なのでまだ問題がある。
SUGGESTIONSに書かれている通りに実行すれば解決。

## tmux
  - SUCCESS: escape-time: 1ms
  - INFO: $TERM: screen
  - ERROR: $TERM should be "screen-256color" or "tmux-256color" when running tmux.
    - SUGGESTIONS:
      - Set default-terminal in ~/.tmux.conf:
          set-option -g default-terminal "screen-256color"

      - See https://github.com/neovim/neovim/wiki/FAQ

これは結構はまった。
tmux起動時の$TERMの設定値がscreen-256colortmux-256colorじゃないと設定通りに色が付かない。
SUGGESTIONSに書かれている書き方はtmuxのバージョンが古かったので、以下を.tmux.confに追加した。

#.tmux.conf
set -g default-terminal "screen-256color"

これで設定自体は問題ないが、tmuxのセッションが一つでも残っていると上記の設定が反映されないようで、
$TERMに設定した値が反映されないと思い15分ほど悩んでた。

##Python 2 provider
  - INFO: `g:python_host_prog` is not set.  Searching for python2 in the environment.
  - SUCCESS: pyenv found: "/usr/local/Cellar/pyenv/1.0.10/libexec/pyenv"
  - WARNING: pyenv is not set up optimally.
    - SUGGESTIONS:
      - Suggestion: Create a virtualenv specifically for Neovim using pyenv and use "g:python_host_prog".  This will avoid the need to install Neovim's Python client in each version/virtualenv.
  - INFO: Executable: /usr/local/bin/python2
  - INFO: Python2 version: 2.7.13
  - INFO: python2-neovim version: 0.1.13
  - SUCCESS: Latest python2-neovim is installed: 0.1.13

g:python_host_progの設定を.init.vimに追加すれば問題なし。

## Python 3 provider
  - WARNING: No Python interpreter was found with the neovim module.  Using the first available for diagnostics.
  - WARNING: provider/pythonx: Could not load Python 3:
    /Users/himalia/.pyenv/shims/python3 does not have the neovim module installed. See |provider-python|.
    /Users/himalia/.pyenv/shims/python3.5 does not exist: pyenv: python3.5: command not found
    
    The `python3.5' command exists in these Python versions:
    anaconda3-4.2.0
    
    
    python3.4 not found in search path or not executable.
    python3.3 not found in search path or not executable.
    /Users/himalia/.pyenv/shims/python is Python 2.7 and cannot provide Python 3.
  - INFO: `g:python3_host_prog` is not set.  Searching for python3 in the environment.
  - SUCCESS: pyenv found: "/usr/local/Cellar/pyenv/1.0.10/libexec/pyenv"
  - WARNING: pyenv is not set up optimally.
    - SUGGESTIONS:
      - Suggestion: Create a virtualenv specifically for Neovim using pyenv and use "g:python3_host_prog".  This will avoid the need to install Neovim's Python client in each version/virtualenv.
  - INFO: Executable: /usr/local/bin/python3
  - ERROR: Command error (8) /usr/local/bin/python3 -c import neovim; print(neovim.__file__): Traceback (most recent call last):  File "<string>", line 1, in <module>ModuleNotFoundError: No module named 'neovim'
  - INFO: Python3 version: 3.6.1
  - INFO: python3-neovim version: unable to find nvim executable
  - ERROR: Neovim Python client is not installed.
    - SUGGESTIONS:
      - Error found was: unable to find nvim executable
      - Use the command `$ pip3 install neovim`
  - WARNING: Latest python3-neovim is NOT installed: 0.1.13

こちらもg:python_host_progの設定を.init.vimに追加。
加えてpip3 install neovimも実行。

## Ruby provider
  - ERROR: Missing Neovim RubyGem
    - SUGGESTIONS:
      - Install or upgrade the neovim RubyGem using `gem install neovim`.
  - INFO: Ruby Version: ruby 2.0.0p648 (2015-12-16 revision 53162) [universal.x86_64-darwin16]
  - INFO: Host Executable: not found
  - INFO: Host Version: not found

SUGGESTIONSに書かれている通りにsudo gem install neovimを実行

以上の変更でERRORとWARNIGがでなくなったので、次はプラグイン入れていきます

neovim 入門1

概要

重い腰を上げてneovimの設定をすることにしたので、ログを残していきます。
現在使っているvimの設定はこちら
neovimとvimの設定を共存させることもできるみたいだが、今回はとりあえず設定ファイルは分けることにして一からneovim環境を整える
公式のリポジトリ : https://github.com/neovim/neovim

環境

  • OS : maxOS Sierra

インストール

ソースからビルドとする方法とパッケージを入れる方法があるが今回はパッケージでインストール
どちらの方法も公式のREADMEに書いてある

$brew tap neovim/neovim
$brew install neovim
$which nvim
/usr/local/bin/nvim
$nvim #neovimが起動する

設定

まずはneovimを起動して :h nvimしてヘルプを確認
どうやらneovimのディレクトリ構成はXDG Base Directory Specificationをサポートしているらしい
$XDG_CONFIG_HOMEを設定していないなら ~/.config/nvim にinit.vimを置けば~/.vimrcの代わりになる
設定ファイルを置くべき場所とファイル名がわかったので、早速設定を開始

vimの設定が大体はそのまま使えるみたいなので、vimの設定をもとに設定を作ったのが下記

"init.vim
let g:home = expand('~')
let g:vim_home = g:home .'/.config/nvim'
let g:rc_dir   = g:vim_home . '/rc'

"分割したファイルを読み込み
function s:load_rc(file)
  execute 'source ' . g:rc_dir . '/' . a:file . '.vim'
endfunction

if filereadable(expand(g:rc_dir .  '/settings.vim'))
    call s:load_rc('settings')
endif

if filereadable(expand(g:rc_dir . '/mapping.vim'))
    call s:load_rc('mapping')
endif

if filereadable(expand(g:rc_dir . '/color.vim'))
    call s:load_rc('color')
endif

if filereadable(expand(g:rc_dir . '/filetypes.vim'))
    call s:load_rc('filetypes')
endif
"setting.vim
"------------------------------------------------------------
"検索関係
"------------------------------------------------------------
"{{{
"検索時に大文字・小文字を区別しない。ただし、検索後に大文字小文字が
"混在しているときは区別する
set ignorecase
set smartcase
"検索結果をハイライト表示
set hlsearch
"逐次検索をオン
set incsearch
"Kでカーソル下の単語のhelpを引く
set keywordprg=:help
"}}}


"------------------------------------------------------------
"編集関係
"------------------------------------------------------------
"{{{
"タブ設定
set tabstop=4
set softtabstop=4
set shiftwidth=4
set noexpandtab
" '<'や'>'でインデントする際に'shiftwidth'の倍数に丸める
set shiftround
"オートインデント
set autoindent
"オートインデント、改行、インサートモード開始直後にバックスペースキーで
"削除できるようにする。
set backspace=indent,eol,start
"検索時にファイルの下まで行ったら上まで戻る
set wrapscan
"閉じ括弧入力時に対応している括弧が画面内にある場合に、一瞬開き括弧にジャンプ
set showmatch
"対応括弧のハイライト表示を0.1秒に
set matchtime=1
"コマンドライン補完を便利に
set wildmenu
"テキスト挿入の自動折り返しに日本語を対応
set formatoptions+=mM
"移動コマンドを使ったとき、行頭に移動しない
set nostartofline
"バッファが変更されているとき、コマンドをエラーにするのでなく、
"保存するかどうか確認を求める
set confirm
"ビジュアルモードで文字の無いところにもカーソル移動を可能に
set virtualedit=block
"キーコードはすぐにタイムアウト
set notimeout
"マッピングは200ms待つ
set ttimeout ttimeoutlen=200
"スワップファイルを作らない
set noswapfile
"バックアップファイルを作らない
set nobackup
"undoファイルを作らない
set noundofile
"クリップボードを他のアプリと共有
set clipboard+=unnamedplus
"対応括弧を追加
set matchpairs& matchpairs+=<:>
"スペルチェック時に日本語を除外
set spelllang=en,cjk
"ファイルを開いた時に,カーソル位置を最後にカーソルがあった位置まで移動
autocmd BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
"}}}

"------------------------------------------------------------
"表示関係
"------------------------------------------------------------
"{{{
"行番号を表示
set number
"ルーラ表示
set ruler
"タブや改行を非表示
set nolist
"マルチバイト文字の表示をいいかんじに
set ambiwidth=double
"折り返し
set wrap
"長い行も表示
set display=lastline
"ステータスラインを常に表示する
set laststatus=2
"コマンドラインの高さを2行に
set cmdheight=2
"タイプ途中のコマンドを画面最下行に表示
set showcmd
"タイトル表示
set title
"カーソル行を可視化
set cursorline
"ビープの代わりにビジュアルベル(画面フラッシュ)を使う
set visualbell
"ビジュアルベルも無効化する
set t_vb=
"フォールディング設定
set foldmethod=marker
" デフォルト不可視文字は美しくないのでUnicodeで綺麗に
set listchars=tab:»-,trail:-,extends:»,precedes:«,nbsp:%,eol:"カーソルが内行の内容を隠蔽しない
set conceallevel=0
"補完メニューの幅
set pumheight=15
"カーソルの表示を変更
let $NVIM_TUI_ENABLE_CURSOR_SHAPE=2
"}}}
"mapping.vim
"Yの動作をDやCと同じにする
map Y y$
"<ESC>2回で検索後の強調表示を解除する
nnoremap <ESC><ESC> :nohl<CR><C-L>
"j k を表示行移動できるよう変更
nnoremap j gj
nnoremap k gk
" 検索後にジャンプした際に検索単語を画面中央に持ってくる
nnoremap n nzz
nnoremap N Nzz
nnoremap * *zz
nnoremap # #zz
nnoremap g* g*zz
nnoremap g# g#zz
".vimrcを開く
nnoremap ,ev :tabe $HOME/.vimrc<CR>
".vimrcを再読み込み
nnoremap ,rv :source $HOME/.vimrc<CR>
"ヤンクした文字列でカーソル位置の単語を置換
nnoremap <silent> cy  ce<C-r>0<ESC>:let@/=@1<CR>:noh<CR>
vnoremap <silent> cy  c<C-r>0<ESC>:let@/=@1<CR>:noh<CR>
nnoremap <silent> ciy  ciw<C-r>0<ESC>:let@/=@1<CR>:noh<CR>

"ウィンドウ幅の変更
nnoremap + 5<C-W>+
nnoremap _ 5<C-W>-
nnoremap > 5<C-W>>
nnoremap < 5<C-W><
"ウィンドウ移動に使用するのでsの元の機能を削除(ci)
nnoremap s <Nop>
"s + hjkl でウィンドウ間を移動
nnoremap sh <C-w>h
nnoremap sj <C-w>j
nnoremap sk <C-w>k
nnoremap sl <C-w>l
nnoremap sw <C-w>w
"ウィンドウそのものを移動
nnoremap sJ <C-w>J
nnoremap sK <C-w>K
nnoremap sL <C-w>L
nnoremap sH <C-w>H

"次のタブに移動
nnoremap sn  :tabn<CR>
"前のタブに移動
nnoremap sp  :tabp<CR>
"タブの作成
nnoremap st :<C-u>tabnew<CR>

"ビジュアルモードでインデント変更後に再選択
vnoremap <  <gv
vnoremap >  >gv
"インサートモード中でも隣のウィンドウに移動
inoremap <C-W><C-W> <ESC><C-W><C-W><ESC>
"filetypes.vim
"ファイルタイプに関係する設定
augroup filetypeds
    autocmd!
    autocmd BufNewFile,BufRead,BufReadPre *.{md,mkd,mark*} set filetype=markdown
    autocmd BufNewFile,BufRead,BufReadPre *.{json} set filetype=json conceallevel=0
    autocmd InsertEnter *.json setlocal conceallevel=0 concealcursor=
    autocmd InsertLeave *.json setlocal conceallevel=0 concealcursor=inc
    autocmd BufRead,BufNewFile,BufReadPre *.coffee   set filetype=coffee
    autocmd BufRead,BufNewFile *.scss set filetype=scss.css
    autocmd FileType scss set iskeyword+=-
augroup END