我该如何在
Vim
中更改当前选项卡的位置/顺序?例如,如果我想将当前选项卡重新定位为第一个选项卡?您可以使用 :tabm
命令相对或绝对索引来重新定位标签页。
绝对索引:
:tabm i
相对索引:
:tabm +i
:tabm -i
这是一个相对较新的功能。如果无法正常工作,请尝试更新您的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
接下来,您可以绑定按键,例如在您的.vimrc
文件中这样做:
map <F9> :call TabMove(-1)<CR>
map <F10> :call TabMove(1)<CR>
现在你可以通过按下 F9 或 F10 来移动当前的选项卡。
我也在寻找同样的答案,在看了一些帖子之后,我找到了比函数更简单的方法:
:execute "tabmove" tabpagenr() # Move the tab to the right
:execute "tabmove" tabpagenr() - 2 # Move the tab to the left
tabpagenr()函数返回当前打开标签页的编号,而tabmove命令使用索引进行移动。
我将向右移动映射为Ctrl+L,向左移动映射为Ctrl+H:
map <C-H> :execute "tabmove" tabpagenr() - 2 <CR>
map <C-J> :execute "tabmove" tabpagenr() <CR>
:execute "tabmove" tabpagenr() + 1 <CR>
来向右移动标签页。 - Paul Oliver:tabm n
其中n
是表示位置的数字(从零开始)
我认为更好的解决方案是将选项卡移动到其当前位置的左侧或右侧,而不是计算出您想要它到达的新位置的数字值。
noremap <A-Left> :-tabmove<cr>
noremap <A-Right> :+tabmove<cr>
使用上述按键映射,您将能够移动当前标签页:
+tabmove
和tabmove+
有什么区别吗?位置重要吗? - user4104817set mouse=a
并且有适当的终端(xterm和大多数仿真器,例如gnome-terminal、Terminal.app、iTerm2和PuTTY/KiTTY等),它也可以在纯tty模式Vim中工作。请注意,超过222列的鼠标单击还需要set ttymouse=sgr
; 有关该背景,请参见在Vim中,为什么我的鼠标不能在第220列之后工作?。这是一个很好的问题,Gavin,因为它有一些微妙之处。
来自maybeshewill和hochl的答案大多回答了这个问题--使用带有索引或相对(+/-)索引的":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.
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个位置 -- 如果这样做了,":tabm3" 和 ":tabm2" 将会产生不同的结果。
最终,如果你有一个好的按键映射,相对移动 +/- 语法更简单易用。
" 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>
-tabm
和+tabm
不能正确移动标签。而且我的:help tabm
没有列出这个有效的语法。所以我使用了Pablo的答案,将最后的else execute ... endif
替换为elseif a:direction == 1 execute "tabmove" s:current_tab else execute "tabmove" s:current_tab - 2 endif
。 - cfitabmove
和 -
/+
符号的顺序反转即可。 - rld." 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>
:tabm
命令支持相对参数。 - maybeshewill:tabm+/-
是在Vim 7.3.591中引入的(发布于2012年7月6日),现已进入几乎所有维护的发行版中。 :) - n.st