Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a vim command to relocate a tab?

Tags:

vim

tabs

People also ask

How do I move a tab in Vim?

Rearranging tabs If you're really meticulous and want to position tabs just so in Vim, you can move the tabs to a specific spot in the tab order using :tabm n , where n is the position number that you want to use. If you don't give the :tabm command an argument, then the current tab will be moved to the last spot.

How do I find tabs in Vim?

Searching for the Tab Character In NORMAL mode, type /\t and hit <Enter> . It will search for the Tab character ( \t ) and highlight the results.


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.


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.


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>

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

In addition to the fine suggestions in other answers, you can also simply drag tabs with the mouse to move them, if you have mouse support enabled.

This is on by default in MacVim and other GUI vim implementations, whether using the GUI widget tabs or the terminal style tabs in GUI mode.

It also works in pure tty mode Vim, if you have set mouse=a and have a suitable terminal (xterm and most emulators of it, such as gnome-terminal, Terminal.app, iTerm2, and PuTTY/KiTTY, to name a view). Note that mouse clicks beyond column 222 also require set ttymouse=sgr; see In Vim, why doesn't my mouse work past the 220th column? for background on that.

I've written a plugin called vim-tabber that provides some additional functionality for swapping tabs around, shifting them, and adding to the capabilities of the built-in tab manipulation commands, while remaining largely compatible with the builtins. Even if you choose not to use the plugin, there's some general tab usage information in the README.


For some reason, the function answer stopped working for me. I suspect a conflict with vim-ctrlspace. Regardless, the math in the function answer is unnecessary, as Vim can move tabs left and right with built in functions. We just have to handle the wrapping case, because Vim is not user friendly.

" Move current tab into the specified direction.
"
" @param direction -1 for left, 1 for right.
function! TabMove(direction)
    let s:current_tab=tabpagenr()
    let s:total_tabs = tabpagenr("$")

    " Wrap to end
    if s:current_tab == 1 && a:direction == -1
        tabmove
    " Wrap to start
    elseif s:current_tab == s:total_tabs && a:direction == 1
        tabmove 0
    " Normal move
    else
        execute (a:direction > 0 ? "+" : "-") . "tabmove"
    endif
    echo "Moved to tab " . tabpagenr() . " (previosuly " . s:current_tab . ")"
endfunction

" Move tab left or right using Command-Shift-H or L
map <D-H> :call TabMove(-1)<CR>
map <D-L> :call TabMove(1)<CR>

Here's my macro, using relative arguments from @maybeshewill's answer:

" Shortcuts to move between tabs with Ctrl+Shift+Left/Right
function TabLeft()
   if tabpagenr() == 1
      execute "tabm"
   else
      execute "tabm -1"
   endif
endfunction

function TabRight()
   if tabpagenr() == tabpagenr('$')
      execute "tabm" 0
   else
      execute "tabm +1"
   endif
endfunction

map <silent><C-S-Right> :execute TabRight()<CR>
map <silent><C-S-Left> :execute TabLeft()<CR>

It handles the wrapping case.