Theo's ʕ•ᴥ•ʔ Park

I made a 'bufferline' with 40 lines of Vimscript!

vim-bufline-demo.gif

I wrote a simple Vimscript function to implement the “bufferline” that many Neovim plugins try to emulate. It goes over all the listed buffers and displays them in the TabLine. I also put the tab (as the tab number) on the right side of the TabLine so that you can use the tab feature if you want to. Make sure to set showtabline=2 if you want the TabLine to be always displayed since this function is overriding the default TabLine. Feel free to modify it as you want and put it in your Vimrc!

Credit: Structure of the code taken from this Stack Overflow thread

 1set showtabline=2
 2
 3function! SpawnBufferLine()
 4  let s = ' hello r/vim | '
 5
 6  " Get the list of buffers. Use bufexists() to include hidden buffers
 7  let bufferNums = filter(range(1, bufnr('$')), 'buflisted(v:val)')
 8  " Making a buffer list on the left side
 9  for i in bufferNums
10    " Highlight with yellow if it's the current buffer
11    let s .= (i == bufnr()) ? ('%#TabLineSel#') : ('%#TabLine#')
12    let s .= i . ' '  " Append the buffer number
13    if bufname(i) == ''
14      let s .= '[NEW]'  " Give a name to a new buffer
15    endif
16    if getbufvar(i, "&modifiable")
17      let s .= fnamemodify(bufname(i), ':t')  " Append the file name
18      " let s .= pathshorten(bufname(i))  " Use this if you want a trimmed path
19      " If the buffer is modified, add + and separator. Else, add separator
20      let s .= (getbufvar(i, "&modified")) ? (' [+] | ') : (' | ')
21    else
22      let s .= fnamemodify(bufname(i), ':t') . ' [RO] | '  " Add read only flag
23    endif
24  endfor
25  let s .= '%#TabLineFill#%T'  " Reset highlight
26
27  let s .= '%=' " Spacer
28
29  " Making a tab list on the right side
30  for i in range(1, tabpagenr('$'))  " Loop through the number of tabs
31    " Highlight with yellow if it's the current tab
32    let s .= (i == tabpagenr()) ? ('%#TabLineSel#') : ('%#TabLine#')
33    let s .= '%' . i . 'T '  " set the tab page number (for mouse clicks)
34    let s .= i . ''          " set page number string
35  endfor
36  let s .= '%#TabLineFill#%T'  " Reset highlight
37
38  " Close button on the right if there are multiple tabs
39  if tabpagenr('$') > 1
40    let s .= '%999X X'
41  endif
42  return s
43endfunction
44
45set tabline=%!SpawnBufferLine()  " Assign the tabline

#vim #vimscript