10

When I run vim, the NerdTree plugin opens a project explorer. I achieved this by adding the following line to my .vimrc file:

autocmd VimEnter * NERDTree

It opens two buffers but unfortunately I need to press ^W^W each time because the active buffer is the left one, where the NerdTree is located.

How can I make vim emulate these key presses on startup?

Gaff
  • 18,569
  • 15
  • 57
  • 68
gennad
  • 349
  • 5
  • 13

1 Answers1

14

There are a couple of ways to give Vim keypresses in a command. The general way is to use the :normal command, which in this case would be

:execute "normal \<C-W>\<C-W>"

where the :execute command is needed to expand the control characters. For normal commands that begin with Ctrl-W, however, the :wincmd command can be simpler to use, e.g.,

:wincmd w

where in this case I've taken advantage of the fact that Ctrl-W Ctrl-W and Ctrl-W w do the same thing. Your autocommand would then be

autocmd VimEnter * wincmd w

See

:help :normal
:help :wincmd
:help CTRL-W_w
garyjohn
  • 34,610
  • 8
  • 97
  • 89
  • I had a script from somewhere that had `execute 'normal 1_'` that didn't work on my vim setup. Swapping the single-quotes for doubles and adding the `\` finally got it running. – Michael Apr 08 '16 at 17:36
  • what's the difference between `:execute "normal \\"` and `:silent execute "normal \\"` ? – theonlygusti Mar 26 '23 at 14:43