簡易バッファエクスプローラ

バッファ一覧を表示して、選択して開く

" :ls の結果からバッファ番号を取り出す
function! ExtractBufferNbr(line)
  return matchstr(a:line, "\\d\\+") + 0
endfunction

" 選択したバッファを開く
function! OpenThisBuffer()
	let bufno = ExtractBufferNbr(getline(".")) 
  close 
	exe "b " . bufno
endfunction

" バッファ一覧を表示するウィンドウを作成する
function! ListBuffers()
  let nBuffers = bufnr('$')     " Get the number of the last buffer.
  let i = 0
  let fileNames = ''

  " Preprocess the list of buffers.
  " Find the max buffer name and buffer number.
  while (i <= nBuffers)
    let i = i + 1

    if (getbufvar(i, '&buflisted') == 1)
      let bufName = bufname(i)

      if (bufName != '[BufExplorer]')
        let length = strlen(i)

        let shortBufName = fnamemodify(bufName, ":t")
        let length = strlen(shortBufName)
				call append(line("$"), i . ": " .  shortBufName)
      endif
    endif
  endwhile
endfunction

" ファサード
function! MyBufExpl()
	redir @z
	silent ls
	redir END
	6new
	setlocal buftype=nofile
  setlocal bufhidden=delete
	nnoremap <buffer> <CR> :call OpenThisBuffer()<CR>
	put z
	g/^\s*$/d
endfunction