vim - Is there a way to automatically preview a file when the cursor is placed over it in netrw listing? -
this not hitting ‘p’ when on file name , having same result placing cursor on it.
i haven't tested long, try write following code, somewhere in file sourced automatically vim (maybe in ~/.vim/after/plugin/my_netrw.vim):
augroup my_netrw au! au filetype netrw call s:netrw_setup() augroup end fu! s:netrw_setup() abort nno <buffer> <nowait> <silent> cop :<c-u>call <sid>toggle_auto_preview()<cr> nno <buffer> <nowait> <silent> [op :<c-u>call <sid>enable_auto_preview()<cr> nno <buffer> <nowait> <silent> ]op :<c-u>call <sid>disable_auto_preview()<cr> if get(g:, 'my_netrw_auto_preview', 0) call s:install_autocmd() endif endfu fu! s:install_autocmd() abort augroup my_netrw_auto_preview au! au cursormoved <buffer> call s:do_i_preview() augroup end endfu fu! s:enable_auto_preview() abort call s:install_autocmd() let g:my_netrw_auto_preview = 1 echo '[netrw auto-preview] on' call s:preview() endfu fu! s:disable_auto_preview() abort if exists('#my_netrw_auto_preview') autocmd! my_netrw_auto_preview augroup! my_netrw_auto_preview endif let g:my_netrw_auto_preview = 0 echo '[netrw auto-preview] off' sil! pclose endfu fu! s:toggle_auto_preview() abort call s:{exists('#my_netrw_auto_preview') ? 'disable' : 'enable'}_auto_preview() endfu fu! s:do_i_preview() abort if get(b:, 'my_last_preview', 0) !=# line('.') let b:my_last_preview = line('.') call s:preview() endif endfu fu! s:preview() abort let file = getline('.') if file =~# '^" ' return endif let file = b:netrw_curdir \ . (!exists('+shellslash') || &shellslash ? '/' : '\') \ . substitute(file, '\v[/*|@=]?%(\t.*)?$', '', '') if filereadable(file) call feedkeys('p', 'it') elseif isdirectory(file) let ls = systemlist('ls '.shellescape(file, 1)) let b:my_preview_ls = get(b:, 'my_preview_ls', tempname()) call writefile(ls, b:my_preview_ls, 'b') exe 'pedit '.b:my_preview_ls endif endfu it seemed want during limited time tested it, there may edge cases forgot.
inside netrw buffer, should install 3 mappings:
[openable automatic preview of files]opdisable itcoptoggle it
Comments
Post a Comment