In VIM how to make NERDTree open at startup nicely when giving args

馋奶兔 提交于 2019-12-24 11:20:00

问题


Let me explain my question, what I want to do is:

  1. From the command line calling gvim without arguments, want NERDTree open by default in my /home/user/Documents folder.

  2. From the command line calling gvim . want to open NERDTree with the directory set to the actual directory where the command was executed from. BUT I still want NERDTree on the left and a empty buffer in the right (not NERDTree being the only window just like normally happens).

  3. From the command line calling gvim /some/path/to/folder want to open NERDTree with the directory set to the given directory. BUT I still want NERDTree on the left and a empty buffer in the right (not NERDTree being the only window just like normally happens).

  4. When calling gvim with an argument:

    • If it is a file, don't open NERDTree just the file.
    • If it is a directory NERDTree should work as #3

To address #1 I have:

function! StartUp()
    if 0 == argc()
        NERDTree ~/Documents
    endif
endfunction

autocmd VimEnter * call StartUp()
autocmd VimEnter * wincmd p

What I was thinking to address #2 and #3 was:

function! StartUp()
    if 0 == argc()
        NERDTree ~/Documents
    else
        if argv(0) == '.'
            NERDTree expand(getcwd())
        else
            NERDTree expand(argv(0))
        endif
    endif
endfunction

autocmd VimEnter * call StartUp()
autocmd VimEnter * wincmd p

But it doesn't work, it gives me errors and vim freezes some times. What I can do to achieve the desired effect?

Thanks for your help.

Complete solution

Does not work exactly as I expected but it's very very close. So far so god.

function! StartUp()
    if 0 == argc()
        NERDTree ~/Documents
    else
        if argv(0) == '.'
            execute 'NERDTree' getcwd()
        else
            execute 'NERDTree' getcwd() . '/' . argv(0)
        endif
    endif
endfunction

autocmd VimEnter * call StartUp()
autocmd VimEnter * wincmd p

回答1:


I can't give you a complete solution, but here's a hint that should resolve the errors:

The :NERDTree command takes an (optional) directory; this doesn't resolve expressions. Vim's evaluation rules are different than most programming languages. You need to use :execute in order to evaluate a variable (or expression); otherwise, it's taken literally; i.e. Vim uses the variable name itself as the argument. So change this:

NERDTree expand(getcwd())

into:

execute 'NERDTree' getcwd()

I've also left out the expand(), as getcwd() already returns a full path.



来源:https://stackoverflow.com/questions/27498962/in-vim-how-to-make-nerdtree-open-at-startup-nicely-when-giving-args

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!