Vim buffers: status(line) symbol
If you use the Vim editor, sooner or later you run into the concept of text buffers.
I wanted a way to report on Vim statusline 
that I have more than one modifiable buffer opened, this is particularly 
useful when Vim is invoked by some other program (in my case it was 
git send-email --compose --annotate')
Inspired from the crazy statuslines from Got Ravings? I tried a first approach:
set statusline+=%#WarningMsg#%{bufnr('$')>1?'[buffers:'.bufnr('$').']':''}%#StatusLine#
This wasn't exactly what I wanted because bufnr('$') 
takes into account also deleted and unlisted buffers, so a very simple 
filtering function is needed:
function! NBuffers()
  let bufmax = bufnr("$")
  let nbuffers = 0
  let i = 1
  while i <= bufmax
    if buflisted(i) && getbufvar(i, "&modifiable")
      let nbuffers += 1
    endif
    let i += 1
  endwhile
  return nbuffers
endfunction
set statusline+=%#WarningMsg#%{NBuffers()>1?'[buffers:'.NBuffers().']':''}%#StatusLine#
While at it I also added a very handy map to switch between buffers, taken from this discussion:
:map __ :ls<CR>:b
This shows the buffer list and leave the :buffer command open so you can use
wildmenu to select the one you want to switch to.
Just for your reference, this is the full statusline I am using right now:
" Status line setting
set statusline=
set statusline+=[%n]			      " buffer number.
set statusline+=[File:%f]		      " filepath (relative).
set statusline+=[%{strlen(&ft)?&ft:'none'},   " filetype
set statusline+=%{strlen(&fenc)?&fenc:&enc},  " encoding
set statusline+=%{&fileformat}]		      " file format
set statusline+=%h%m%r%w		      " flags
set statusline+=%=			      " separator
function! NBuffers()
  let bufmax = bufnr("$")
  let nbuffers = 0
  let i = 1
  while i <= bufmax
    if buflisted(i) && getbufvar(i, "&modifiable")
      let nbuffers += 1
    endif
    let i += 1
  endwhile
  return nbuffers
endfunction
" highlight if there is more than one buffer opened
"set statusline+=%#WarningMsg#%{bufnr('$')>1?'[buffers:'.bufnr('$').']':''}%#StatusLine#
set statusline+=%#WarningMsg#%{NBuffers()>1?'[buffers:'.NBuffers().']':''}%#StatusLine#
set statusline+=[Row:%l]		      " row
set statusline+=[Col:%v]		      " column
set statusline+=[%3p%%]			      " percentage
set laststatus=2
Have fun with VIM, if you are an heretic too.
 
          

![Valida il feed RSS [RSS Valido]](https://ao2.it/sites/default/files/valid-rss-rogers.png)



Commenti
Invia nuovo commento