vimnetrw

How to mimic Lexplore behavior in Vim 7.4


This topic in SE has confirmed that I cannot use Lexplore in Vim 7.4 (which is what I'm currently using). I cannot download plug-ins as my workstation is in a secure environment, and unless I request our IT to upgrade Vim (which is very difficult), I'm stuck with finding workarounds to mimic Lexplore.

Any help on how I can mimic Lexplore in Vim is much appreciated. Thank you in advance!


Solution

  • How faithful do you want your :Lexplore emulation to be?

    A superficial "open a slim vertical Netrw window on the left side of the current window" is as simple as:

    :20Vexplore!
    

    But you may want that slim window to take the full height of the screen. Moving the window to the far left side of the screen is easy enough with an additional :help :wincmd:

    :20Vexplore!|wincmd H
    

    Note that 20 is a percentage, here, not a number of columns.

    But, here, the width of the window is lost in the process. In my quick tests, neither :set noequalalways nor :set winfixwidth appear to have any effect on that, which is surprising, honestly. But you can still use :help vertical-resize and a bit of calculation:

    :Vexplore!|wincmd H|execute 'vertical resize ' .. &columns / 100 * 20
    

    Let's turn that into a custom mapping:

    function! LexploreOpen()
        Vexplore
        wincmd H
        execute 'vertical resize ' .. &columns / 100 * 20
    endfunction
    nnoremap <F5> :<C-u>call LexploreOpen()<CR>
    

    Now you can press <F5> to open your :lexplore lookalike.

    But what really sets :Lexplore apart from :help Vexplore is that it is a toggle: you do :Lex to open it and :Lex again to close it.

    You can use a global variable for that:

    function! LexploreToggle()
        if exists('g:FakeLexploreIsOpened')
            wincmd t
            close
            unlet g:FakeLexploreIsOpened
        else
            Vexplore
            wincmd H
            execute 'vertical resize ' .. &columns / 100 * 20
            let g:FakeLexploreIsOpened = 1
        endif
    endfunction
    nnoremap <F5> :<C-u>call LexploreToggle()<CR>
    

    Setting a window-local option and closing the window that has it set would be cleaner, IMO, but I don't remember what functions were available back then for that. See if :help getwinvar() can help.

    Anyway, this should give you a reasonable approximation of :Lexplore but the original accepts a path, for starter, and the snippet above doesn't. And it is entirely possible that the original command does other things, like setting various useful internal Netrw variables and so on. This means that the closer you want to get from the real :Lexplore, the more involved it will be. For a full clone of :Lexplore I'm afraid you will have to look at Netrw's code.

    Note that there is no fundamental difference between the snippet above and a "plugin" so, if you can get that snippet into your secure environment, then you can also probably get a recent Netrw.