简体   繁体   English

在VIM中,给参数时如何使NERDTree在启动时很好地打开

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

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. 从命令行调用gvim不带参数,想用默认值在我NERDTree打开/home/user/Documents夹。

  2. From the command line calling gvim . 从命令行调用gvim . want to open NERDTree with the directory set to the actual directory where the command was executed from. 要设置到该命令从执行的实际目录的目录中打开NERDTree。 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). 但我还是想在左侧和右侧的空缓冲器(未NERDTree是就像通常发生的唯一窗口)NERDTree。

  3. From the command line calling gvim /some/path/to/folder want to open NERDTree with the directory set to the given directory. 从命令行调用gvim /some/path/to/folder要打开NERDTree设置为给定的目录中。 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). 但我还是想在左侧和右侧的空缓冲器(未NERDTree是就像通常发生的唯一窗口)NERDTree。

  4. When calling gvim with an argument: 用参数调用gvim时:

    • If it is a file, don't open NERDTree just the file. 如果它是一个文件,不要打开NERDTree只是文件。
    • If it is a directory NERDTree should work as #3 如果它是一个目录NERDTree应该工作为#3

To address #1 I have: 为了解决#1我有:

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: 我在想什么,以解决#2#3是:

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. 但是,这是行不通的,它给我的错误和VIM冻结一些时间。 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

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; :NERDTree命令使用一个(可选)目录; this doesn't resolve expressions. 这不会解析表达式。 Vim's evaluation rules are different than most programming languages. Vim的评估规则与大多数编程语言不同。 You need to use :execute in order to evaluate a variable (or expression); 您需要使用:execute来评估变量(或表达式); otherwise, it's taken literally; 否则,将按字面意思使用; ie Vim uses the variable name itself as the argument. 即Vim使用变量名本身作为参数。 So change this: 所以改变这个:

NERDTree expand(getcwd())

into: 成:

execute 'NERDTree' getcwd()

I've also left out the expand() , as getcwd() already returns a full path. 我还省略了expand() ,因为getcwd()已经返回了完整路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM