TWcmd.vim is a set of useful commands for navigating / moving / managing windows and tabs in vim.
It also provides the ability to jump to the last focused window when closing a window or tab, by managing the tab-window history stack.
-
Navigating windows and tabs
-
Moving the current window to other windows' positions, or even to other tabs
-
Stacking visited windows and recovering them when closing (by using
:TWcmd tcm q
or:TWcmd wcm q
) -
Jumping to the last focused window when closing a window or tab (by using
:TWcmd tcm q
or:TWcmd wcm q
)
- Using plugin managers (recommended)
- Using Pathogen
cd ~/.vim/bundle; git clone https://github.com/yssl/TWcmd.vim.git
- Manual install (not recommended)
- Download this plugin and extract it in
~/.vim/
- Download this plugin and extract it in
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 |
---|---|---|
twh |
h | Move forward in the tab-window history stack. |
l | Move backward in the tab-window history stack. |
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.
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.
For your information, I'm currently using right-alt shortcuts because I used left-alt shortcuts too much and felt pain in my left thumb.
If you are experiencing similar symptoms, I would recommend using the right-alt shortcuts using the following .vimrc setting.
If you just want to use left-alt shortcuts, you can just set let g:left_alt = 1
in the following code.
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
endfunction
let g:left_alt = 0
if g:left_alt
" 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>')
call s:nnoreicmap('','<A-Y>',':TWcmd tcm t<CR>')
call s:nnoreicmap('','<A-)>',':TWcmd tcm b<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-U>',':TWcmd tmv t<CR>')
call s:nnoreicmap('','<A-I>',':TWcmd tmv b<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('','<C-w>',':TWcmd wcm q<CR>')
call s:nnoreicmap('','<A-n>',':TWcmd wcm n<CR>')
call s:nnoreicmap('','<C-v>',':TWcmd wcm v<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-o>',':TWcmd wmv l<CR>')
" window moving between tabs
call s:nnoreicmap('','<A-u>',':TWcmd wmvt h<CR>')
call s:nnoreicmap('','<A-i>',':TWcmd wmvt l<CR>')
else
" tab cursor moving and managing
call s:nnoreicmap('','<A-S>',':TWcmd tcm h<CR>')
call s:nnoreicmap('','<A-G>',':TWcmd tcm l<CR>')
call s:nnoreicmap('','<A-Q>',':TWcmd tcm p<CR>')
call s:nnoreicmap('','<A-C>',':TWcmd tcm q<CR>')
call s:nnoreicmap('','<A-B>',':TWcmd tcm n<CR>')
call s:nnoreicmap('','<A-W>',':TWcmd tcm t<CR>')
call s:nnoreicmap('','<A-T>',':TWcmd tcm b<CR>')
" tab moving
call s:nnoreicmap('','<A-D>',':TWcmd tmv h<CR>')
call s:nnoreicmap('','<A-F>',':TWcmd tmv l<CR>')
call s:nnoreicmap('','<A-E>',':TWcmd tmv t<CR>')
call s:nnoreicmap('','<A-R>',':TWcmd tmv b<CR>')
" window cursor moving and managing
call s:nnoreicmap('','<A-s>',':TWcmd wcm h<CR>')
call s:nnoreicmap('','<A-f>',':TWcmd wcm j<CR>')
call s:nnoreicmap('','<A-d>',':TWcmd wcm k<CR>')
call s:nnoreicmap('','<A-g>',':TWcmd wcm l<CR>')
call s:nnoreicmap('','<A-q>',':TWcmd wcm p<CR>')
call s:nnoreicmap('','<A-c>',':TWcmd wcm q<CR>')
call s:nnoreicmap('','<A-b>',':vnew<CR>')
call s:nnoreicmap('','<A-v>',':TWcmd wcm v<CR>')
call s:nnoreicmap('','<A-x>',':TWcmd wcm m<CR>')
" window moving
call s:nnoreicmap('','<A-w>',':TWcmd wmv h<CR>')
call s:nnoreicmap('','<A-t>',':TWcmd wmv l<CR>')
" window moving between tabs
call s:nnoreicmap('','<A-e>',':TWcmd wmvt h<CR>')
call s:nnoreicmap('','<A-r>',':TWcmd wmvt l<CR>')
endif
I've define the function s:nnoreicmap()
to map for normal, insert and command-line modes simultaneously,
and installed plugin to use alt-key mappings.
<A-H>
means alt+shift+h.