1

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

ju pasknow
  • 13
  • 4

1 Answers1

2

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.
romainl
  • 186,200
  • 21
  • 280
  • 313
  • Thank you very much!!! it is right answer except I open Lexplore file in new tab not new windows so I changed that part. its gtq. – ju pasknow Dec 02 '21 at 22:15
  • But it work when I have 1 file and opening a second file not when I have 2 files and opening a third file. Is there a alternative to gt that says "Go to last active tab" ?? – ju pasknow Dec 02 '21 at 22:30
  • Actually I found a solution in [link](https://stackoverflow.com/a/2120168/17351029) and it totally worked!! I put that code in my ~/nvim/after/ftplugin/netwr.vim – ju pasknow Dec 02 '21 at 22:42
  • What if I wanted to map actuall ?? It is not working! `nmap , ltq` is working but when I replace `,` with `` it is not. Now I'm studying mapping in vim. – ju pasknow Dec 02 '21 at 23:12
  • I know i should NOT use tabs that way in vim. please dont say that :))))) I'm new but speedy. I would appreciate A-level resources to learn. – ju pasknow Dec 02 '21 at 23:31
  • **Note** this open-close method make buffer numbers unordered. Every file I open with Explore skips a buffer number. like first file is 1 second file is 3 third file is 5. Actually I will stick to the defaults and make a Explore buffer in my workflow! when you change one thing it effects other stuff which they effect other stuff..... . – ju pasknow Dec 03 '21 at 00:36
  • Yes I'm pretty much affected by VSCode and stuff. I'm struggling to make vim like that and waste so much time and effort on that. I've picked up couple of famous VIM books but they just teach shortcuts not thinking model. vim guides are simillar. they just say what a command does. Can you recommend any resources for me to stop looking back for my old IDE and start learning to do VIM as it's desinged for. I'm sorry if I am being annoying but I need your guide!!! ty. I'm reading your and other pros answers in stackoverflow and taking notes but its all over the place. Not focuced in one thing. – ju pasknow Dec 03 '21 at 21:14
  • Vim comes with two built-in manuals: *the reference manual*, which is exhaustive but terse and doesn't make much sense if one doesn't have the fundamentals right, and *the user manual*, which is an easy to follow tutorial with each section building up on what you learned in previous section. The latter is the only valuable resource for learning Vim. `:help user-manual`. – romainl Dec 04 '21 at 08:30