Skip to content

Instantly share code, notes, and snippets.

@tyru
Created October 10, 2018 02:40
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 tyru/8ff91a82f0d1bf73eaa9536bcaa77789 to your computer and use it in GitHub Desktop.
Save tyru/8ff91a82f0d1bf73eaa9536bcaa77789 to your computer and use it in GitHub Desktop.
Toggle current tab's terminal window modes
nnoremap <Plug>(vimrc:prefix:excmd)oT :<C-u>call <SID>toggle_terminal_modes()<CR>
function! s:toggle_terminal_modes()
let prevwinnr = winnr()
" -1: undefined, 0: terminal normal mode, 1: terminal job mode
" Change all terminal window's modes to this mode.
" It is determined by the first terminal window's mode.
let dest_mode = -1
try
for bufnr in tabpagebuflist()
if getbufvar(bufnr, '&buftype') isnot# 'terminal'
continue
endif
let is_normal = term_getstatus(bufnr) =~# '\<normal\>'
if dest_mode is# -1
let dest_mode = is_normal
endif
if is_normal && dest_mode is# 1
call win_gotoid(win_getid(bufwinnr(bufnr)))
normal! GA
elseif !is_normal && dest_mode is# 0
" XXX: `exe "normal! \<C-w>N"` cannot work in terminal window
call feedkeys("\<C-w>" . bufwinnr(bufnr) . "\<C-w>\<C-w>N", 'n')
endif
endfor
finally
call feedkeys("\<C-w>" . prevwinnr . "\<C-w>", 'n')
endtry
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment