5

I want to use netrw as a project drawer. Not for opening/closing files or general file management, but only as a way to get a visual overview over the project structure.

I open netrw automatically when I open gVim, and keep it open to the left.

When I have several splits open, I'd like the netrw "project drawer" to keep in sync with the active buffer. Is this possible? Additionally, if the current file is in a directory with a lot of files, I want netrw to scroll so that the current file is visible.

How can I accomplish this?

I've previously gotten this working with NERDTree using NERDTreeFind. But I found NERDTree to slow down vim too much. I've also gotten it kind of working in defx, but with too much flickering. So now I'm putting my hope to netrw...

Often when NERDTree and/or netrw are discussed the "oil and vinegar" article is mentioned. So ofcourse I have read it :)

It mentions that fuzzyfinding files with something like CtrlP is faster. And I agree, and that's what I use. But I still want my project drawer for a visual overview of where the current file is in the file tree, and the overall layout of the project.

  • Related: 1, 2 – D. Ben Knoble Jun 13 '19 at 12:28
  • @D.BenKnoble 1: See update in my post. 2: I'm not using tabs, so no need to worry about those :) – Tobbe Jun 13 '19 at 13:27
  • to be clear, its an interesting question—I just wanted to point future readers to resources. Hope you get an answer – D. Ben Knoble Jun 13 '19 at 13:40
1

There are multiple ways to solve your problem. However, you just need to have an open mind because often the solution is changing your workflow.

I think the reason people have a hard time moving to vim is that most try to fit the workflow from their current editor/IDE and try to recreate it in vim. However, the beauty of vim is it's very programmable, you can edit to your heart's contents. But oftentimes, you only need to change your workflow which tends to be more efficient since that's how vim was designed.

I want to use netrw as a project drawer. Not for opening/closing files or general file management, but only as a way to get a visual overview of the project structure.

So quoting Bram (creator of vim)

An editor is for editing text. An e-mail program is for sending and receiving messages. An Operating System is for running programs. Each program has its own task and should be good at it. The power comes from having the programs work together. taken from seven habits

So you have the full arsenal of the terminal to use it. So programs like grep, find, fd, ripgrep, and tree comes to mind.

For example, you could do :r !rg --files | rg js on an empty buffer, and use it more like a table of contents. Where you can:

  1. Filter it out more by the full power of vim, :g/pattern/d, dd, etc.
  2. Do a quick search via normal /pattern
  3. Do a gf to jump to the current file under the cursor

With this, you can visually see your project. But if continuous lines of filename displease you. You could use tree instead, :r tree -L 2 for example and do a gf on the filenames too.

I open netrw automatically when I open gVim, and keep it open to the left.

netrw has :Lex which also accepts N to size it and directory name to browse. (:h :Lex)

So :30Lex %:h would open a file drawer on the left with the current file's directory open.

%:h is a shorthand for the current file's directory.

Additionally, if the current file is in a directory with a lot of files, I want netrw to scroll so that the current file is visible.

If you do :Ex it'll replace the current buffer with the directory, and you could do a quick search to highlight the file via / if it has multiple files.

If doing a search, displeases you because it's an additional command; You could get away with it by combining those commands to a single mapping.

eg: map <silent> <Leader>ex :Ex <bar> :sil! /<C-R>=expand("%:t")<CR><CR>

Also, there's a very tiny plugin that Tim Pope created, which only augments the already built-in (netrw). vim-vinegar

It just changes a few behaviors of netrw, via custom mappings and sane defaults (like turning off the banner by default). I suggest you try it out since it's only around 100 lines of code approx compared to nerdtree for example which has thousand(s)? of lines of code. This gets loaded every time you open vim.

It actually has a search function, which selects the file automatically when you press - (shortcut for going up a directory). This replaces the custom binding I mentioned. Normally the - binding is only available if you're in netrw buffer. vim-vinegar extends this binding in normal mode, so you can easily switch to the file-tree mode by a single key -

map <silent> <Leader>ex :Ex <bar> :sil! /<C-R>=expand("%:t")<CR><CR>

| improve this answer | |
  • Also, welcome to Vi and Vim! :) – D. Ben Knoble Aug 2 at 3:02
  • 1
    Thanks buddy for welcoming! 👋 Just returning the favor of helping people with vim. The community helped me a lot. – chriz Aug 2 at 5:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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