How can I close a Netrw :Explore buffer and return to the buffer I was last in?

517 views
Skip to first unread message

Ivan

unread,
Jan 21, 2014, 10:30:26 PM1/21/14
to vim...@googlegroups.com
I'm trying to write a function that will let me toggle a Netrw Explore window open/closed in the current window. Opening is easy, but closing (without killing the window/split) is a problem. Using <C-6> doesn't work if there's no alternate buffer, and my current implementation yields unpredictable results. Has anyone got an idea how I could pull this off? What I have so far, below:

fun! ExToggle(dir)
if &filetype != "netrw"
call ExOpen(a:dir)
else
call ExClose()
endif
endf

fun! ExOpen(dir)
exe "Explore " . a:dir
let g:netrw_browse_split=0 " open files in current window
endf

fun! ExClose()
while &filetype == "netrw"
exe "normal! \<C-O>"
endw
endf

Matteo Cavalleri

unread,
Jan 22, 2014, 3:18:14 AM1/22/14
to vim...@googlegroups.com
you can try something like this

fun! ExOpen(dir)
let g:last_bufnr = bufnr('%')


exe "Explore " . a:dir
let g:netrw_browse_split=0 " open files in current window
endf

fun! ExClose()
if &filetype == "netrw"
exe ':b' . g:last_bufnr
endif
endf

Matteo Cavalleri

unread,
Jan 22, 2014, 3:22:41 AM1/22/14
to vim...@googlegroups.com

> you can try something like this
>

btw if you want to open multiple netrw windows, putting bufnr in a global variable is obviously wrong, you'll need to do a bit more work i guess.

Charles Campbell

unread,
Feb 6, 2014, 5:21:44 PM2/6/14
to vim...@googlegroups.com
Ivan wrote:
> I'm trying to write a function that will let me toggle a Netrw Explore window open/closed in the current window. Opening is easy, but closing (without killing the window/split) is a problem. Using <C-6> doesn't work if there's no alternate buffer, and my current implementation yields unpredictable results. Has anyone got an idea how I could pull this off? <snip>
Hello!

Please try netrw v151a; I've extended the :Rex command to now mean
"return to explorer from some file" as before plus "return from explorer
to previous file".

In other words, after installing v151a of netrw, type :Rex to return.

Regards,
C Campbell

Reply all
Reply to author
Forward
0 new messages