Vim 自动命令在打开“nothing”时触发

发布于 2024-11-06 22:08:16 字数 179 浏览 0 评论 0原文

我希望 vim 在没有打开或创建文件时打开 :Explorer。例如。当我在没有任何选项的情况下调用 vim 时。

不过,调用 vim newfile.txt 仍应按正常方式运行。

我该怎么做呢?我似乎找不到正确的 autocmd

I want vim to open up the :Explorer when no file is opened or created. Eg. when I call vim without any options.

calling vim newfile.txt should still behave the normal way though.

How would I go about doing this? I can't seem to find the correct autocmd for it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瀟灑尐姊 2024-11-13 22:08:16

如果您只想对 vim 调用执行此操作,最好的方法是使用 argc() :

autocmd VimEnter * :if argc() is 0 | Explore | endif

argc() 函数返回在命令行上指定的多个文件名除非修改了参数列表,否则 vim 会被调用,更多信息请参见 :h argc()

If you want to do this for vim invocation only, the best way is to use argc():

autocmd VimEnter * :if argc() is 0 | Explore | endif

argc() function returns a number of filenames specified on command-line when vim was invoked unless something modified arguments list, more information at :h argc().

不必在意 2024-11-13 22:08:16

我自己找到了答案:

"open to Explorer when no file is opened
function! TabIsEmpty()
    " Remember which window we're in at the moment
    let initial_win_num = winnr()

    let win_count = 0
    " Add the length of the file name on to count:
    " this will be 0 if there is no file name
    windo let win_count += len(expand('%'))

    " Go back to the initial window
    exe initial_win_num . "wincmd w"

    " Check count
    if win_count == 0
        " Tab page is empty
        return 1
    else
        return 0
    endif
endfunction

" Test it like this:
" echo TabIsEmpty()

function! OpenExplorer()
    if (TabIsEmpty())
        :Explore
    end  
endfunction

这段代码的最大部分取自 这个问题

Found the answer myself:

"open to Explorer when no file is opened
function! TabIsEmpty()
    " Remember which window we're in at the moment
    let initial_win_num = winnr()

    let win_count = 0
    " Add the length of the file name on to count:
    " this will be 0 if there is no file name
    windo let win_count += len(expand('%'))

    " Go back to the initial window
    exe initial_win_num . "wincmd w"

    " Check count
    if win_count == 0
        " Tab page is empty
        return 1
    else
        return 0
    endif
endfunction

" Test it like this:
" echo TabIsEmpty()

function! OpenExplorer()
    if (TabIsEmpty())
        :Explore
    end  
endfunction

The greatest part of this code was taken from this question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文