Setting netrw like NERDTree

31.6k views Asked by At
  1. I used nmap <silent> <f2> :NERDTreeToggle<cr> to toggle nerdtree window. How can I do the same with netrw?

  2. nerdtree window is not shown in the buffer list(:ls). netrw is listed in the buffer list. How can I make it not listed?

  3. :bn command works but :bp command does not work in the netrw window. Is this a bug?

7

There are 7 answers

1
Nick On BEST ANSWER

The 'Vexplore' command opens a vertical directory browser. You can build on this by adding the following code to your .vimrc file to toggle the vertical browser with Ctrl-E (for example):

" Toggle Vexplore with Ctrl-E
function! ToggleVExplorer()
  if exists("t:expl_buf_num")
      let expl_win_num = bufwinnr(t:expl_buf_num)
      if expl_win_num != -1
          let cur_win_nr = winnr()
          exec expl_win_num . 'wincmd w'
          close
          exec cur_win_nr . 'wincmd w'
          unlet t:expl_buf_num
      else
          unlet t:expl_buf_num
      endif
  else
      exec '1wincmd w'
      Vexplore
      let t:expl_buf_num = bufnr("%")
  endif
endfunction
map <silent> <C-E> :call ToggleVExplorer()<CR>

The code above tries to open the Explorer window on the left hand side of the screen at all times; I use it with multiple split vertical windows open.

[OPTIONAL] You might like to add the following lines to your .vimrc to improve the browsing experience:

" Hit enter in the file browser to open the selected
" file with :vsplit to the right of the browser.
let g:netrw_browse_split = 4
let g:netrw_altv = 1

" Change directory to the current buffer when opening files.
set autochdir
1
Xabierrek On

Actually,

let g:netrw_browse_split = 4
let g:netrw_altv = 1

works best for me.

 *g:netrw_browse_split* when browsing, <cr> will open the file by:
                =0: re-using the same window
                =1: horizontally splitting the window first
                =2: vertically   splitting the window first
                =3: open file in new tab
                =4: act like "P" (ie. open previous window)
                    Note that |g:netrw_preview| may be used
                    to get vertical splitting instead of
                    horizontal splitting.

I think the best behavior is described by option 4. By pressing enter, file is opened on the other split, avoiding an overpopulation of splits.

2
bimlas On

I just did some improvements on Nick's solution which fixes:

  • opens 100% high window (independent from window splits)
  • :Lexplore opens it on left side, :Lexplore! on the right
  • listing the directory of the current file (even on remote directories)

Put these lines to the end of your .vimrc:

com!  -nargs=* -bar -bang -complete=dir  Lexplore  call netrw#Lexplore(<q-args>, <bang>0)

fun! Lexplore(dir, right)
  if exists("t:netrw_lexbufnr")
  " close down netrw explorer window
  let lexwinnr = bufwinnr(t:netrw_lexbufnr)
  if lexwinnr != -1
    let curwin = winnr()
    exe lexwinnr."wincmd w"
    close
    exe curwin."wincmd w"
  endif
  unlet t:netrw_lexbufnr

  else
    " open netrw explorer window in the dir of current file
    " (even on remote files)
    let path = substitute(exists("b:netrw_curdir")? b:netrw_curdir : expand("%:p"), '^\(.*[/\\]\)[^/\\]*$','\1','e')
    exe (a:right? "botright" : "topleft")." vertical ".((g:netrw_winsize > 0)? (g:netrw_winsize*winwidth(0))/100 : -g:netrw_winsize) . " new"
    if a:dir != ""
      exe "Explore ".a:dir
    else
      exe "Explore ".path
    endif
    setlocal winfixwidth
    let t:netrw_lexbufnr = bufnr("%")
  endif
endfun

Suggested options to behave like NERDTree:

" absolute width of netrw window
let g:netrw_winsize = -28

" do not display info on the top of window
let g:netrw_banner = 0

" tree-view
let g:netrw_liststyle = 3

" sort is affecting only: directories on the top, files below
let g:netrw_sort_sequence = '[\/]$,*'

" use the previous window to open file
let g:netrw_browse_split = 4
0
Kunsteak On

As a similar and simpler aprroach to Nick's, you could make it toggleable (and very NERDTree-like) with F9 with this in your .vimrc:

" ---------------------------------------------------------------

" File Explorer start

let g:netrw_banner = 0
let g:netrw_liststyle = 3
let g:netrw_browse_split = 4
let g:netrw_altv = 1
let g:netrw_winsize = 15

" Toggle Vexplore with F9

map <silent> <F9> :Lexplore<CR>

" File Explorer end

" ---------------------------------------------------------------
3
Alex Kosh On

Toggle function

Here is my version of toggle function, based on Nick's answer. Now you can use hotkey from any pane, not only from netrw's pane. In Nick's version it causes an error, also I did some code cleanup and remap it to Ctrl-O, because Ctrl-E is used by default to scroll down by one line.

" Toggle Vexplore with Ctrl-O
function! ToggleVExplorer()
    if exists("t:expl_buf_num")
        let expl_win_num = bufwinnr(t:expl_buf_num)
        let cur_win_num = winnr()

        if expl_win_num != -1
            while expl_win_num != cur_win_num
                exec "wincmd w"
                let cur_win_num = winnr()
            endwhile

            close
        endif

        unlet t:expl_buf_num
    else
         Vexplore
         let t:expl_buf_num = bufnr("%")
    endif
endfunction

map <silent> <C-O> :call ToggleVExplorer()<CR>

Variable "t:expl_buf_num" is global for current tab, so you can have one Explorer per tab. You can change it to "w:expl_buf_num" if you want to be able to open Explorer in every window.

Keep focus in Explorer

Also I like to have this at my .vimrc:

" Open file, but keep focus in Explorer
autocmd filetype netrw nmap <c-a> <cr>:wincmd W<cr>
2
Charles Campbell On

Starting with netrw v150, there's :Lexplore, which will toggle a netrw window on the left-hand side.

0
DDD On
" Toggle Vexplore with Ctrl-E

function! ToggleVExplorer()
      Lexplore
      vertical resize 30
endfunction
map <silent> <C-E> :call ToggleVExplorer()<CR>

Simplify