Walking Around Your Windows

Vim can do an insane number of things with windows in Vim. I take a minimalist approach to windows in Vim and try to make it as simple for myself as possible. There are a number of factors that drive my configuration:

  • I always use my left hand to hit the CTRL key. The main key that is used for windowing is CTRL-W.
  • I prefer to have multiple key sequences to occur on opposing hands when possible. CTRL-W is two simultaneous keys on the same hand.
  • It’s all about reusing existing habits when I can. So h, j, k and l are important to reuse. The existing Vim solution actually already does this.

Those points lead me to have the following mappings for window management:

" Move the cursor to the window left of the current one

noremap <silent> ,h :wincmd h<cr>

" Move the cursor to the window below the current one

noremap <silent> ,j :wincmd j<cr>

" Move the cursor to the window above the current one

noremap <silent> ,k :wincmd k<cr>

" Move the cursor to the window right of the current one

noremap <silent> ,l :wincmd l<cr>

" Close the window below this one

noremap <silent> ,cj :wincmd j<cr>:close<cr>

" Close the window above this one

noremap <silent> ,ck :wincmd k<cr>:close<cr>

" Close the window to the left of this one

noremap <silent> ,ch :wincmd h<cr>:close<cr>

" Close the window to the right of this one

noremap <silent> ,cl :wincmd l<cr>:close<cr>

" Close the current window

noremap <silent> ,cc :close<cr>

" Move the current window to the right of the main Vim window

noremap <silent> ,ml <C-W>L

" Move the current window to the top of the main Vim window

noremap <silent> ,mk <C-W>K

" Move the current window to the left of the main Vim window

noremap <silent> ,mh <C-W>H

" Move the current window to the bottom of the main Vim window

noremap <silent> ,mj <C-W>J

9 comments on this post.
  1. Nikos Aggelidis:

    for movements between windows, i use shift + h,j,k,l .
    I also like to follow the rule:

    “I prefer to have multiple key sequences to occur on opposing hands when possible. “

  2. Mike B:

    It looks like the ‘s got stripped from the mappings.

  3. Mike B:

    Let’s try that again:

    It looks like the (less than sign)CR(greater than sign)’s got stripped from the mappings.

  4. Derek Wyatt:

    You’re right! Bloody wordpress :) Should be good now… thanks to the Vim blogit plugin… beauty stuff :)

  5. Mitko:

    You shouldn’t use , as a map leader. It is an important feature of vim. :h,

  6. Derek Wyatt:

    That’s why I have ‘[vn]noremap <c-e> ,’

    I’ll keep using ‘,’ as a mapleader, thanks :)

  7. Mitko:

    Sorry to insist but c-e is important for scrolling in normal mode :)

  8. Derek Wyatt:

    Obviously not to me. :) Fortunately Vim’s configuration allows us to get rid of stuff that we don’t really find all that useful.

    The main issue is that ‘\’ sucks as a map leader… it’s placed differently on almost every keyboard out there and it’s right next to the enter key a lot of the time. Making a mistake once in a while is OK but when you hit the key a hundred times an hour, making mistake after mistake after mistake is a real pain in the butt. The ‘,’ key is easily accessible and universally placed – using it as a map leader is far preferable to me, and <c-e> is mostly useless considering there are *so many* other ways to scroll around.

  9. Mitko:

    Fair enough,
    I am using c-e quite a lot and find it very usefull indeed and \ is just besides my left shift key so I do use it as a map leader but your arguments make sense… for you :)

Leave a comment