1

I want to create a netrw sidebar whenever I open a new tab. I tried this:

au TabNew,VimEnter *
    \ | :Vex
    \ | :vert res 40

but this makes a layout like this when I :tabe file2:

tab1:          tab2:
netrw|file1    file2|file1

It works fine with TabEnter, but then it opens a new sidebar whenever I switch tabs. How can I fix this?

2

1 Answer 1

0

Not sure why you're having the odd behavior with TabNew...

But one possible way to implement a netrw window per tab is to use a TabEnter event together with the :Lexplore command, and some detection of whether :Lex is already opened in a window using the t:netrw_lexbufnr per-tab variable.

function! OpenLexplore()
  if bufwinid(get(t:, 'netrw_lexbufnr')) >= 0
    " If :Lexplore is already opened in a window
    " in the current tab, do nothing.
    return
  endif

  " Otherwise, open :Lexplore, resize it, and
  " switch the cursor back to the previous window.
  Lexplore
  vertical resize 40
  wincmd p
endfunction

augroup tab_lexplore
  autocmd!
  autocmd TabEnter,VimEnter * call OpenLexplore()
augroup END
3
  • Thanks, this works great! What's the difference between Lexplore and Vexplore?
    – Stonks3141
    Commented Dec 21, 2021 at 22:00
  • @SamNystrom See :help netrw-:Lexplore for all the details... In short, it toggles the pane on the left (multiple :Vexplores will create multiple netrw panes) and it sets it up so that when you open a file from that pane, it will open in a separate existing window (preserving the netrw pane around even while you're browsing and opening files from it.)
    – filbranden
    Commented Dec 21, 2021 at 22:56
  • 1
    Yeah, that seems better for what I want to use it for.
    – Stonks3141
    Commented Dec 21, 2021 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.