分頁標籤

Nvim 的 :help 頁面,由 產生,來源為 原始碼,並使用 tree-sitter-vimdoc 解析器。


在多個分頁標籤中使用視窗進行編輯。tabpage
此處說明為了使用多個分頁標籤而新增的命令。此外,也說明當與多個分頁標籤組合使用時,運作方式不同的命令。

1. 簡介 tab-page-intro

一個分頁標籤包含一個或多個視窗。您可以輕鬆地在分頁標籤之間切換,以便擁有數個視窗集合來處理不同的事情。
通常您會在 Vim 視窗的頂部看到一個標籤列表,每個分頁標籤一個。您可以使用滑鼠點擊標籤來跳到該分頁標籤。還有其他方法可以在分頁標籤之間移動,請參閱下方。
大多數命令僅在目前的分頁標籤中運作。這包括 CTRL-W 命令、:windo:all:ball (當不使用 :tab 修飾符時)。下方列出會感知目前分頁標籤以外的其他分頁標籤的命令。
分頁標籤也是一種臨時編輯緩衝區的好方法,而不會變更目前的視窗佈局。開啟新的分頁標籤,執行您想執行的操作,然後關閉該分頁標籤。
開啟新的分頁標籤
當啟動 Vim 時,"vim -p filename ..." 會在不同的分頁標籤中開啟每個檔案引數 (最多 'tabpagemax' 個)。請參閱 -p
在非 GUI 分頁標籤列中使用滑鼠雙擊會開啟一個新的空白分頁標籤。它會放置在點擊位置的左側。第一次點擊可能會先選取另一個分頁標籤,導致額外的畫面更新。
這在一些 GUI 版本中也有效,特別是 Win32。但僅限於點擊標籤右側時。
在 GUI 分頁標籤列中,您可以使用滑鼠右鍵開啟選單。tabline-menu
關於相關的自動命令,請參閱 tabnew-autocmd
:[count]tabe[dit] :tabe :tabedit :tabnew :[count]tabnew 在目前分頁標籤之後開啟一個包含空白視窗的新分頁標籤。如果給定 [count],則新分頁標籤會出現在分頁標籤 [count] 之後,否則新分頁標籤會出現在目前分頁標籤之後。
:tabnew     " opens tabpage after the current one
:.tabnew    " as above
:+tabnew    " opens tabpage after the next tab page
            " note: it is one further than :tabnew
:-tabnew    " opens tabpage before the current
:0tabnew    " opens tabpage before the first one
:$tabnew    " opens tabpage after the last one
:[count]tabe[dit] [++opt] [+cmd] {file} :[count]tabnew [++opt] [+cmd] {file} 開啟一個新的分頁標籤並編輯 {file},如同使用 :edit。關於 [count],請參閱上方的 :tabnew
:[count]tabf[ind] [++opt] [+cmd] {file} :tabf :tabfind 開啟新的分頁標籤並在 'path' 中編輯 {file},如同使用 :find。關於 [count],請參閱上方的 :tabnew
:[count]tab {cmd} :tab
執行 {cmd},當它開啟新視窗時,會改為開啟新的分頁標籤。不適用於 :diffsplit:diffpatch:execute:normal。如果給定 [count],則新分頁標籤會出現在分頁標籤 [count] 之後,否則新分頁標籤會出現在目前分頁標籤之後。範例:
:tab split            " opens current buffer in new tab page
:tab help gt    " opens tab page with help for "gt"
:.tab help gt   " as above
:+tab help            " opens tab page with help after the next
                " tab page
:-tab help            " opens tab page with help before the
                " current one
:0tab help            " opens tab page with help before the
                " first one
:$tab help            " opens tab page with help after the last
                " one
CTRL-W gf 開啟新的分頁標籤並編輯游標下的檔案名稱。請參閱 CTRL-W_gf
CTRL-W gF 開啟新的分頁標籤並編輯游標下的檔案名稱,然後跳到檔案名稱後面的行號。請參閱 CTRL-W_gF
關閉分頁標籤
關閉分頁標籤的最後一個視窗也會關閉該分頁標籤,除非只有一個分頁標籤。
使用滑鼠:如果顯示分頁標籤列,您可以點擊右上角的「X」以關閉目前的分頁標籤。自訂的 'tabline' 可能會顯示其他內容。
:tabc :tabclose :tabc[lose][!] 關閉目前的分頁標籤。當發生以下情況時,此命令會失敗:
畫面上只有一個分頁標籤。E784
'hidden' 未設定、未使用 [!]、緩衝區有變更,而且此緩衝區上沒有其他視窗時。對緩衝區的變更不會被寫入,也不會遺失,因此這是一個「安全」的命令。
:tabclose   " close the current tab page
:{count}tabc[lose][!] :tabc[lose][!] {count} 關閉分頁標籤 {count}。失敗方式與上方的 :tabclose 相同。
:-tabclose            " close the previous tab page
:+tabclose            " close the next tab page
:1tabclose            " close the first tab page
:$tabclose            " close the last tab page
:tabclose -2    " close the 2nd previous tab page
:tabclose +            " close the next tab page
:tabclose 3            " close the third tab page
:tabclose $            " close the last tab page
:tabclose #     " close the last accessed tab page
當關閉一個分頁標籤時,下一個分頁標籤會變成目前的分頁標籤。可以使用 'tabclose' 選項自訂此行為。
:tabo :tabonly :tabo[nly][!] 關閉所有其他分頁標籤。當設定 'hidden' 選項時,已關閉視窗中的所有緩衝區都會變成隱藏。當 'hidden' 未設定且設定 'autowrite' 選項時,會寫入已修改的緩衝區。否則,具有已修改緩衝區的視窗不會被移除,除非給定 [!],否則它們會變成隱藏。但是已修改的緩衝區永遠不會被捨棄,因此變更不會遺失。
:tabonly " close all tab pages except the current one
:tabo[nly][!] {count} 關閉除了 {count} 以外的所有分頁標籤。
:.tabonly            " as above
:-tabonly            " close all tab pages except the previous
                " one
:+tabonly            " close all tab pages except the next one
:1tabonly            " close all tab pages except the first one
:$tabonly            " close all tab pages except the last one
:tabonly -            " close all tab pages except the previous
                " one
:tabonly +2     " close all tab pages except the two next
                " one
:tabonly 1            " close all tab pages except the first one
:tabonly $            " close all tab pages except the last one
:tabonly #            " close all tab pages except the last
                " accessed one
切換到另一個分頁標籤
使用滑鼠:如果顯示分頁標籤列,您可以點擊分頁標籤的標籤以切換到該分頁標籤。點擊沒有標籤的地方以移至下一個分頁標籤。'tabline'
:tabn[ext] :tabn :tabnext gt <C-PageDown> CTRL-<PageDown> <C-PageDown> gt i_CTRL-<PageDown> i_<C-PageDown> 移至下一個分頁標籤。從最後一個換至第一個。
:{count}tabn[ext] :tabn[ext] {count} 移至分頁標籤 {count}。第一個分頁標籤的編號為一。
:-tabnext        " go to the previous tab page
:+tabnext        " go to the next tab page
:+2tabnext        " go to the two next tab page
:1tabnext        " go to the first tab page
:$tabnext        " go to the last tab page
:tabnext $        " as above
:tabnext #        " go to the last accessed tab page
:tabnext -        " go to the previous tab page
:tabnext -1        " as above
:tabnext +        " go to the next tab page
:tabnext +1        " as above
{count}<C-PageDown> {count}gt 移至分頁標籤 {count}。第一個分頁標籤的編號為一。
:tabp[revious] :tabp :tabprevious gT :tabN :tabN[ext] :tabNext CTRL-<PageUp> <C-PageUp> <C-PageUp> i_CTRL-<PageUp> i_<C-PageUp> gT 移至上一個分頁標籤。從第一個換至最後一個。
:tabp[revious] {count} :tabN[ext] {count} {count}<C-PageUp> {count}gT 返回 {count} 個分頁標籤。從第一個換至最後一個。請注意,{count} 的用法與 :tabnext 不同,在 :tabnext 中,它被用作分頁標籤編號。
:tabr[ewind] :tabfir :tabfirst :tabr :tabrewind :tabfir[st] 移至第一個分頁標籤。
:tabl :tablast :tabl[ast] 移至最後一個分頁標籤。
<C-Tab> CTRL-<Tab> <C-Tab> g<Tab> g<Tab> CTRL-W_g<Tab> CTRL-W g<Tab> 移至上次存取的分頁標籤。
其他命令::tabs
:tabs 列出分頁標籤及其包含的視窗。目前的視窗顯示一個「>」。已修改的緩衝區顯示一個「+」。例如:
分頁標籤 1
+ tabpage.txt
ex_docmd.c
分頁標籤 2
> main.c
重新排序分頁標籤
:tabm[ove] [N] :tabm :tabmove :[N]tabm[ove] 將目前的分頁標籤移至分頁標籤 N 之後。使用零使目前的分頁標籤成為第一個。N 在移動之前計算,因此如果第二個分頁標籤是目前分頁標籤,則 :tabmove 1:tabmove 2 無效。如果沒有 N,則會將分頁標籤設為最後一個。
:.tabmove        " do nothing
:-tabmove        " move the tab page to the left
:+tabmove        " move the tab page to the right
:0tabmove        " move the tab page to the first
:tabmove 0        " as above
:tabmove        " move the tab page to the last
:$tabmove        " as above
:tabmove $        " as above
:tabmove #        " move the tab page after the last accessed
            " tab page
:tabm[ove] +[N] :tabm[ove] -[N] 將目前的分頁標籤向右移動 N 個位置(使用 +)或向左移動(使用 -)。
:tabmove -        " move the tab page to the left
:tabmove -1        " as above
:tabmove +        " move the tab page to the right
:tabmove +1        " as above
請注意,雖然可以使用 :Ntabmove 將分頁標籤移至第 N 個分頁標籤之後。並使用 :+Ntabmove 將其移動 N 個位置。如需釐清在此內容中 +N 的含義,請參閱 [range]
在分頁標籤上迴圈
:tabd :tabdo :[range]tabd[o] {cmd} 在每個分頁標籤中執行 {cmd},如果給定 [range],則僅在分頁標籤編號位於 [range] 中的分頁標籤中執行。它的作用類似於執行下列操作:
:tabfirst
:{cmd}
:tabnext
:{cmd}
etc.
這只會在每個分頁標籤的目前視窗中運作。當在一個分頁標籤上偵測到錯誤時,將不會再造訪其他分頁標籤。最後一個分頁標籤 (或發生錯誤的地方) 會變成目前的分頁標籤。{cmd} 可以包含 '|' 來串連數個命令。{cmd} 不得開啟或關閉分頁標籤或重新排序它們。另請參閱 :windo:argdo:bufdo:cdo:ldo:cfdo:lfdo

3. 其他項目 tab-page-other

tabline-menu
GUI 分頁標籤列具有快顯選單。可以使用滑鼠右鍵存取。選單項目為:關閉 - 關閉滑鼠指標下的分頁標籤。如果滑鼠指標下沒有標籤,則會關閉目前的分頁標籤。新增分頁標籤 - 開啟分頁標籤,編輯空白緩衝區。它會出現在滑鼠指標的左側。開啟分頁標籤... - 類似於「新增分頁標籤」,並額外使用檔案選取器來選取要編輯的檔案。
差異模式在每個分頁標籤中運作。您可以在一個分頁標籤中看到數個檔案之間的差異。其他分頁標籤可以顯示其他檔案之間的差異。
分頁標籤本機的變數以「t:」開頭。tabpage-variable
目前只有一個選項是分頁標籤本機的:'cmdheight'
tabnew-autocmd
TabLeave 和 TabEnter 自動指令事件可用於在從一個標籤頁切換到另一個標籤頁時執行某些操作。確切的順序取決於您正在執行的操作。當建立新的標籤頁時,其作用如同在同一個緩衝區上建立一個新視窗,然後編輯另一個緩衝區。因此,":tabnew" 會觸發:WinLeave 離開目前的視窗 TabLeave 離開目前的標籤頁 WinEnter 進入新標籤頁中的視窗 TabEnter 進入新的標籤頁 BufLeave 離開目前的緩衝區 BufEnter 進入新的空白緩衝區
當切換到另一個標籤頁時,順序為:BufLeave WinLeave TabLeave WinEnter TabEnter BufEnter
當進入新的標籤頁 (:tabnew) 時,TabNew 會在 TabEnter 之前和 WinEnter 之後觸發。
'tabline' 選項指定帶有標籤頁標籤的行的外觀。它僅在沒有 GUI 標籤頁行時使用。
您可以使用 'showtabline' 選項來指定何時顯示帶有標籤頁標籤的行:永不顯示、當有多個標籤頁時或總是顯示。
標籤頁行的突出顯示由 TabLine、TabLineSel 和 TabLineFill 群組設定。hl-TabLine hl-TabLineSel hl-TabLineFill
對於具有已修改視窗的標籤頁,會顯示一個「+」號。也會顯示標籤頁中的視窗數量。因此,「3+」表示三個視窗,其中一個視窗具有已修改的緩衝區。
'tabline' 選項可讓您定義您偏好的標籤頁標籤方式。這不容易,因此這裡將提供一個範例。
基本知識請參閱 'statusline' 選項。'tabline' 選項中可以使用相同的項目。此外,tabpagebuflist()tabpagenr()tabpagewinnr() 函數也很有用。
由於標籤標籤的數量會有所不同,因此您需要為整個選項使用表達式。類似這樣
:set tabline=%!MyTabLine()
然後定義 MyTabLine() 函數以列出所有標籤頁標籤。一種方便的方法是將其分為兩個部分:首先遍歷所有標籤頁並定義它們的標籤。然後取得每個標籤頁的標籤。
function MyTabLine()
  let s = ''
  for i in range(tabpagenr('$'))
    " select the highlighting
    if i + 1 == tabpagenr()
      let s ..= '%#TabLineSel#'
    else
      let s ..= '%#TabLine#'
    endif
    " set the tab page number (for mouse clicks)
    let s ..= '%' .. (i + 1) .. 'T'
    " the label is made by MyTabLabel()
    let s ..= ' %{MyTabLabel(' .. (i + 1) .. ')} '
  endfor
  " after the last tab fill with TabLineFill and reset tab page nr
  let s ..= '%#TabLineFill#%T'
  " right-align the label to close the current tab page
  if tabpagenr('$') > 1
    let s ..= '%=%#TabLine#%999Xclose'
  endif
  return s
endfunction
現在會為每個標籤頁呼叫 MyTabLabel() 函數以取得其標籤。
function MyTabLabel(n)
  let buflist = tabpagebuflist(a:n)
  let winnr = tabpagewinnr(a:n)
  return bufname(buflist[winnr - 1])
endfunction
這只是一個簡單的範例,會產生一個與預設相似的標籤頁行,但不會為已修改的緩衝區新增「+」號或截斷名稱。當空間不足時,您會希望以聰明的方式縮減標籤的寬度。請檢查 'columns' 選項以了解可用的空間。
當顯示 GUI 標籤頁行時,可以使用 'guitablabel' 來指定每個標籤頁要顯示的標籤。與 'tabline' 一次指定整個標籤頁行不同,'guitablabel' 用於個別標籤。
'guitabtooltip' 非常相似,用於同一標籤的工具提示。它僅在滑鼠指標懸停在標籤上時才會出現,因此通常會更長。但僅在某些系統上支援。
請參閱 'statusline' 選項以了解值的格式。
「%N」項目可用於目前的標籤頁編號。當評估選項時,也會將 v:lnum 變數設定為此編號。使用檔案名稱的項目指的是標籤頁的目前視窗。
請注意,語法突出顯示不適用於此選項。「%T」和「%X」項目也會被忽略。
一個簡單的範例,將標籤頁編號和緩衝區名稱放在標籤中
:set guitablabel=%N\ %f
一個與預設 'guitablabel' 相似的範例:顯示標籤頁中的視窗數量,如果緩衝區已修改,則顯示「+」號
function GuiTabLabel()
  let label = ''
  let bufnrlist = tabpagebuflist(v:lnum)
  " Add '+' if one of the buffers in the tab page is modified
  for bufnr in bufnrlist
    if getbufvar(bufnr, "&modified")
      let label = '+'
      break
    endif
  endfor
  " Append the number of windows in the tab page if more than one
  let wincount = tabpagewinnr(v:lnum, '$')
  if wincount > 1
    let label ..= wincount
  endif
  if label != ''
    let label ..= ' '
  endif
  " Append the buffer name
  return label .. bufname(bufnrlist[tabpagewinnr(v:lnum) - 1])
endfunction
set guitablabel=%{GuiTabLabel()}
請注意,必須先定義函數,然後才能設定選項,否則會收到找不到函數的錯誤訊息。
如果您想要回復為預設標籤,請傳回空字串。
如果您想要針對特定標籤頁顯示某些內容,您可能需要使用標籤頁區域變數。 t:var
主頁
指令索引
快速參考