smart split

2 Points

Nic West Nic West

7 years ago

splits a window vertically only if there is space for it otherwise splits horizontally

let g:smartsplit_width = 80

function! s:smartsplit() abort
  if winwidth('.') >= 2 * g:smartsplit_width
    execute "norm! \<C-W>v\<C-W>l"
  else
    execute "norm! \<C-W>s\<C-W>j"
  endif
endfunction

command! SmartSplit :call <SID>smartsplit()

"EXAMPLE

" original opens vimrc in forced vertical split
nnoremap <leader>ev <C-w><C-v><C-l>:e $MYVIMRC<CR>

" new version will only open in vertical split if there is space otherwise 
" will open in horizontal split.
nnoremap <leader>ev :SmartSplit<CR>:e $MYVIMRC<CR>