Search code examples
linuxvimnerdtreeautocmd

How would you switch to the previous window using vim autocmd


I use NERDTree and have it set to automatically open when I open vim, but that causes the NERDTree window to be the one selected, which isn't what I want. I want to be able to go right into editing the file I opened, but autocmd vimenter * <c-w><c-p> just gives me an error on startup. Is there any way to do what I'm wanting to do, or is it impossible?

I'm not completely familiar with Vim and its configuration files, so excuse me if this is a simple question, but I haven't been able to find it anywhere.


Solution

  • :autocmd's execute ex-commands not normal mode commands. This means you can use :normal! or use :wincmd (method I prefer).

    Your new command:

    autocmd VimEnter * wincmd p
    

    Note: wincmd p is the same as <c-w>p or <c-w><c-p> in normal mode.

    For more help see:

    :h :wincmd
    :h :normal
    :h :au