date | tags | ||
---|---|---|---|
2016-02-22T12:59:20+09:00 |
|
I use Vim as my main text editor. It's ubiquitous. I have a minimal .vim/ setup. It moves around with me easily.
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.
function! s:ListOld()
let fn = tempname() . '.listold'
exe 'e ' . fn
"
" create and enter temporary file
exe 'redir @z'
exe 'silent bro ol'
exe 'redir END'
exe 'put=@z'
"
" run `bro ol` and pipe its result into the `z` register
" paste the content of the register in our file
exe '%s/^[0-9]\+: //'
"
" remove the file numbers at the head of the lines
exe 'g/^[^0-9]/d'
exe 'g/^$/d'
exe 'g/^[^~]/d'
exe 'g/EDITMSG/d'
exe 'g/NetrwTreeListing/d'
"
" remove unnecessary lines
setlocal syntax=listold
"
" activate `listold` syntax
call feedkeys("1GO^M== ListOld^M^[j")
call feedkeys(":w^M")
"
" add '== ListOld' header
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
endfunction
command! -nargs=0 ListOld :call <SID>ListOld()
nnoremap <silent> <leader>o :call <SID>ListOld()
"
" when I hit ";o" it lists the recently used files...
au BufRead *.listold set filetype=listold
"
" ensure listold syntax stays with our .listold buffers
The original is at https://github.com/jmettraux/dotvim/blob/3eac19aee/vimrc#L340-L366
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 "ListOld"'
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.
Update: I went for another function, named ListFiles that lists the buffers and the recent files in one go.