4

ファイルが開かれていない、または作成されていないときに、vim で :Explorer を開くようにしたい。例えば。vimオプションなしで電話したとき。

ただし、呼び出しvim newfile.txtは通常どおりに動作するはずです。

どうすればこれを行うことができますか?私はそれの正しいものを見つけることができないようですautocmd

4

2 に答える 2

6

これを vim の呼び出しのみに使用する場合は、次の方法を使用するのが最善の方法ですargc()

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

argc()関数は、vim が呼び出されたときにコマンドラインで指定されたファイル名の数を返します。引数リストが変更されていない限り、詳細については を参照してください:h argc()

于 2011-05-12T20:03:12.340 に答える
2

自分で答えを見つけました:

"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

このコードの大部分は、この質問から取られました。

于 2011-05-12T12:01:59.790 に答える