Justin Li

Open a Directory in Vim Terminal

2020-05-05

I've never found (neo)vim's netrw to be particularly useful, and have generally much preferred to navigate via the built-in terminal. I've generally managed to avoid netrw, with the one exception being when I :tabnew a directory (for example, :tabnew ~/courses/). I finally fixed that by configuring vim to open a terminal at that directory instead:

function s:isdir(dir) abort
    return !empty(a:dir) && isdirectory(a:dir)
endfunction

augroup justinnhli_open_directories
    autocmd!
    autocmd VimEnter * silent! autocmd! FileExplorer *
    autocmd BufEnter * if s:isdir(expand('%')) | execute ':terminal' | startinsert | endif
augroup END

I got the general idea from this StackOverflow answer. The key are the two autocmd commands. The first disables netrw ("FileExplorer") from running. The second checks if the target is a directory, then opens :terminal if it is.

I have several more neovim terminal settings, available on my dotfiles repo.