Humanity

Edit the world by your favorite way

Vim の terminal window でサッと別のタブに移動したい

※本記事は Vim Advent Calendar の記事ではありません

忙しいと文章書くの面倒になりますね。箇条書きでつらつらと書きます。

  • タブ移動 (gt, gT) は terminal window ではフォーカスを奪われるので使えない
  • terminal window でも普通のバッファでも使えるように <C-w>gt, <C-w>gT がある (追加された?)
  • ただこれは gt, gT と違って {count} を受け取れない
  • {count}<C-w>gtVim の仕様上無理そうなので <C-w>{count}gt で {count} 個右のタブに移動したい
  • それがこちら
" <C-w>{count}gt, <C-w>{count}gT
function! s:setup_jump_tab_mappings() abort
  for [mode, cmds] in [['t', ['gt', 'gT']], ['n', ['gt', 'gT']]]
    for cmd in cmds
      execute printf('%snoremap <C-w>1%s <C-w>%s', mode, cmd, cmd)
      execute printf('%smap <C-w>2%s <C-w>%s<C-w>1%s', mode, cmd, cmd, cmd)
      execute printf('%smap <C-w>3%s <C-w>%s<C-w>2%s', mode, cmd, cmd, cmd)
      execute printf('%smap <C-w>4%s <C-w>%s<C-w>3%s', mode, cmd, cmd, cmd)
      execute printf('%smap <C-w>5%s <C-w>%s<C-w>4%s', mode, cmd, cmd, cmd)
      execute printf('%smap <C-w>6%s <C-w>%s<C-w>5%s', mode, cmd, cmd, cmd)
      execute printf('%smap <C-w>7%s <C-w>%s<C-w>6%s', mode, cmd, cmd, cmd)
      execute printf('%smap <C-w>8%s <C-w>%s<C-w>7%s', mode, cmd, cmd, cmd)
      execute printf('%smap <C-w>9%s <C-w>%s<C-w>8%s', mode, cmd, cmd, cmd)
    endfor
  endfor
endfunction
call s:setup_jump_tab_mappings()
  • {count} は 1-9 までですが普段使いで特に問題になることはなさそう
  • YATTA〜

追記:2019/12/20 17:20

gT, <C-w>gT 押しづらいなと今更ながら思ったので gw, <C-w>gw で使えるようにしました。

" Use gw instead of gT
nnoremap gw gT
nnoremap <C-w>gw <C-w>gT
tnoremap <C-w>gw <C-w>gT

最初の設定もこんな感じに修正。

" <C-w>{count}gt, <C-w>{count}gw -> <C-w>{count}gT
function! s:setup_jump_tab_mappings() abort
  for [mode, cmds] in [
    \ ['t', [#{lhs: 'gt', rhs: 'gt'}, #{lhs: 'gw', rhs: 'gT'}]],
    \ ['n', [#{lhs: 'gt', rhs: 'gt'}, #{lhs: 'gw', rhs: 'gT'}]],
    \]
    for cmd in cmds
      execute printf('%snoremap <C-w>1%s <C-w>%s', mode, cmd.lhs, cmd.rhs)
      execute printf('%smap <C-w>2%s <C-w>%s<C-w>1%s', mode, cmd.lhs, cmd.lhs, cmd.lhs)
      execute printf('%smap <C-w>3%s <C-w>%s<C-w>2%s', mode, cmd.lhs, cmd.lhs, cmd.lhs)
      execute printf('%smap <C-w>4%s <C-w>%s<C-w>3%s', mode, cmd.lhs, cmd.lhs, cmd.lhs)
      execute printf('%smap <C-w>5%s <C-w>%s<C-w>4%s', mode, cmd.lhs, cmd.lhs, cmd.lhs)
      execute printf('%smap <C-w>6%s <C-w>%s<C-w>5%s', mode, cmd.lhs, cmd.lhs, cmd.lhs)
      execute printf('%smap <C-w>7%s <C-w>%s<C-w>6%s', mode, cmd.lhs, cmd.lhs, cmd.lhs)
      execute printf('%smap <C-w>8%s <C-w>%s<C-w>7%s', mode, cmd.lhs, cmd.lhs, cmd.lhs)
      execute printf('%smap <C-w>9%s <C-w>%s<C-w>8%s', mode, cmd.lhs, cmd.lhs, cmd.lhs)
    endfor
  endfor
endfunction
call s:setup_jump_tab_mappings()