Wikia

Vim Tips Wiki

Fast window resizing with plus/minus keys

Talk0
1,610pages on
this wiki
Revision as of 01:08, August 3, 2012 by Nievim (Talk | contribs)

Tip 427 Printable Monobook Previous Next

created February 14, 2003 · complexity basic · author Bob Heckel · version 5.7


Depending on your willingness to occasionally consume the + and - keys, here's a fast way to resize the active window if you have more than one window open:

if bufwinnr(1)
  map + <C-W>+
  map - <C-W>-
endif

I normally use the scrollpad + and - keys. (<kPlus> and <kMinus> in gvim)

The map eliminates the need for time-consuming chording and if you liked chording, you'd be using Emacs instead of Vim anyway...

Comments

Depending on your screen size, and your brain's memory capacity for more vim mappings (caution this will affect your every day speech when vim mappings exceeds 100), you may want to increase the windows' frame size to some preselected settings. I am sure there is a more elegant solution for incrementing the size of this (get the current setting into the buffer, increment it and set it again). I am too lazy so I use full screen and min screen based on my 1400x1050 and guifont=lucida_console:h7:w5 " small (94 lines in one screen)

map <M-[> :set co=210<CR>:set lines=180<CR> "fullscreen
map <M-]> :set co=50<CR>:set lines=50<CR> "minimumscreen
map <M-\> :set co=120<CR>:set lines=80<CR> "my default screen size

I'm not much for resizing windows.. However, I stole your tip and made a slight modification:

if bufwinnr(1)
  map - <C-W><UP>
  map + <C-W><DOWN>
endif

Now I can go through my open windows very easily with + and - :)


The <C-W><UP> and <C-W><DOWN> work well if you're only split horizontally, but if you split vertically, this might make rotating between windows a bit easier:

if bufwinnr(1)
  map - <C-W><C-W>
  map + <S-C-W><S-C-W>
endif

I have several mappings for window movement/operations in my .vimrc, too. Here are some of them:

map + <c-w>+
map - <c-w>-

What was posted as the original tipp. I had the same idea, i think.

map <c-n> <c-w><
map <c-m> <c-w>>

Similar to the mappings above, but for horizontal resizing.

map <c-w>F <c-w>_<c-w><Bar>

Fullscreen the actual window. (not depending on the terminal's size)

map <Up> <c-w>k<c-w>_<c-w><Bar>
map <Down> <c-w>j<c-w>_<c-w><Bar>
map <Left> <c-w>h<c-w>_<c-w><Bar>
map <Right> <c-w>l<c-w>_<c-w><Bar>

Walking through windows and make them fullscreen at the same time.

In addition, I've set winminwidth and winminheight to 0.


Using the ideas above I come up with this version :)

" Fast window resizing with +/- keys (horizontal); / and * keys (vertical)
if bufwinnr(1)
  map <kPlus> <C-W>+
  map <kMinus> <C-W>-
  map <kDivide> <c-w><
  map <kMultiply> <c-w>>
endif

+ / - didn't work for me, it moved the cursor up and down, but with kPlus, etc. it's OK.


I have added the following to my vimrc file. Holding down the SHIFT key and using the arrow keys is quick and efficient for resizing the windows.

" Window resizing mappings /*{{{*/
nnoremap <S-Up> :normal <c-r>=Resize('+')<CR><CR>
nnoremap <S-Down> :normal <c-r>=Resize('-')<CR><CR>
nnoremap <S-Left> :normal <c-r>=Resize('<')<CR><CR>
nnoremap <S-Right> :normal <c-r>=Resize('>')<CR><CR>
function! Resize(dir)
  let this = winnr()
  if '+' == a:dir || '-' == a:dir
    execute "normal \<c-w>k"
    let up = winnr()
    if up != this
      execute "normal \<c-w>j"
      let x = 'bottom'
    else
      let x = 'top'
    endif
  elseif '>' == a:dir || '<' == a:dir
    execute "normal \<c-w>h"
    let left = winnr()
    if left != this
      execute "normal \<c-w>l"
      let x = 'right'
    else
      let x = 'left'
    endif
  endif
  if ('+' == a:dir && 'bottom' == x) || ('-' == a:dir && 'top' == x)
    return "5\<c-v>\<c-w>+"
  elseif ('-' == a:dir && 'bottom' == x) || ('+' == a:dir && 'top' == x)
    return "5\<c-v>\<c-w>-"
  elseif ('<' == a:dir && 'left' == x) || ('>' == a:dir && 'right' == x)
    return "5\<c-v>\<c-w><"
  elseif ('>' == a:dir && 'left' == x) || ('<' == a:dir && 'right' == x)
    return "5\<c-v>\<c-w>>"
  else
    echo "oops. check your ~/.vimrc"
    return ""
  endif
endfunction
" /*}}}*/ 

This is what I use.

" Maps Alt-[h,j,k,l] to resizing a window split
map <silent> <A-h> <C-w><
map <silent> <A-j> <C-W>-
map <silent> <A-k> <C-W>+
map <silent> <A-l> <C-w>>
" Maps Alt-[s.v] to horizontal and vertical split respectively
map <silent> <A-s> :split<CR>
map <silent> <A-v> :vsplit<CR>
" Maps Alt-[n,p] for moving next and previous window respectively
map <silent> <A-n> <C-w><C-w>
map <silent> <A-p> <C-w><S-w>

I find it very useful as my shift and ctrl key are overused for mapping and I don't like moving my hand to the arrow keys (yes I am very very lazy)


vim

Around Wikia's network

Random Wiki