Setting netrw like NERDTree
Asked Answered
S

7

28
  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?

Sarge answered 15/2, 2011 at 17:11 Comment(1)
I would complement/put together Nicks answer with: "resize" vi.stackexchange.com/questions/10988/toggle-explorer-window/…Foreshank
I
43

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_id = win_getid()
          exec expl_win_num . 'windo close'
          let prev_win_num = win_id2win(cur_win_id)
          if prev_win_num != 0
              exec prev_win_num . 'wincmd w'
          endif
      endif
      unlet t:expl_buf_num
  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
Ischia answered 12/4, 2011 at 14:33 Comment(2)
After opening a file, and toggling (to close), line 7 gives an error: E16: Invalid range: 2wincmd w, Vim 8.0.4. Any ideas how to resolve?Holofernes
@Holofernes That's because the previous code was wrong, the value of winnr will change when close is executed. It's edited to use window ID instead.Exchequer
S
26

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

Slapdash answered 14/4, 2014 at 20:3 Comment(1)
:Lexplore takes half of the current buffer size by default. To open a 30 character size buffer only, you can map Ll as such in my .vimrc : command! Ll Lexplore | vert res 30.Britska
L
11

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
Loydloydie answered 28/5, 2014 at 19:26 Comment(3)
After opening a file, toggling gives the error: line 8: E16: Invalid range: 2wincmd w. In fact this gives the error whenever toggling when the explorer isn't the active window. Possible some problem with VIM8?Holofernes
If you want filetype-based highlighting, check gitlab.com/bimlas/vim-highLoydloydie
@Loydloydie See my comment below Nick's answer. (Exchequer
S
8

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>
Species answered 5/7, 2018 at 20:11 Comment(3)
Great solution! Have you noticed the flash of the file contents before rendering the NETRW pane? Do you have an idea of what could be? Thanks again!Leeway
@Leeway this is probably due to Vim's bell. Here's how to deactivate it (set it to "visual" then tell it to do nothing) set visualbell set t_vb=Passage
I like your function, been using it a while! However, you don't want to remap <c-o>, that's opposite of <c-i> (pair of mappings I use a lot). Last, you remap <c-a> in any filetype, try that instead!Passage
C
7

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.

Carboloy answered 5/6, 2012 at 17:28 Comment(1)
Yep - I now do this myself, and have updated my answer accordingly.Ischia
M
1
" Toggle Vexplore with Ctrl-E

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

Simplify

Muliebrity answered 11/11, 2020 at 21:31 Comment(0)
U
0

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

" ---------------------------------------------------------------
Upstroke answered 13/6, 2021 at 17:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.