是否有vim命令来重新定位选项卡?

是否有vim命令来重新定位选项卡?,vim,tabs,Vim,Tabs,如何更改当前选项卡在Vim中的位置/顺序?例如,如果要将当前选项卡重新定位为第一个选项卡?您的意思是移动当前选项卡吗?这可以通过tabmove实现 :tabm[ove] [N] *:tabm* *:tabmove* Move the current tab page to after tab page N. Use zero to make the current

如何更改当前选项卡在
Vim
中的位置/顺序?例如,如果要将当前选项卡重新定位为第一个选项卡?

您的意思是移动当前选项卡吗?这可以通过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.
我有两个键绑定,可以向左或向右移动当前选项卡。非常方便

编辑:这是我的VIM宏。我不是一个大型ViM编码器,所以也许可以做得更好,但这就是它对我的工作原理:

" 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

现在,您可以按F9或F10移动当前选项卡。

我正在寻找相同的选项卡,在一些帖子之后,我找到了一种比函数更简单的方法:

:execute "tabmove" tabpagenr() # Move the tab to the right
:execute "tabmove" tabpagenr() - 2 # Move the tab to the left
返回实际选项卡位置,tabmove使用索引

我将右侧映射为Ctrl+L,将左侧映射为Ctrl+H:

map <C-H> :execute "tabmove" tabpagenr() - 2 <CR>
map <C-J> :execute "tabmove" tabpagenr() <CR>
map:执行“tabmove”tabpagenr()-2
映射:执行“tabmove”tabpagenr()

您可以使用相对或零索引绝对参数重新定位带有
:tabm
的选项卡

绝对:

  • 将选项卡移动到位置i:
    :选项卡i
亲属:

  • 向右移动选项卡i位置:
    :tabm+i
  • 向左移动选项卡i位置:
    :tabm-i

这是一个相对较新的功能。因此,如果它不起作用,请尝试更新您的vim。

除了其他答案中的建议之外,如果您启用了鼠标支持,您还可以简单地用鼠标拖动选项卡来移动它们

在MacVim和其他GUI vim实现中,无论是在GUI模式下使用GUI小部件选项卡还是终端样式选项卡,默认情况下都会启用此选项

如果您有
set mouse=a
并且有一个合适的终端(xterm和它的大多数模拟器,例如gnome terminal、terminal.app、iTerm2和PuTTY/KiTTY,以命名一个视图),那么它也可以在纯tty模式Vim下工作。请注意,鼠标单击第222列以外的位置也需要
设置ttymouse=sgr
;有关背景信息,请参见


我已经编写了一个名为的插件,它提供了一些额外的功能,用于交换标签、移动标签,并添加到内置标签操作命令的功能中,同时保持与内置的基本兼容。即使您选择不使用该插件,自述文件中也有一些常规选项卡使用信息。

由于某种原因,函数answer对我不再起作用。我怀疑这与我的想法有冲突。无论如何,函数答案中的数学是不必要的,因为Vim可以使用内置函数左右移动选项卡。我们只需要处理包装箱,因为Vim对用户不友好

" 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>
将当前选项卡移动到指定方向。 " “@param direction-1表示左侧,1表示右侧。 函数!TabMove(方向) 设s:current_tab=tabpagenr() 设s:total_tabs=tabpagenr($) “从头到尾 如果s:current_tab==1&&a:direction==1 塔布莫夫 “开始吧 elseif s:current\u tab==s:total\u tabs&&a:direction==1 tabmove 0 “正常行动 其他的 执行(a:方向>0?+:“-”)。“移动标签” 恩迪夫 echo“移动到选项卡”。tabpagenr()。“(previosuly.s:当前_选项卡)。” 端功能 “使用命令Shift-H或L向左或向右移动选项卡 映射:调用TabMove(-1) 映射:调用TabMove(1) 将当前选项卡移动到第n个位置 其中
n
是表示位置的数字(从零开始)


将选项卡向左/向右移动 我认为更好的解决方案是将选项卡向左或向右移动到其当前位置,而不是计算您希望它位于的新位置的数值

noremap <A-Left>  :-tabmove<cr>
noremap <A-Right> :+tabmove<cr>
noremap:-tabmove
noremap:+tabmove
使用上述键映射,您将能够移动当前选项卡:

  • 向左使用:Alt+left
  • 向右使用:Alt+right

    • 这是我的宏,使用@maybeshewill答案中的相关参数:

      " 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>
      
      使用Ctrl+Shift+Left/Right在选项卡之间移动的快捷方式 函数TabLeft() 如果tabpagenr()==1 执行“tabm” 其他的 执行“tabm-1” 恩迪夫 端功能 函数TabRight() 如果tabpagenr()==tabpagenr(“$”) 执行“tabm”0 其他的 执行“tabm+1” 恩迪夫 端功能 映射:执行TabRight() 映射:执行TabLeft()
      这是一个很好的问题,加文,因为它有一个微妙之处

      答案来自并主要回答了这个问题——使用“:tabm”和索引或相对(+/-)索引

      不过,请注意,这里可能会出现混淆,其他一些答案可能会忽略这一点。请查看hochl包含的以下帮助文本:

         :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.
      
      关键词是after。我怀疑这有时会让人困惑。为什么?因为如果使用“:tabm3”将选项卡移出第一个位置,则随后键入“:tabm2”不会再次移动选项卡。占据tab#3的文件现在占据tab#2,并且“:tabm2”不会更改顺序

      这是一张图表。所选选项卡为每行的[file_c]

      file_a file_b [file_c] file_d
      :tabm0
      [file_c] file_a file_b file_d
      :tabm3
      file_a file_b [file_c] file_d
      :tabm2
      file_a file_b [file_c] file_d
      
      “:tabm”命令可以被认为是将当前选项卡移动到从0开始计数时当前占据位置的选项卡之后,其中选项卡0是最左侧的选项卡,并且是虚构的。“:tabmi”不会移动到第i个位置——如果移动到第i个位置,“:tabm3”和“:tabm2”将产生不同的结果


      最后,如果你有一个好的键映射,相对移动+/-语法会更简单。

      Hmm。。。所以将标签向左或向右移动一个位置需要脚本?你能粘贴它吗?我添加了我的脚本。这对你有帮助吗?这不行tabm不接受相对参数,尽管它确实应该接受。如果不接受,您应该更新vim,因为
      :tabm
      在vim 7.3中接受相对参数。请注意,绝对位置是零索引,这有点奇怪,因为vim显示的是1索引的选项卡(或者它可能只是我的vim配置)这不会处理记录的包装盒,
      :tabm+/-<
      
         :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.
      
      file_a file_b [file_c] file_d
      :tabm0
      [file_c] file_a file_b file_d
      :tabm3
      file_a file_b [file_c] file_d
      :tabm2
      file_a file_b [file_c] file_d