Vim's wincmd-style tab / window moving commands (vim plugin).
Vim script
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


TWcmd.vim is a set of useful commands for moving & managing tabs and windows in vim.

  • Move a tab / window twmove2_opt

  • Stack visited windows and recover them when closing

- The cursor can jumps to the lastest window of the current tab or to the latest [tab, window] pair in history when closing a window. (see `:help g:twcmd_focus_after_closing` for more details.)


  • Using plugin managers (recommended)
    • Vundle : Add Bundle 'yssl/TWcmd.vim' to .vimrc & :BundleInstall
    • NeoBundle : Add NeoBundle 'yssl/TWcmd.vim' to .vimrc & :NeoBundleInstall
    • vim-plug : Add Plug 'yssl/TWcmd.vim' to .vimrc & :PlugInstall
  • Using Pathogen
    • cd ~/.vim/bundle; git clone
  • Manual install (not recommended)
    • Download this plugin and extract it in ~/.vim/


In vim normal mode, type the following command and press <Enter>.
:TWcmd {cmd} {arg}

  • {cmd} : sub-commands for tabs or windows
  • {arg} : vim's :wincmd-style single character arguments

For example,

:TWcmd tcm l    |" go to the right tab
:TWcmd tmv l    |" move the current tab right
:TWcmd wmv h    |" move the current window left
:TWcmd tcm q    |" close current tab

It must be much more convenient to define key mappings for frequently used commands.
My key mapping recommendation is given in the last section of this page.


{cmd} description
tcm tab commands
tmv tab moving commands
wcm window commands
wmv window moving commands
wmvt window moving commands between tabs
twh tab-window history commands


{cmd} {arg} description
tcm / wcm
h Go to the left tab / window.
l Go to the right tab / window.
j Go to the below window. (wcm only)
k Go to the above window. (wcm only)
W Go to the left tab / left or above window. Wraps around from the first to the last one.
w Go to the right tab / right or below window. Wraps around from the last to the first one.
t Go to the first tab / top-left window.
b Go to the last tab / bottom-right window.
p Go to the previous tab / previous window in the current tab.
q Close the current tab / window and go to the latest one in the tab-window history.
n Open a new tab / window.
o Close all other tabs / windows except the current one.
m Toggle maximizing the current window. (wcm only)
  • wcm can be used with all other arguments that used in the vim's :wincmd command.

{cmd} {arg} description
tmv / wmv / wmvt
h Move the current tab left / window left / window to the left tab.
l Move the current tab right / window right / window to the right tab.
j Move the current window below. (wmv only)
k Move the current window above. (wmv only)
W Move the current tab left / window left or above / window to the left tab. Wraps around from the first to the last one.
w Move the current tab right / window right or below / window to the right tab. Wraps around from the last to the first one.
t Move the current tab to first position / window to top-left position / window to the first tab.
b Move the current tab to last position / window to bottom-right position / window to the last tab.
p Move the current tab to previous position / window to previous position / window to the previous tab.

For wmvt,

  • The current tab will be closed after moving its window if it is the only one.
  • If the target tab has only one window with [No Name] title and empty buffer, the moved window will replace it.
  • Otherwise, the moved window will be located as a new vertical split window in the target tab.

{cmd} {arg} description
h Move forward in the tab-window history stack.
l Move backward in the tab-window history stack.

Tab-Window History

When you leave a window in vim, TWcmd.vim pushes [tab_id, window_id] information of the window into its tab-window history stack.

TWcmd.vim moves the cursor to the last focused window using the history stack when closing a window or tab. (see :help g:twcmd_focus_after_closing for more details.) You can also walk through windows in the history with twh sub-command.

Recommended Key Mappings

TWcmd.vim does not provide default key mappings to keep your key mappings clean. Instead, I suggest convenient one what I'm using now. You can add them to your .vimrc and modify them as you want.

function! s:nnoreicmap(option, shortcut, command)
	execute 'nnoremap '.a:option.' '.a:shortcut.' '.a:command
	execute 'imap '.a:option.' '.a:shortcut.' <Esc>'.a:shortcut
	execute 'cmap '.a:option.' '.a:shortcut.' <Esc>'.a:shortcut

" tab cursor moving and managing
call s:nnoreicmap('','<A-H>',':TWcmd tcm h<CR>')
call s:nnoreicmap('','<A-L>',':TWcmd tcm l<CR>')
call s:nnoreicmap('','<A-P>',':TWcmd tcm p<CR>')
call s:nnoreicmap('','<A-Q>',':TWcmd tcm q<CR>')
call s:nnoreicmap('','<A-N>',':TWcmd tcm n<CR>')

" tab moving
call s:nnoreicmap('','<A-J>',':TWcmd tmv h<CR>')
call s:nnoreicmap('','<A-K>',':TWcmd tmv l<CR>')
call s:nnoreicmap('','<A-)>',':TWcmd tmv p<CR>')

" window cursor moving and managing
call s:nnoreicmap('','<A-h>',':TWcmd wcm h<CR>')
call s:nnoreicmap('','<A-j>',':TWcmd wcm j<CR>')
call s:nnoreicmap('','<A-k>',':TWcmd wcm k<CR>')
call s:nnoreicmap('','<A-l>',':TWcmd wcm l<CR>')
call s:nnoreicmap('','<A-p>',':TWcmd wcm p<CR>')
call s:nnoreicmap('','<A-q>',':TWcmd wcm q<CR>')
call s:nnoreicmap('','<A-n>',':TWcmd wcm n<CR>')
call s:nnoreicmap('','<A-v>',':TWcmd wcm v<CR>')
call s:nnoreicmap('','<A-s>',':TWcmd wcm s<CR>')
call s:nnoreicmap('','<A-m>',':TWcmd wcm m<CR>')

" window moving
call s:nnoreicmap('','<A-y>',':TWcmd wmv h<CR>')
call s:nnoreicmap('','<A-u>',':TWcmd wmv j<CR>')
call s:nnoreicmap('','<A-i>',':TWcmd wmv k<CR>')
call s:nnoreicmap('','<A-o>',':TWcmd wmv l<CR>')
call s:nnoreicmap('','<A-0>',':TWcmd wmv p<CR>')

" window moving between tabs
call s:nnoreicmap('','<A-U>',':TWcmd wmvt h<CR>')
call s:nnoreicmap('','<A-I>',':TWcmd wmvt l<CR>')
call s:nnoreicmap('','<A-(>',':TWcmd wmvt p<CR>')

I've define the function s:nnoreicmap() to map for normal, insert and command-line modes simultaneously, and installed vim-fixkey plugin to use alt-key mappings. <A-H> means alt+shift+h.