Simple Vim Window Management

When I use vim I find myself constantly, opening windows, splitting them and generally putting production code next to my specs. Sometimes on a really big screen I'll manage a 5 window split. I have yet to find an editor that gives me the same power and speed in window management as vim does. But it certainly isn't due to the default key bindings.

I never got my head around <c-w><c-s>. having to translate to horizontal and vertical splits takes away from the fluidity of my intentions. I prefer to think in terms of up down left right. or in vim: kjhl

The shortcut I've come up with over time is: <leader> followed by either hjkl to move into the window above,below,left or right respectively.

What makes this particularly useful, is using these same commands to open new windows.

So if <leader>k means move to the window above, then if there isn't a window above it should mean: open a window above. This allows you to use the same muscle memory for moving and opening.

function! WinMove(key) let t:curwin = winnr() exec "wincmd ".a:key if (t:curwin == winnr()) "we havent moved if (match(a:key,'[jk]')) "were we going up/down wincmd v else wincmd s endif exec "wincmd ".a:key endif endfunction

map <leader>h :call WinMove('h')<cr> map <leader>k :call WinMove('k')<cr> map <leader>l :call WinMove('l')<cr> map <leader>j :call WinMove('j')<cr>

If anything that's the most useful portion of this post. And I'd urge your to give it a try, it's extremely intuitive. The rest of this post is mainly for completion.

closing, rotating and moving

I also use <leader>wc to close a window. The mnemonic is "window close" and <leader>wr to rotate windows or "window rotate"

map <leader>wc :wincmd q<cr> map <leader>wr <C-W>r

vim also allows you to move existing windows. I use this more than Rotate, it follows the same principle as before using hjkl, but relies on it's capital forms:

map <leader>H :wincmd H<cr> map <leader>K :wincmd K<cr> map <leader>L :wincmd L<cr> map <leader>J :wincmd J<cr>

It's kind of hard to explain how these function exactly, as it's easier to learn by example. If you find the need to move windows a lot (I typically don't) then try these out in a vim session and see how they fit.

Resizing windows

People always advise new vim users to disable the up down left right keys, and rightly so. Here is a use for them: easily resize windows.

Take note that these are sometimes inverted depending on which side your on. It's quite easy to get used to, but again hard to explain. To be honest, resizing windows is something I hardly ever do.

nmap <left> :3wincmd <<cr> nmap <right> :3wincmd ><cr> nmap <up> :3wincmd +<cr> nmap <down> :3wincmd -<cr>

That's it, hopefully you can pick up something useful here to drop in your .vimrc