Is there a vim command to relocate a tab?

前端 未结 7 1599
南笙
南笙 2021-01-29 18:16

How can I change the position / order of my current tab in Vim? For example, if I want to reposition my current tab to be the first tab?

相关标签:
7条回答
  • 2021-01-29 18:57

    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.

    0 讨论(0)
  • 2021-01-29 19:04

    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
    0 讨论(0)
  • 2021-01-29 19:05

    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.

    0 讨论(0)
  • 2021-01-29 19:10

    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>
    
    0 讨论(0)
  • 2021-01-29 19:11

    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>
    
    0 讨论(0)
  • 2021-01-29 19:14

    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.

    0 讨论(0)
提交回复
热议问题