Is there a vim command to relocate a tab?

前端 未结 7 1603
南笙
南笙 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 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  :call TabMove(-1)
    map  :call TabMove(1)
    

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

提交回复
热议问题