Skip to content

Instantly share code, notes, and snippets.

@bonsaiben
Created October 7, 2012 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bonsaiben/3847844 to your computer and use it in GitHub Desktop.
Save bonsaiben/3847844 to your computer and use it in GitHub Desktop.
Vim seamless splitting and movement
" if there is no split in the direction of movement, split and then move to it
function! MoveOrSplit(direction)
" the width of an unsplit window on your display
let maxwidth = 181
" the height of an unsplit window on your display
let maxheight = 49
let has_no_vsplit = winnr('$') < 2 || winwidth(0) == maxwidth
let has_no_split = winnr('$') < 2 || winheight(0) == maxheight
if a:direction == 'j' || a:direction == 'k'
if has_no_split
:split
end
else
if has_no_vsplit
:vsplit
end
end
:exec "normal \<c-w>" . a:direction
endfunction
:command! MoveOrSplitDown :call MoveOrSplit('j')
:command! MoveOrSplitUp :call MoveOrSplit('k')
:command! MoveOrSplitRight :call MoveOrSplit('l')
:command! MoveOrSplitLeft :call MoveOrSplit('h')
:map <c-j> :MoveOrSplitDown<cr>
:map <c-k> :MoveOrSplitUp<cr>
:map <c-l> :MoveOrSplitRight<cr>
:map <c-h> :MoveOrSplitLeft<cr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment