Defx

カテゴリー:  Tech タグ:  neovim software vim

Vim上で長らく使っていたファイラー、NERDTreeに変えて Defxという 新しいファイラーを試してみました。

Defxとは

DefxとはVim上で稼動するファイラーです。

VimFilerの作者であった @Shougo さんが昨年リリースされたものです。

私はずっと NERDTreeを使用してきましたが、NERDTreeよりも動作が高速であると いうので導入してみました。

たしかに起動やファイル操作の一つ一つが軽く機敏に動作しているという 印象です。

Defx

Defx

Defxのインストール

Defx自体は単体でも動作するようですが、NERDTreeでもやっていたように NERDFont対応にしたいと思います。

Git関連のステータスのアイコン対応と、defxのアイコンを設定できる 以下の3つのプラグインを導入します。以下は、 deinでの導入例です。

# --------------------------------------------
#  Setting for devicon
#  アイコン表示
# --------------------------------------------
[[plugins]]
repo = 'ryanoasis/vim-devicons'
hook_add = '''
    let g:webdevicons_enable_nerdtree = 1
    let g:webdevicons_enable_denite = 1
'''

# --------------------------------------------
#  Setting for defx
#  defx の Gitアイコン
# --------------------------------------------
[[plugins]]
repo ="kristijanhusak/defx-git"

# --------------------------------------------
#  Setting for defx
#  defx の アイコンカスタマイズ
# --------------------------------------------
[[plugins]]
repo ="kristijanhusak/defx-icons"

それぞれの設定は以下のようなコードです。

前提として NERD Fontなどフォントは導入されている必要があります。

" Setting for defx-git
call defx#custom#column('git', 'indicators', {
  \ 'Modified'  : '✹',
  \ 'Staged'    : '✚',
  \ 'Untracked' : '✭',
  \ 'Renamed'   : '➜',
  \ 'Unmerged'  : '═',
  \ 'Ignored'   : '☒',
  \ 'Deleted'   : '✖',
  \ 'Unknown'   : '?'
  \ })

" Setting for defx-icons
let g:defx_icons_enable_syntax_highlight = 0
let g:defx_icons_column_length = 2
let g:defx_icons_directory_icon = ''
let g:defx_icons_mark_icon = '*'
let g:defx_icons_copy_icon = ''
let g:defx_icons_move_icon = ''
let g:defx_icons_parent_icon = ''
let g:defx_icons_default_icon = ''
let g:defx_icons_directory_symlink_icon = ''

次にDefx自体のインストールです。

Deinでプラグインとして読み込みます。

# --------------------------------------------
#  Setting for defx
#  Filer
# --------------------------------------------
[[plugins]]
repo = 'Shougo/defx.nvim'
depends = ['defx-git' , 'defx-icons']

以下のようカスタマイズしています。NERDTreeに替えて、Control-N で起動するようにしています。

call defx#custom#column('icon', {
    \ 'directory_icon': '▸',
    \ 'opened_icon': '▾',
    \ 'root_icon': ' ',
    \ })

call defx#custom#column('filename', {
    \ 'min_width': 40,
    \ 'max_width': 40,
    \ })

call defx#custom#column('mark', {
    \ 'readonly_icon': '✗',
    \ 'selected_icon': '✓',
    \ })

call defx#custom#option('_', {
    \ 'winwidth' : 40,
    \ 'split' : 'vertical',
    \ 'direction' : 'topleft',
    \ 'buffer_name' : 'exploler',
    \ 'toggle' : 1,
    \ 'resume' : 1,
    \ 'columns': 'indent:git:icons:filename:type',
    \ 'show_ignored_files': 1,
    \ })
nnoremap <silent><C-n> :<C-u>Defx -split=vertical -winwidth=40 -search=`expand('%:p')` -direction=topleft `expand('%:p:h')`<CR>
    autocmd FileType defx call s:defx_my_settings()
    function! s:defx_my_settings() abort
      " Define mappings
      nnoremap <silent><buffer><expr> <CR>
  \ defx#is_directory() ?
  \   defx#do_action('open') :
      \   defx#do_action('multi', ['drop', 'quit'])
      nnoremap <silent><buffer><expr> c
      \ defx#do_action('copy')
      nnoremap <silent><buffer><expr> m
      \ defx#do_action('move')
      nnoremap <silent><buffer><expr> p
      \ defx#do_action('paste')
      nnoremap <silent><buffer><expr> l
      \ defx#do_action('open')
      nnoremap <silent><buffer><expr> E
      \ defx#do_action('open', 'vsplit')
      nnoremap <silent><buffer><expr> P
      \ defx#do_action('open', 'pedit')
      nnoremap <silent><buffer><expr> o
      \ defx#do_action('open_tree', 'toggle')
      nnoremap <silent><buffer><expr> K
      \ defx#do_action('new_directory')
      nnoremap <silent><buffer><expr> N
      \ defx#do_action('new_file')
      nnoremap <silent><buffer><expr> M
      \ defx#do_action('new_multiple_files')
      nnoremap <silent><buffer><expr> C
      \ defx#do_action('toggle_columns',
      \                'mark:indent:icon:filename:type:size:time')
      nnoremap <silent><buffer><expr> S
      \ defx#do_action('toggle_sort', 'time')
      nnoremap <silent><buffer><expr> d
      \ defx#do_action('remove')
      nnoremap <silent><buffer><expr> r
      \ defx#do_action('rename')
      nnoremap <silent><buffer><expr> !
      \ defx#do_action('execute_command')
      nnoremap <silent><buffer><expr> x
      \ defx#do_action('execute_system')
      nnoremap <silent><buffer><expr> yy
      \ defx#do_action('yank_path')
      nnoremap <silent><buffer><expr> .
      \ defx#do_action('toggle_ignored_files')
      nnoremap <silent><buffer><expr> ;
      \ defx#do_action('repeat')
      nnoremap <silent><buffer><expr> h
      \ defx#do_action('cd', ['..'])
      nnoremap <silent><buffer><expr> ~
      \ defx#do_action('cd')
      nnoremap <silent><buffer><expr> q
      \ defx#do_action('quit')
      nnoremap <silent><buffer><expr> <Space>
      \ defx#do_action('toggle_select') . 'j'
      nnoremap <silent><buffer><expr> *
      \ defx#do_action('toggle_select_all')
      nnoremap <silent><buffer><expr> j
      \ line('.') == line('$') ? 'gg' : 'j'
      nnoremap <silent><buffer><expr> k
      \ line('.') == 1 ? 'G' : 'k'
      nnoremap <silent><buffer><expr> <C-l>
      \ defx#do_action('redraw')
      nnoremap <silent><buffer><expr> <C-g>
      \ defx#do_action('print')
      nnoremap <silent><buffer><expr> cd
      \ defx#do_action('change_vim_cwd')
    endfunction

キーバインドについては、サンプルとして掲載されていたものをほぼ使用しちえます。 NERDTreeに慣れてしまっているので多少戸惑う場面もありますが、NERDTreeと違ってほとんどの 動作がワンキーで出来るようになっているので慣れるとこちらの方が素早く操作できそうです。

コメント

Comments powered by Disqus