28

I am using NerdTree on Linux . I would like to jump from the nerd tree buffer to the file buffer after opening a file in a new tab using 't'. I need to press 'Ctrl+W+W' all the time.

Any idea about how to have vi execute C-W-W after a new buffer is created .

Sairam

3 Answers 3

93

When vim starts, it opens a window for the file, then it opens another window for NerdTree.

The easiest way to come back to the main window is just to jump to the previous window like this:

" Start NERDTree
autocmd VimEnter * NERDTree
" Go to previous (last accessed) window.
autocmd VimEnter * wincmd p
5
  • 6
    exactly what i needed! when opening a file with vim, this sets focus to the newly opened file while still opening NERDTree Feb 3, 2014 at 2:38
  • 8
    You can combine commands: autocmd VimEnter * NERDTree | wincmd p
    – NARKOZ
    Mar 13, 2015 at 16:39
  • perfect thanks got here by searching vimenter window
    – Plato
    Jun 27, 2016 at 23:00
  • This works actually. Combining as @NARKOZ says doesn't works for me, not sure why.
    – X3MBoy
    Mar 12, 2018 at 19:57
  • 1
    to make NERDTree open on startup and stay focus on nerdtree if no files specified autocmd StdinReadPre * let s:std_in=1 autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | else | NERDTree | wincmd p | endif
    – buncis
    Dec 17, 2020 at 3:11
4

You can use so-called autocommands, like:

  au BufNew * <command>

It will execute command when new buffer is created. Start from there. Check out vimdoc on the subject.

-3

Simply add autocmd BufNew * wincmd l to your .vimrc

2
  • 1
    For future travelers: This doesn't work for me (vim 7.2), but putting PaulP's solution in .vimrc after the the line autocmd vimenter * NERDTree did the trick
    – Hamy
    Apr 11, 2013 at 1:44
  • 1
    Yohann's solution worked for me, not this one. Not sure if this is out of date or not, but definitely is very similar.
    – MMM
    Oct 13, 2014 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.