Skip to content

Instantly share code, notes, and snippets.

@kien
Forked from AndrewRadev/windows.vim
Created January 3, 2012 04:14
Show Gist options
  • Save kien/1553446 to your computer and use it in GitHub Desktop.
Save kien/1553446 to your computer and use it in GitHub Desktop.
Repeatable <c-w> and family
" Repeatable <c-w> and family
nn <silent> <c-w> <esc>:cal wincmd#init()<cr>
nn <silent> _. <esc>:cal wincmd#repeat('act')<cr>
nn <silent> _; <esc>:cal wincmd#repeat('cur')<cr>
" File: autoload/wincmd.vim
" Description: Use <c-w>{key} mappings with the possibility of repeating them
fu! wincmd#init(...)
" get the count before <c-w> i.e. {count}<c-w>
let vpct = v:prevcount
let pct = vpct ? vpct : ''
" if this is looped, accumulate the count after <c-w> i.e. <c-w>{count}
let cmd_count = a:0 ? a:1 : pct
" echo the command so far for some visual feedback
redr | ec ( a:0 ? pct.( pct == '' ? '' : '*' ) : '' ).cmd_count.'wincmd '
" wait for a keypress from the user
let char = nr2char(getchar())
if char =~ '\d'
" if it's a digit, add it to cmd_count and ask again
retu wincmd#init( a:0 ? cmd_count.char : char )
en
" get the accumulated <c-w>{count}, multiply it by {count}<c-w>
let cmd_count = !vpct && ( !a:0 || ( a:0 && !a:1 ) ) ? ''
\ : ( vpct ? vpct : 1 ) * ( a:0 ? a:1 : 1 )
let wcmd = 'wincmd '.char
cal s:exewinc(cmd_count, wcmd)
" setup the storage var
if !exists('s:rp')
let s:rp = {}
en
if char !~# "\\vj|k|h|l|w|t|b|p|P"
\ && char !~# "\\v\<Up>|\<Down>|\<Left>|\<Right>|\<Esc>"
\ && char !~# "\\v\<C-J>|\<C-K>|\<C-H>|\<C-L>"
\ && char !~# "\\v\<C-W>|\<C-T>|\<C-B>|\<C-P>|\<BS>"
" window manipulation
cal extend(s:rp, { 'act': [cmd_count, wcmd] })
elsei char !~# "\\v\<Esc>"
" cursor position
cal extend(s:rp, { 'cur': [cmd_count, wcmd] })
en
endf
fu! wincmd#repeat(...)
if exists('s:rp') && has_key(s:rp, a:1)
" get the saved count, multiply it by {count}<repeat-mapping>
let [sav_count, sav_cmd] = s:rp[a:1]
let cmd_count = !v:prevcount && sav_count == '' ? ''
\ : ( v:prevcount ? v:prevcount : 1 ) * ( sav_count != '' ? sav_count : 1 )
cal s:exewinc(cmd_count, sav_cmd)
en
endf
fu! s:exewinc(cmdc, winc)
" execute the command and show it to the user
redr | ec a:cmdc != '' ? a:cmdc.a:winc : ''
sil! exe a:cmdc.a:winc
endf
" vim:nofen:noet:ts=2:sw=2:sts=2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment