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:
- Filter it out more by the full power of vim,
:g/pattern/d
, dd
, etc.
- Do a quick search via normal
/pattern
- 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>