vimで開いたファイルの履歴をたどる
vimでは開いたファイルはバッファリストに記憶されてるので:bnとかで前のバッファとか開けるんですけど、これは全ウインドウ共通なので別ウインドウ(分割とかタブで開いたウインドウ)のバッファもごちゃまぜになります。ウインドウ単位で開いたファイルの履歴をたどりたいときがあるんですけど、どうすればいいかわからんかったので自分で書きました。
" ウインドウ単位で開いたファイルの履歴をたどる
function! FileJumpPush()
if !exists('w:histories')
let w:histories = []
endif
let buf = bufnr('%')
if count(w:histories, buf) == 0
call add(w:histories, buf)
endif
endfunction
function! FileJumpPrev()
if exists('w:histories')
let buf = bufnr('%')
let current = match(w:histories, '^'.buf.'$')
if current != 0 && exists('w:histories[current - 1]')
execute 'buffer ' . w:histories[current - 1]
endif
endif
endfunction
function! FileJumpNext()
if exists('w:histories')
let buf = bufnr('%')
let current = match(w:histories, '^'.buf.'$')
if exists('w:histories[current + 1]')
execute 'buffer ' . w:histories[current + 1]
endif
endif
endfunction
augroup FileJumpAutoCmd
autocmd!
augroup END
autocmd FileJumpAutoCmd BufReadPre * call FileJumpPush()
nnoremap <silent> ,p :<C-u>call FileJumpPrev()<CR>
nnoremap <silent> ,n :<C-u>call FileJumpNext()<CR>
前に開いたファイルだと新しくリストに追加しないので、開いた順にたどります。なんかまだ微妙に動作が気にくわないところがあるのでちょいちょい直すと思います。
ちなみに似たようなのに、CTRL-o/i
があります。これは:jumps
に記録されているジャンプリストをたどっていけるんですが、ファイルの履歴とはちょっと異なります。CTRL-^
も微妙に違うんですよね。
- Prev Entry:mysqlのSTRICT_ALL_TABLES
- Next Entry:snipMate.vimのsnippetを書いた時点で再読み込みする