Skip to content

Instantly share code, notes, and snippets.

@arcmags
Last active April 14, 2022 06:29
Show Gist options
  • Select an option

  • Save arcmags/9fd6961c88823e058ba06e805f1c9fb9 to your computer and use it in GitHub Desktop.

Select an option

Save arcmags/9fd6961c88823e058ba06e805f1c9fb9 to your computer and use it in GitHub Desktop.
colored vim buffer list
nnoremap <C-w>b :BufList<CR>:buffer<Space>
nnoremap <C-w>B :BufList!<CR>:buffer<Space>
command! -bang BufList call BufList(<q-bang>)
function! BufList(bang)
if a:bang == '!'
let bufs = filter(range(1,bufnr('$')),'bufexists(v:val)')
else
let bufs = filter(range(1,bufnr('$')),'buflisted(v:val)')
endif
let bufslist = []
let bufstab = tabpagebuflist()
let bufnamelen = 0
for buf in bufs
let bufname = bufname(buf)
let buftag = ' '
let buftype = getbufvar(buf,'&buftype')
let filetype = getbufvar(buf,'&filetype')
let bufpath = expand('#'.buf.':p')
if filetype == 'netrw' || bufpath =~ '.*/$'
let filetype = 'netrw'
let buftype = 'netrw'
let bufname = '[netrw]'
elseif buftype == 'help' || bufpath =~ '.*/vim82/doc/.*txt$'
let filetype = 'help'
let buftype = 'help'
let bufname = '[h:'.fnamemodify(bufname,':t:r').']'
elseif buftype == 'quickfix'
let filetype = 'quickfix'
let bufname = '[quickfix]'
elseif buftype == 'nofile'
if bufname =~ '\/.'
let bufname = substitute(bufname,'.*\/\ze.','','')
endif
elseif buftype == 'terminal'
let filetype = 'term'
let bufname = '[terminal]'
if has('nvim')
let bufpath = expand(substitute(b:term_title,'.*:','','')).'/'
else
let bufpath = expand(substitute(term_gettitle(buf),'.*:','','')).'/'
endif
else
let bufname = fnamemodify(bufname,':p:t')
if getbufvar(buf,'&modified') && buftype != 'terminal'
let buftag = '+'
endif
if bufname == ''
let bufname = '[no name]'
endif
endif
let bufnamelen = max([len(bufname),bufnamelen])
call add(bufslist,[buf,buftag,bufname,bufpath])
endfor
for buf in bufslist
echohl Constant
echo printf('%3d ',buf[0])
if buf[0] == bufnr('%')
echohl String
elseif index(bufstab,buf[0]) >= 0
echohl Special
elseif !buflisted(buf[0])
echohl Comment
else
echohl Normal
endif
echon buf[1].printf('%-*s',bufnamelen,buf[2])' '
if buf[0] == bufnr('%') || index(bufstab,buf[0]) >= 0
echohl Type
elseif !buflisted(buf[0])
echohl Comment
else
echohl Normal
endif
echon buf[3]
endfor
echohl NONE
return 1
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment