1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

vimのタブ化

Last updated at Posted at 2019-07-21

vim標準のタブ機能

元々vimには1プロセスで複数ファイルをバッファで管理するという機能があります。これをタブで表示するという事も出来ちゃいます。

tabpage - Vim日本語ドキュメント

ですが、help見てもらえれば分かる通り、タブ操作のコマンドが煩わしい。。と感じた方がいて、とても便利なrc scriptを公開してくれている方がいます。

vimのすごい便利なのにあまり使われていない「タブページ」機能

頻繁に使用するであろう以下の機能が簡易キー入力で操作できるようになる。これはいい。
201907212220.png

以下のvim scriptを.vimrcに追記すれば使えるようになります。

.vimrc
"------------------------------------------------------------
"Tab pages Scripts

" Anywhere SID.
function! s:SID_PREFIX()
    return matchstr(expand('<sfile>'), '<SNR>\d\+_\zeSID_PREFIX$')
endfunction

" Set tabline.
function! s:my_tabline() "{{{
    let s= ''
    for i in range(1, tabpagenr('$'))
        let bufnrs = tabpagebuflist(i)
        let bufnr = bufnrs[tabpagewinnr(i) - 1]  " first window, first appears
        let no = i  " display 0-origin tabpagenr.
        let mod = getbufvar(bufnr, '&modified') ? '!' : ' '
        let title = fnamemodify(bufname(bufnr), ':t')
        let title = '[' . title . ']'
        let s .= '%'.i.'T'
        let s .= '%#' . (i == tabpagenr() ? 'TabLineSel' : 'TabLine') . '#'
        let s .= no . ':' . title
        let s .= mod
        let s .= '%#TabLineFill# '
    endfor
    let s .= '%#TabLineFill#%T%=%#TabLine#'
    return s
endfunction "}}}
let &tabline = '%!'. s:SID_PREFIX() . 'my_tabline()'
set showtabline=2 " 常にタブラインを表示

" The prefix key.
nnoremap    [Tag]   <Nop>
nmap    t [Tag]

" Tab jump
for n in range(1, 9)
  execute 'nnoremap <silent> [Tag]'.n  ':<C-u>tabnext'.n.'<CR>'
endfor

" t1 で1番左のタブ、t2 で1番左から2番目のタブにジャンプ
map <silent> [Tag]c :tablast <bar> tabnew<CR>
" tc 新しいタブを一番右に作る
map <silent> [Tag]x :tabclose<CR>
" tx タブを閉じる
map <silent> [Tag]n :tabnext<CR>
" tn 次のタブ
map <silent> [Tag]p :tabprevious<CR>
" tp 前のタブ

"---------- End tab pages Script

コマンドモードの時に

  • tc = タブページの追加
  • tx = タブページの削除
  • t = 表示するタブの切り替え

タブの表示位置の切り替えとか、他にも機能はありますが、それは正直ほとんど使用しないと思われます、現時点では。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?