2

I want to achieve two effects about using the netrw plugin for vim.

EFFECT ONE:

I have a test directory.Here is its directory structure:

MINGW64 ~/test
$ ll
total 1
drwxr-xr-x 1 uooki2 197121  0 一月   18 18:37 dir1/
drwxr-xr-x 1 uooki2 197121  0 一月   18 18:37 dir2/
drwxr-xr-x 1 uooki2 197121  0 一月   18 18:37 dir3/
-rw-r--r-- 1 uooki2 197121 13 一月   18 18:38 file1

I can open the test directory via vim ,And vim displays the directory tree structure of this directory in a window.

$ vim test

Just like this:

" ============================================================================
" Netrw Directory Listing                                        (netrw v156)
"   /c/Users/uooki2/test
"   Sorted by      name
"   Sort sequence: [\/]$,\<core\%(\.\d\+\)\=\>,\.h$,\.c$,\.cpp$,\~\=\*$,*,\.o$,\.obj$,\.info$,\.swp
"   Quick Help: <F1>:help  -:go up dir  D:delete  R:rename  s:sort-by  x:special
" ==============================================================================
../
./
dir1/
dir2/
dir3/
file1
~
~

But,i hope that vim can open two windows ,one on the left and one on the right ; and the window on the left shows the directory tree structure of the test directory, and the right side is a blank window. And the width of the left window is 30 percent of the width of the right window. Just like this:

" ==================|  1
" Netrw Directory Li|~
"   /c/Users/uooki2/|~
"   Sorted by      n|~
"   Sort sequence: [|~
"   Quick Help: <F1>|~
" ==================|~
../                 |~
./                  |~
dir1/               |~
dir2/               |~
dir3/               |~
file1               |~
~                   |~
~                   |~
~                   |~
~                   |~
~                   |~
~                   |~
~                   |~
~                   |~
~                   |~
~                   |~
~                   |~
~                   |~
~                   |~
~                   |~
~                   |~
~                   |~
~                   |~
~                   |~
~                   |~
2          test 0x2> 3          [unnamed] 0x0       0,0-1         <a

what should i do to achieve this effect?

EFFECT TWO:

I opened the file1 file in bash by the command: vim file1.

$ vim file1

Vim open the file1 file in a window. As follows:

 1 hello world!
~
~
~
~
~
~
~
~
~
~
~
~
1          file1                           0x68      1,1           all

But,this is not what i want. I want that vim can open two windows ,one on the left and one on the right ; and the window on the left shows the directory tree structure of the directory where the file1 file is located, and the right side shows the content of the file1 file. And the width of the left window is 30 percent of the width of the right window. Just like this:

"   /c/Users/uooki2/|  1 hello world!
"   Sorted by      n|~
"   Sort sequence: [|~
"   Quick Help: <F1>|~
" ==================|~
../                 |~
./                  |~
dir1/               |~
dir2/               |~
dir3/               |~
.file1.swo          |~
file1               |~
.file1.swp          |~
~                   |~
~                   |~
~                   |~
~                   |~
~                   |~
~                   |~
~                   |~
~                   |~
~                   |~
2          test 0x2> 4          test/file1 0x68      1,1      >

Should I modify the vimrc file? Or write a vimScript to achieve? Or netrw can do these functions by itself.

What should i do to achieve this effect?

My vim version: vim8.1

UPDATE:

I don't want to depend too heavily on third-party plugins, and I want to be able to use these two features directly on all machines where vim is installed, so I don't want to use nerdtree or any other third party plugins.

  • Do you know NERDTree? – Ralf Jan 18 at 13:36
  • 1
  • 1
    @D.BenKnoble I added a comment to the answer below. Right after I did that I noticed your comment. Great minds think alike. :) – B Layer Jan 19 at 2:40
  • @BLayer seems like weve done more of that recently :) – D. Ben Knoble Jan 19 at 2:45
  • @D.BenKnoble I know, right? Separated at birth? ;) – B Layer Jan 19 at 2:45
1

As others have pointed out your intended way of using netrw as a project drawer is not very well supported, designed for or a good fit in general for editors which use splits. I hope you find the time to read the mentioned article Oil and vinegar – split windows and the project drawer.

Despite this, netrw tries to provide some functionality to behave like a project drawer

  • :Lexplore Left Explorer Toggle
  • P Edit in the previously used window
  • Setting an editing window with :NetrwC (i.e. open file from netrw in a specified window)

Additionally, you could try following to open the left explorer on startup:

" On VimEnter
" 1. open empty window along netrw window when starting with a directory as
"    argument, or
" 2. open netrw-Lexplore when opening a file or without argument
let g:netrw_winsize = 30
let g:netrw_liststyle = 3
augroup OpenLexploreOnStartup
    autocmd!
    autocmd VimEnter * if isdirectory(@%)
            \|   rightb vnew | exec 'vertical resize '. string(&columns * 0.7)
            \|   let g:netrw_chgwin = winnr()
            \| else 
            \|   Lexplore
            \| endif
            \| wincmd p
augroup END

However, you will most likely make the experience that netrw is cumbersome to use this way, in particular when you use several windows which cannot really be avoided: help window, quickfix window, diff windows, two code files next to each other (header/implementation), terminal window, ....

I can only reiterate it one more time: If you want to use a project drawer, consider NerdTree, otherwise the way suggested by the article is recommended. The plugin vim-vinegar helps here and adds the global normal mode mapping - to open a netrw window in-place.

You can basically replicate vim-vinegar with these two lines in your vimrc

let g:netrw_banner = 0
nnoremap - :E<CR>

Vim-vinegar would provide additionally a different sorting, file hiding behavior (ie. hide 'wildignore'), a few shortcuts (e.g. ~ go home, y. yank current filename as absolute path) and some modifications to syntax highlighting.


I also want to add a remark about vanilla vim vs. vim+vimrc vs vim+plugins

  • Vanilla vim obviously does not work the way you want it. You need to customize.
  • Copying an answer/code snippet from the internet into your vimrc is not any different than using a plugin. All plugins are open source and the code can be read and modified. They are not necessarily black boxes.
  • Small lightweight plugins provide a solution to a desired functionality. They are usually documented and more tested than a homebaked code snippet.
  • After a year people often do not understand anymore what the thousands of lines in their vimrc do.
  • vim-vinegar is a single file plugin with a line count of 130 which can be read, understood and changed to the own liking.
1

NERDTree (#1658) is a plugin that does those things by default, and much more.

If you're using vim 8, just enter

$ git clone https://github.com/scrooloose/nerdtree  ~/.vim/pack/my-plugins/start/nerdtree

You can also look through the included manual with :h NerdTree. You might need to update the help tags with :helptags ALL.

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.