vim :ListOld

The initial version of this post was about a ListOld function. After a few days, I changed my mind about it and rewrote as ListFiles, including current buffers and recently opened files in a single "view". The original post is still visible.

I use Vim as my main text editor. It's ubiquitous. I have a minimal .vim/ setup. It moves around with me easily, as long as I have ssh and git, I can get comfortable on a host very quickly.

I needed a way to access the most recently updated files. :bro old is painful to use.

Here is a vimscript function that pipes the output of :bro old in a file then lets you roam in it with j and k and then hit <space> to open a file. Use the standard ctrl-6 or ctrl-^ to get back in the file list.

It also outputs the result of :buffers, so you're presented with a list of currently open buffers followed by a list of recently opened files. I find it convenient for quickly navigating or getting back into context.

function! s:ListFiles()

  exe 'silent bwipeout ==ListFiles'
    " close previous ListFiles if any

  exe 'new | only'
    " | only makes it full window
  exe 'file ==ListFiles'
    " replace buffer name
  exe 'setlocal buftype=nofile'
  exe 'setlocal bufhidden=hide'
  exe 'setlocal noswapfile'
  exe 'setlocal nobuflisted'

  exe 'redir @z'
  exe 'silent echo "== recent"'
  exe 'silent echo ""'
  exe 'silent bro ol'
  exe 'redir END'
  exe 'silent 0put z'
    " list recently opened files, put at top

  exe 'redir @y'
  exe 'silent echo "== buffers"'
  exe 'silent echo ""'
  exe 'silent buffers'
  exe 'redir END'
  exe 'silent 0put y'
    " list currently open buffers, put at top

  exe '%s/^\s\+\d\+[^\"]\+"//'
  exe '%s/"\s\+line /:/'
  exe 'g/^Type number and /d'
  exe 'g/COMMIT_EDITMSG/d'
  exe 'g/NetrwTreeListing/d'
  exe 'silent %s/^[0-9]\+: //'
    " remove unnecessary lines and ensure format {filepath}:{linenumber}

  call feedkeys('1Gjj')
    " position just above the first buffer, if any

  setlocal syntax=listold
    " syntax highlight as per ~/.vim/syntax/listold.vim

  nmap <buffer> o gF
  nmap <buffer> <space> gF
  nmap <buffer> <CR> gF
    " hitting o, space or return opens the file under the cursor
    " just for the current buffer


command! -nargs=0 ListFiles :call <SID>ListFiles()
nnoremap <silent> <leader>b :call <SID>ListFiles()<CR>
  " when I hit ";b" it shows my list

The original is at

Here is an example output:

I move up and down with k and j and hit space to open the file under the cursor. I hit ctrl-6 (;; in my setting) to get back to the list of files.

I also added:

alias vo='vim -c "ListFiles"'
to my .bashrc so that vo fires up Vim directly in this list of files.

This script is condensed from a series of google searches and stackoverflow scans. I felt like quitting in the middle, but there is always an answer somewhere that unlocks it all.