Skip to content

Instantly share code, notes, and snippets.

@hrsh7th
Last active March 8, 2019 08:25
Show Gist options
  • Save hrsh7th/d79fe665c53b8186fe883a3a271579e8 to your computer and use it in GitHub Desktop.
Save hrsh7th/d79fe665c53b8186fe883a3a271579e8 to your computer and use it in GitHub Desktop.
defx close_tree like common file explorer.
autocmd FileType defx call s:defx_setting()
function! s:defx_setting() abort
nnoremap <silent><buffer><expr>h defx#do_action('call', 'DefxCloseTree')
nnoremap <silent><buffer><expr>l defx#is_directory() ? defx#do_action('open_tree') . 'j' : defx#do_action('open', 'edit')
endfunction
function! DefxCloseTree(_)
" candidate is opend tree?
if defx#is_opened_tree()
return defx#call_action('close_tree')
endif
" parent is root?
let s:candidate = defx#get_candidate()
let s:parent = fnamemodify(s:candidate['action__path'], s:candidate['is_directory'] ? ':p:h:h' : ':p:h')
if s:trim_right(s:parent, '/') == s:trim_right(b:defx.paths[0], '/')
return defx#call_action('cd', ['..'])
endif
" move to parent.
call defx#call_action('search', s:parent)
" if you want close_tree immediately, enable below line.
" call defx#call_action('close_tree')
endfunction
function! s:trim_right(str, trim)
return substitute(a:str, printf('%s$', a:trim), '', 'g')
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment