ju pasknow
ju pasknow

Reputation: 13

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! :)))))))))))))

Upvotes: 1

Views: 438

Answers (1)

romainl
romainl

Reputation: 196546

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…

  • Buffer numbers are implementation detail. They don't matter at all in your day-to-day work so you shouldn't think too much about them. Buffer names are far more useful and much easier to think about.
  • The "sidebar file explorer" UX pattern is a familiar one that newcomers like to transfer from their previous editor but it necessitates a lot of fighting against Vim itself to "work". Netrw's take on the pattern is different, yes, and it might take time to adjust to, but it fits much better within the overall experience. Hint: give up on :Lexplore. :Explore, :Sexplore, :Vexplore, and :Texplore are much more useful.
  • As you learn more, you will soon discover that file explorers, whatever UX pattern they follow, are not that useful to begin with.
  • Netrw already has a mapping for opening a file in a new tab page: :help netrw-t.

Upvotes: 2

Related Questions