0

I have decided to learn vim and want to give it a go for a month. I am about a week in, and it has been OK. I say "OK" because I do run into the usual frustrations that a n00b might run into.

Before vim, I used Visual Studio Code, and let's say I am in my project's directory: /home/jwan/code/bejebeje.admin

Whilst in that directory in the terminal I would run code . and that would open Visual Studio code with all my files in the left pane.

I read that NERDTree can give me that, so I've installed it and I have tried to configure it, but I must be doing something wrong, cause it isn't giving me that workflow that VS Code gave me.

According to the NERDTree docs, I need:

" Start NERDTree when Vim is started without file arguments.
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists('s:std_in') | NERDTree | endif

That comment sounds like what I want! Unless it thinks the dot . in vim . counts as a file argument, in which case maybe that doesn't apply!? I'm not sure.

Anyways, I went ahead and tried it. In my ~/.config/nvim/init.vim I have:

fun! TrimWhitespace()
  let l:save=winsaveview()
  keeppatterns %s/\s\+$//e
  call winrestview(l:save)
endfun

augroup custom_group
  autocmd!
  autocmd BufWritePre * :call TrimWhitespace()
  autocmd StdinReadPre * let s:std_in=1
  autocmd VimEnter * if argc() == 0 && !exists('s:std_in') | NERDTree | endif
augroup end

I believe it is OK to have it in the augroup since that rus automatically, at least that's the understanding I have.

So I saved that and exited vim and restarted it. But I don't get NERDTree with a pane to the right, I just get what I believe is vim's own file browser.

What am I doing wrong?

5
  • 2
    Caveat: I use vim, not neovim, and don't use NERDTree. However, it may be helpful to note that starting vim with vim . opens . in the first buffer, which by default uses the netrw plugin to display a directory listing. Therefore argc() will equal 1, causing your autocmd VimEnter not to fire. You can check this by doing vim . then :echo argc(). Feb 27 at 11:34
  • 1
    Opening vim with no arguments means just executing vim: this may be the use you are striving for? Feb 27 at 11:40
  • 1
    Good read if you’re interested in the possibility of a different workflow altogether: vimcasts.org/blog/2013/01/…
    – D. Ben Knoble
    Feb 27 at 14:52
  • I'm sold! thank you very much @D.BenKnoble
    – J86
    Feb 27 at 19:02
  • Np; if you do solve/answer your question, please add an answer
    – D. Ben Knoble
    Feb 27 at 19:26
1

I ditched NERDTree in favour of the netrw and the workflow described here. Make sure you watch them video casts too, they were super helpful.

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.