Is there a vim command to relocate a tab?

You can relocate a tab with :tabm using either relative or zero-index absolute arguments.

absolute:

  • Move tab to position i: :tabm i

relative:

  • Move tab i positions to the right: :tabm +i
  • Move tab i positions to the left: :tabm -i

It's a relatively new feature. So if it doesn't work try updating your vim.


I was looking for the same and after some posts I found a simpler way than a function:

:execute "tabmove" tabpagenr() # Move the tab to the right
:execute "tabmove" tabpagenr() - 2 # Move the tab to the left

The tabpagenr() returns the actual tab position, and tabmove uses indexes.

I mapped the right to Ctrl+L and the left to Ctrl+H:

map <C-H> :execute "tabmove" tabpagenr() - 2 <CR>
map <C-J> :execute "tabmove" tabpagenr() <CR>

Do you mean moving the current tab? This works using tabmove.

:tabm[ove] [N]                                          *:tabm* *:tabmove*
            Move the current tab page to after tab page N.  Use zero to
            make the current tab page the first one.  Without N the tab
            page is made the last one.

I have two key bindings that move my current tab one left or one right. Very handy!

EDIT: Here is my VIM macro. I'm not a big ViM coder, so maybe it could be done better, but that's how it works for me:

" Move current tab into the specified direction.
"
" @param direction -1 for left, 1 for right.
function! TabMove(direction)
    " get number of tab pages.
    let ntp=tabpagenr("$")
    " move tab, if necessary.
    if ntp > 1
        " get number of current tab page.
        let ctpn=tabpagenr()
        " move left.
        if a:direction < 0
            let index=((ctpn-1+ntp-1)%ntp)
        else
            let index=(ctpn%ntp)
        endif

        " move tab page.
        execute "tabmove ".index
    endif
endfunction

After this you can bind keys, for example like this in your .vimrc:

map <F9> :call TabMove(-1)<CR>
map <F10> :call TabMove(1)<CR>

Now you can move your current tab by pressing F9 or F10.


Move Current Tab to the nth Position

:tabm n

Where n is a number denoting the position (starting from zero)


Move Tabs to the Left / Right

I think a better solution is to move the tab to the left or right to its current position instead of figuring out the numerical value of the new position you want it at.

noremap <A-Left>  :-tabmove<cr>
noremap <A-Right> :+tabmove<cr>

With the above keymaps, you'll be able to move the current tab:

  • To the left using: Alt + Left
  • To the right using: Alt + Right

Tags:

Vim

Tabs