8

I can make netrw use the tree view by default by adding this to my vimrc:

let g:netrw_liststyle=3

But I can't figure out how to expand sub-directories by default, is there a way?

3 Answers 3

2

Reading all subdirectories would be a costly operation, and slow down the opening of netrw considerably. As the tree display is just one (non-default) mode of several display modes, that's probably the reason why there's no such command. I tried something like this (triggering the <Enter> mapping on each directory line to open it):

:global/^│ /exe "normal \<CR>"

But it unfortunately didn't work.

Plugin alternative

The NERDTree plugin is similar to netrw, but it is built around a tree-style display (in a sidebar). As such, it also has commands to open subtrees (but those are just as slow as with netrw).

1
  • Thanks for answering - I was wondering if there was something like the command you tried. I'm trying to get away from plugins because I work with many different servers and netrw is there in the default install.
    – user12810
    May 16, 2015 at 21:00
0

This works:

function ExpandAllDirectories()
  call cursor(0,0)
  let found = 1
  while found != 0
    " match lines with expand-able directories
    " do not wrap around to the start of the file
    let found = search('^|.*\/$', 'W')
    if found != 0
      execute "normal \<CR>"
    endif
  endwhile
endfunction

This will expand all collapsed (sub)directories.

To use the above code, save it to a file, e.g. xpand.vim. Open a folder with netrw (maybe one with not too much stuff in it!), e.g. $ vim /some/folder/.

Make sure netrw is showing a tree style directory listing with all subfolders closed (the default/initial tree view). Either have let g:netrw_liststyle= 3 in your vimrc, or hit i until you see the tree style directory listing. See :help netrw-i for details.

Anyway, once you're in Vim and looking at a tree, use :source xpand.vim to load the code, then :call ExpandAllDirectories() to run it.


I'm guessing NERDTree is better, I just wanted to make it work with netrw. It is indeed slow... lemme know if I'm doing something silly (besides refusing to use NERDTree, apparently). It's probably just that (my) Vimscript is slow. The tree command line program is quite fast, so certainly this approach can be optimized too (perhaps in another language).

0

I have this in ~/.vimrc:

function SideDirTree()
    Lexplore
    if bufname(winbufnr(0)) == 'NetrwTreeListing'
        2
        "while search('^\([|│] \)\+.\+\/$', '', line('$'))  " (no recursion limit)
        while search('^\([|│] \)\{1,3}[^|│].*/$', '', line('$'))  " (recursion limited to 3 subdirs)
            execute "normal \<CR>"
        endwhile
        3
    endif
endfunction
noremap <F9> :call SideDirTree()<CR>

Working on ViM/GViM 8.2 (the other answers didn't work for me).

It's hacky and not fast but essentially does what you're asking.

You must log in to answer this question.