I would like to be able to use my mouse to scroll and click on directories to expand them in netrw. I don't want the mouse to be on for any other filetype/buffertype, so that I can still highlight/copy/paste as normal. I tried a couple ways:

  1. I created a ~/.vim/plugin/netrw.vim script:
setlocal mouse=n
nnoremap <buffer> <LeftMouse> <LeftMouse> :norm 0 <CR>

But this leaves the mouse on even after I switch windows or close the netrw window. I also tried putting the netrw.vim script in ~/.vim/ftdetect/ to see if it makes a difference, but it doesn't.

  1. I remapped my mouse buttons in my .vimrc file to shift-mouse_button hoping this answer would allow me to toggle off mouse only while that button is pressed, but it didn't.
set mouse=n
nnoremap <LeftMouse> <S-LeftMouse>
nnoremap <RightMouse> <S-RightMouse>
  1. I also tried including this in my .vimrc, on top of what I wrote above in (1):
function! TurnOffMouse()
   set mouse=

noremap <LeftMouse> :call TurnOffMouse() <CR><LeftMouse>
noremap <RightMouse> :call TurnOffMouse() <CR><RightMouse>

After I use netrw with mouse and switch to a non-netrw window, I have to click the right mouse button twice to get the normal context menu to come up for copy and paste. Is there a way to do it in one click? The left mouse button also doesn't call turn off mouse after the netrw script is loaded.

  1. This answer uses map-expression and checks filetype, but would doing something like this work (for example only remapping left mouse if filetype == netrw) if mouse isn't set to begin with?

Seems like if mouse isn't set, then it doesn't matter what my custom mouse remappings are, they won't be called.

1 Answer 1


Riffing off of this answer, I added these lines to my .vimrc to enable mouse support only when starting, entering, or leaving netrw buffer.

function! NetrwMouseOn()
     set mouse=n

function! NetrwMouseOff()
     set mouse=

au FileType netrw :call NetrwMouseOn()
au FileType netrw au BufEnter <buffer> :call NetrwMouseOn() 
au FileType netrw au BufLeave <buffer> :call NetrwMouseOff()

" Map every left click to enter button
au FileType netrw nmap <buffer> <LeftMouse> <LeftMouse> <CR> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.