I used
nmap <silent> <f2> :NERDTreeToggle<cr>
to toggle nerdtree window. How can I do the same with netrw?nerdtree window is not shown in the buffer list(
:ls
). netrw is listed in the buffer list. How can I make it not listed?:bn
command works but:bp
command does not work in the netrw window. Is this a bug?

- 2,547
- 4
- 24
- 30
-
Do not forget to look at preview before posting question or answer. You got all `<...>` constructs missing. – ZyX Feb 15 '11 at 20:22
-
I would complement/put together Nicks answer with: "resize" https://vi.stackexchange.com/questions/10988/toggle-explorer-window/26029#26029 – alnavegante Jun 19 '20 at 07:18
7 Answers
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

- 8,480
- 3
- 37
- 33
-
6After 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? – ideasman42 Sep 15 '16 at 17:08
Starting with netrw
v150, there's :Lexplore
, which will toggle a netrw
window on the left-hand side.

- 2,705
- 2
- 30
- 34

- 351
- 3
- 2
-
-
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`. – Paul Rougieux Jun 17 '21 at 10:20
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
-
2After 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? – ideasman42 Sep 15 '16 at 17:14
-
If you want filetype-based highlighting, check https://gitlab.com/bimlas/vim-high – bimlas Dec 10 '18 at 07:31
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,206
- 2
- 19
- 18
-
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! – betoharres Jun 03 '19 at 21:23
-
1@betoharres 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=` – Biggybi Jun 17 '19 at 10:07
-
I like your function, been using it a while! However, you don't want to remap
, that's opposite of – Biggybi Sep 10 '19 at 19:00(pair of mappings I use a lot). Last, you remap in any filetype, try [that](https://stackoverflow.com/a/54108005/6651927) instead!
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.

- 79
- 1
- 2
" Toggle Vexplore with Ctrl-E
function! ToggleVExplorer()
Lexplore
vertical resize 30
endfunction
map <silent> <C-E> :call ToggleVExplorer()<CR>
Simplify

- 471
- 1
- 5
- 13
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
" ---------------------------------------------------------------

- 1