diff --git a/autoload/SpaceVim/api/vim/tab.vim b/autoload/SpaceVim/api/vim/tab.vim index e9c0567db..146bb5cbf 100644 --- a/autoload/SpaceVim/api/vim/tab.vim +++ b/autoload/SpaceVim/api/vim/tab.vim @@ -34,16 +34,21 @@ function! s:self.realTabBuffers(id) abort return filter(copy(tabpagebuflist(a:id)), 'buflisted(v:val) && getbufvar(v:val, "&buftype") ==# ""') endfunction +function! s:tab_closed_handle() abort + if expand('') <= get(s:, 'previous_tabpagenr', 0) + let s:previous_tabpagenr -= 1 + endif +endfunction + +" as vim do not support tabpagenr('#') +augroup spacevim_api_vim_tab + autocmd! + autocmd TabLeave * let s:previous_tabpagenr = tabpagenr() + autocmd TabClosed * call tab_closed_handle() +augroup END + function! s:self.previous_tabpagenr() abort - let tabsinfo = self.__cmp.execute('tabs') - let number = 0 - for line in split(tabsinfo, "\n") - if line =~# '^Tab page \d' - let number = str2nr(matchstr(line, '\d\+')) - elseif line =~# '^#' - return number - endif - endfor + return get(s:, 'previous_tabpagenr', 0) endfunction function! SpaceVim#api#vim#tab#get() abort diff --git a/autoload/SpaceVim/default.vim b/autoload/SpaceVim/default.vim index e3b9e58d2..5dcc667be 100644 --- a/autoload/SpaceVim/default.vim +++ b/autoload/SpaceVim/default.vim @@ -9,6 +9,7 @@ scriptencoding utf-8 let s:SYSTEM = SpaceVim#api#import('system') +let s:TAB = SpaceVim#api#import('vim#tab') " Default options {{{ function! SpaceVim#default#options() abort @@ -262,9 +263,9 @@ function! SpaceVim#default#keyBindings() abort cnoremap w " Tabs - nnoremap g0 :tabfirst - nnoremap g$ :tablast - nnoremap gr tabpagenr('#') > 0 ? ':exe "tabnext " . tabpagenr("#")' : '' + nnoremap g0 :tabfirst + nnoremap g$ :tablast + nnoremap gr :call switch_tabs() " Remove spaces at the end of lines nnoremap , :silent! keeppatterns %substitute/\s\+$//e @@ -351,4 +352,11 @@ function! SpaceVim#default#Customfoldtext() abort return prefix . line . expansionString . foldSizeStr . foldPercentage . foldLevelStr endfunction +function! s:switch_tabs() abort + let previous_tab = s:TAB.previous_tabpagenr() + if previous_tab > 0 + exe "tabnext " . previous_tab + endif +endfunction + " vim:set et sw=2: diff --git a/docs/cn/documentation.md b/docs/cn/documentation.md index b8fdf5e98..168d7e05a 100644 --- a/docs/cn/documentation.md +++ b/docs/cn/documentation.md @@ -629,21 +629,26 @@ endfunction 如果只有一个 Tab, Buffers 将被罗列在标签栏上,每一个包含:序号、文件类型图标、文件名。如果有不止一个 Tab, 那么所有 Tab 将被罗列在标签栏上。标签栏上每一个 Tab 或者 Buffer 可通过快捷键 ` number` 进行快速访问,默认的 `` 是 `\`。 -| 快捷键 | 功能描述 | -| ------------ | ------------------ | -| ` 1` | 跳至标签栏序号 1 | -| ` 2` | 跳至标签栏序号 2 | -| ` 3` | 跳至标签栏序号 3 | -| ` 4` | 跳至标签栏序号 4 | -| ` 5` | 跳至标签栏序号 5 | -| ` 6` | 跳至标签栏序号 6 | -| ` 7` | 跳至标签栏序号 7 | -| ` 8` | 跳至标签栏序号 8 | -| ` 9` | 跳至标签栏序号 9 | -| `g r` | 跳至前一个标签序号 | +| 快捷键 | 功能描述 | +| ------------ | --------------------------------------- | +| ` 1` | 跳至标签栏序号 1 | +| ` 2` | 跳至标签栏序号 2 | +| ` 3` | 跳至标签栏序号 3 | +| ` 4` | 跳至标签栏序号 4 | +| ` 5` | 跳至标签栏序号 5 | +| ` 6` | 跳至标签栏序号 6 | +| ` 7` | 跳至标签栏序号 7 | +| ` 8` | 跳至标签栏序号 8 | +| ` 9` | 跳至标签栏序号 9 | +| `g r` | 跳至前一个 Tab,常用于两个 Tab 来回切换 | + +**注意:** 两个缓冲区来回切换的快捷键是 `SPC Tab`, +可阅读[缓冲区管理](#缓冲区管理)部分内容,了解更多缓冲区相关的快捷键。 标签栏上也支持鼠标操作,左键可以快速切换至该标签,中键删除该标签。该特性只支持 Neovim,并且需要 `has('tablineat')` 特性。 +**注意:** 这一特性仅限于Neovim并且 `has('tablineat')` 返回 `true`。 + | 快捷键 | 功能描述 | | ---------------- | ------------ | | `` | 切换至该标签 | diff --git a/docs/documentation.md b/docs/documentation.md index 84f22f6b9..c2564d306 100644 --- a/docs/documentation.md +++ b/docs/documentation.md @@ -652,18 +652,21 @@ the index, bufname and the filetype icon. If there are more than one tab, all tabs will be listed on the tabline. Each item can be quickly accessed by using ` number`. Default `` is `\`. -| Key Bindings | Descriptions | -| ------------ | -------------------------- | -| ` 1` | Jump to index 1 on tabline | -| ` 2` | Jump to index 2 on tabline | -| ` 3` | Jump to index 3 on tabline | -| ` 4` | Jump to index 4 on tabline | -| ` 5` | Jump to index 5 on tabline | -| ` 6` | Jump to index 6 on tabline | -| ` 7` | Jump to index 7 on tabline | -| ` 8` | Jump to index 8 on tabline | -| ` 9` | Jump to index 9 on tabline | -| `g r` | Go to previous tab | +| Key Bindings | Descriptions | +| ------------ | ----------------------------------------------- | +| ` 1` | Jump to index 1 on tabline | +| ` 2` | Jump to index 2 on tabline | +| ` 3` | Jump to index 3 on tabline | +| ` 4` | Jump to index 4 on tabline | +| ` 5` | Jump to index 5 on tabline | +| ` 6` | Jump to index 6 on tabline | +| ` 7` | Jump to index 7 on tabline | +| ` 8` | Jump to index 8 on tabline | +| ` 9` | Jump to index 9 on tabline | +| `g r` | Switch to alternate tab (switch back and forth) | + +**Note:** `SPC Tab` is the key binding for switching to alternate buffer. +Read [Buffers and Files](#buffers-and-files) section for more info. SpaceVim tabline also supports mouse click, left mouse button will switch to buffer, while middle button will delete the buffer.