codegourmet

savory code and other culinary highlights

Using Autocmd for Vim Startup

| Comments

A staple of my vimconfig is autocmd and it can be used for a lot of contextual hooks.

When I start up my Vim in the morning, I want NERDTree to be open and a list of my TaskWarrior tasks to show up. But when I start vim with a file, I don’t want either of those to show.

Here’s how I start NERDTree and TaskWarrior when opening an empty vim instance:

1
2
3
4
5
6
7
8
9
function NERDTreeAndTaskWarrior()
    TW
    NERDTreeToggle
    wincmd l
endfunction

if argc() == 0
    autocmd VimEnter * call NERDTreeAndTaskWarrior()
endif

Note the use of argc() to prevent this from happening when a file argument is supplied to vim. The wincmd l is necessary to return the cursor to the taskwarrior buffer window after opening NERDTree.

Happy Coding! – codegourmet

Comments