vim-smartsplit

You're lazy. Let Vim decide your split directions.
git clone git://git.danielmoch.com/vim-smartsplit.git
Log | Files | Refs | README

commit 620e0ff310881876fe1d4182d77d735b4358513d
Author: Daniel Moch <daniel@danielmoch.com>
Date:   Fri, 19 Jul 2019 17:47:55 -0400

Initial commit

Diffstat:
Adoc/smartsplit.vim | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aplugin/smartsplit.vim | 83+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 148 insertions(+), 0 deletions(-)

diff --git a/doc/smartsplit.vim b/doc/smartsplit.vim @@ -0,0 +1,65 @@ +*smartsplit.txt* You're lazy. Let Vim decide your split directions. + +Author: Daniel Moch <daniel@danielmoch.com> +License: VIM LICENSE + + *smartsplit* + +Yeah, you're aware of split/vsplit, new/vnew and the like. You use them +because you have to, but you know there must be a better way. This is it. + +Vim, given it's old pedigree, tacitly assumes a standard terminal width of +around 80 characters. In this environment it makes sense for Vim to assume any +command that divides the current window should divide horizonally. But our +monitors are often capable of more than twice the width of an old terminal, so +this doesn't make sense any more. + +One sensible option, which this plugin adopts, is to make a best-guess at the +width of the text displayed in the current window (see 'textwidth'. Then +operations should default to vertical if there is enough horizonal room to +display two splits side-by-side. + +This plugin provides :Help, :Split, :New, and :Buffer commands to replace +their lower-case counterparts. It also sets 'diffopt' to split in either a +horizontal or vertical direction, again depending on the horizontal space +available. + +1. Commands |smartsplit-commands| +2. About |smartsplit-about| + +============================================================================= +1. Commands *smartsplit-commands* + + *smartsplit-Split* *:Split* +Split A smart version of the |:split| command. Open a new + split in the current tab. The direction is + determined in reference to the value of + 'textwidth' for the current buffer (defaulting to + 80). If the horizontal space is greater than twice + the current 'textwidth' value, then the split is + made vertically. Otherwise the split is + horizontal. + + *smartsplit-New* *:New* +New A smart version of |:new|. Horizontal versus + vertical splitting is determined as described in + |:Split|. + + *smartsplit-Buffer* *:Buffer* +Buffer A smart version of |:buffer|. Horizontal versus + vertical splitting is determined as described in + |:Split|. + + *smartsplit-Help* *:Help* +Help A smart version of |:help|. Horizontal versus + vertical splitting is determined as described in + |:Split|. + +============================================================================= +2. ABOUT *smartsplit-about* + +More details can be found in README.md or by navigating to: + + https://git.danielmoch.com/vim-smartsplit/about + +vim:tw=78:ts=8:ft=help:norl: diff --git a/plugin/smartsplit.vim b/plugin/smartsplit.vim @@ -0,0 +1,83 @@ +" +" TITLE: VIM-SMARTSPLIT +" AUTHOR: Daniel Moch <daniel AT danielmoch DOT com> +" LICENSE: VIM LICENSE +" VERSION: 0.1.0-dev +" +if exists('g:loaded_smartsplit') || &cp + finish +endif + +let g:loaded_smartsplit = 1 +let s:save_cpo = &cpo +set cpo&vim + +function! s:WinWidth() + let l:textwidth = &tw ? (&tw + 8) : 80 + echo l:textwidth + if &number + let l:numwidth = &numberwidth > len(line('$')) + 1 ? + \ &numberwidth : len(line('$')) + 1 + else + let l:numwidth = 0 + endif + return winwidth(0) > (2 * l:textwidth) + (2 * l:numwidth) +endfunction + +function! s:ScratchBuffer() abort + let l:wincmd = s:WinWidth() ? 'vnew' : 'new' + exec l:wincmd + setlocal bufhidden=hide buftype=nofile noswapfile +endfunction + +function! s:DiffOpt() + if s:WinWidth() + set diffopt+=vertical + else + set diffopt-=vertical + endif +endfunction + +function! s:NewWindow(...) + if a:0 == 0 + let l:filename = '' + else + let l:filename = a:1 + endif + if s:WinWidth() + execute 'vnew '.l:filename + else + execute 'new '.l:filename + endif +endfunction + +function! s:Help(topic) + if s:WinWidth() + execute 'vert help '.a:topic + else + execute 'help '.a:topic + endif +endfunction + +function! s:Buffer(...) + let l:splitcmd = s:WinWidth() ? 'vsplit' : 'split' + execute l:splitcmd + if a:0 == 1 + execute bufnr(a:1).'buffer' + endif +endfunction + +augroup smartsplit + autocmd! + autocmd VimResized * call <sid>DiffOpt() + autocmd VimEnter * call <sid>DiffOpt() +augroup END + +command! -complete=file -nargs=? New call <sid>NewWindow(<f-args>) +command! -complete=help -nargs=1 Help call <sid>Help(<f-args>) +command! -complete=buffer -nargs=1 Buffer call <sid>Buffer(<f-args>) +command! Split call <sid>Buffer() + +let &cpo = s:save_cpo +unlet s:save_cpo +" vim: et sts=4 sw=4 tw=72