7

I am trying to do some window management in VIM where I create certain windows such as two or three vertical splits with different sizes.

At the moment, I only know how to change panes with <C-w>h/j/k/l

How am I meant to tell my ~/.vimrc to automatically go to the pane to the right?

I have tried autocmd VimEnter * <C-w>l but that does not do anything.

4
  • iProgram, I suggested the edit to the title because the body of your question has to do with window commands and vimscript (ex commands), not “executing keybindings” (which, tbh, doesnt make a lot of sense). I was hoping to bring them together so that title and body align.
    – D. Ben Knoble
    Nov 10, 2018 at 22:03
  • @D.BenKnoble I see what you mean. Its just because I was while doing window management, it just so happens to use keybindings. By asking this question, it makes it more generic. For example, whenever you want to simulate <C-w>, you can do it via wincmd. If you still think it's a good idea to edit the title, feel free and I will accept.
    – iProgram
    Nov 10, 2018 at 22:09
  • 1
    What about “How do I execute normal commands from an autocmd?”
    – D. Ben Knoble
    Nov 10, 2018 at 22:11
  • @D.BenKnoble Sorry about that. It's because I thought normal mode commands were different from pressing keybindings in normal mode. That could work.
    – iProgram
    Nov 10, 2018 at 22:13

1 Answer 1

6

If you want to run commands as if you had typed keys automatically when you start vim, you'll need to use the normal command (to run a normal mode command).

:norm[al][!] {commands}                 *:norm* *:normal*
            Execute Normal mode commands {commands}.  This makes
            it possible to execute Normal mode commands typed on
            the command-line.  {commands} are executed like they
            are typed.  For undo all commands are undone together.
            Execution stops when an error is encountered.

So you'd need autocmd VimEnter * normal <C-w>l. However, vim is really dumb about the <C-w> family of commands. For example this answer of mine. I still don't know why this happens, I just know it's a quirk of vimscript.

Thankfully, there's an easy way around this:

:[count]winc[md] {arg}
        Like executing CTRL-W [count] {arg}.  Example: >
            :wincmd j
<       Moves to the window below the current one.
        This command is useful when a Normal mode cannot be used (for
        the |CursorHold| autocommand event).  Or when a Normal mode
        command is inconvenient.
        The count can also be a window number.  Example: >
            :exe nr . "wincmd w"
<       This goes to window "nr".

So you should be able to do what you want with autocmd VimEnter * wincmd l. Although this might not do very much, since typically there would only be one split open when you open up vim for the first time.

2
  • Thanks for that. Its because I have NERDTree open automatically on startup. I am then using a.out for when I edit .cpp files to create a vertical split and open the header file. Thats why I want it.
    – iProgram
    Nov 9, 2018 at 21:16
  • 1
    @iProgram Ah, that makes sense. Glad I could help! :)
    – DJMcMayhem
    Nov 9, 2018 at 21:19

Your Answer

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

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