vimnetrw

Write this in VimScript : "If Lexplore opened a file(anyfile) then close Lexplore"


I want to: Automaticaly close Lexpore after opening a file for better experience but I don't know vim script. It shouldn't be hard, I suppose. I will appreciate any comment!

I'm now learning vim script and trying to do it. here is where I've got by now:

:autocmd <Lexplore> * :<Lexplore-exit>

or

if <command-mode> == "Lexplore *.*"
    excute \\close last buffer. (because now I am in new buffer)

I just need to know how to say "RUN" / "RUNED" in script then i use regex for anyfile and i need to say "CLOSE".

The truth is I'm actually Hopeless! :)))))))))))))


Solution

  • There are other avenues to try before going full-on with VimScript. In this case, a simple mapping would probably be enough.

    Basically, the right-hand side of a mapping is just a macro, a sequence of commands that you would type yourself, which makes mappings a very good and approchable way to discover Vim automation.

    Assuming a simple layout, with only one window, how do you do what you describe manually?

    1. You do :Lexplore and browse around.
    2. In the Netrw window, you press <CR> to open the file.
    3. Then you go back to the previous window (the Netrw window) with <C-w>p.
    4. You close the window with <C-w>q.

    Steps 2-4 above are easy to turn into a mapping (<key> is a placeholder, use what you want):

    nmap <key> <CR><C-w>p<C-w>q
    

    But that mapping will be available everywhere, which may not be a good idea. Enters the notion of filetype plugins:

    1. Create these directories if they don't already exist:

      ~/.vim/after/ftplugin/                          (Unix-like systems)
      %userprofile%\vimfiles\after\ftplugin\          (Windows)
      
    2. Create a netrw.vim file in the directory above:

      ~/.vim/after/ftplugin/netrw.vim                 (Unix-like systems)
      %userprofile%\vimfiles\after\ftplugin\netrw.vim (Windows)
      
    3. Put the mapping above in that file:

      nmap <buffer> <key> <CR><C-w>p<C-w>q
      

      The <buffer> tells Vim to only define that mapping in Netrw.

    This is the simplest approach, which may or may not work for you.


    A few notes regarding your comments…