Search code examples
vimctrlp

Open CtrlP on VimEnter Only If Opening a Directory


I'd like to open CtrlP if I am opening a directory with vim, but not a file. I like to have it automatically open in I just open a directory for convenience. However, it is slightly inconvenient if I know exactly which file I want to open because of the added loading time.

Currently I just have this in my .vimrc:

autocmd vimenter * CtrlP

Thanks in advance for any responses!


Solution

  • You can write a function to test the args to see if a single directory was passed in and if it was, execute CtrlP. Here is a very rudimentary solution:

    function! MaybeCtrlP()
        if argc() == 1 && isdirectory(argv()[0])
            " Uncomment this to remove the Netrw buffer (optional)
            " execute "bdelete"
            execute "CtrlP"
        endif
    endfunction
    
    autocmd VimEnter * :call MaybeCtrlP()