tpope / vim-vinegar

vinegar.vim: Combine with netrw to create a delicious salad dressing
https://www.vim.org/scripts/script.php?script_id=5671
2.23k stars 87 forks source link

Possible to _replace_ current buffer with Netrw? #117

Closed JakeElder closed 3 years ago

JakeElder commented 4 years ago

This plugin adds the - key binding which opens netrw in the current pane, with the currently open file selected.

I find I often want to perform this operation, but close the buffer I came from. I've looked through the source to attempt it and am struggling.

IE, if I know I'm finished working on a file, then navigate to another from the path of that file.

I lifted the s:seek function from this plugin and tried some variations using it but I'm not doing well..

" <leader>u Goes to current directory and closes buffer
" Taken from vim-vinegar
function! s:seek(file) abort
  if get(b:, 'netrw_liststyle') == 2
    let pattern = '\%(^\|\s\+\)\zs'.escape(a:file, '.*[]~\').'[/*|@=]\=\%($\|\s\+\)'
  else
    let pattern = '^\%(| \)*'.escape(a:file, '.*[]~\').'[/*|@=]\=\%($\|\t\)'
  endif
  call search(pattern, 'wc')
  return pattern
endfunction

function! s:replace_with_netrw()
  let dir = expand('%:p:h')
  execute "enew"
  execute "Explore " . dir
  call s:seek(expand('#:t'))
  execute "bd #"
endfunction

map <leader>u :call <SID>replace_with_netrw()<cr>

Can anyone help?

OpakAlex commented 3 years ago

+1

tpope commented 3 years ago

If I was going to attempt this, I would let bufnr = bufnr(''), navigate to the new location, and exe bufnr 'bdelete'. I would not rely on # as sometimes a hidden keepalt can screw you.