I like consulting my oldfiles to see what I've been working on recently and to make it easy to find files that are buried deep in various directories. Before I started using the grep and make commands, the oldfiles list only contained files I had intentionally visited, for the most part.

I've recently started using grep and other commands that populate the quickfix list, but most of the matches I end up with are not interesting. However, these non-interesting matches end up polluting my oldfiles list and limiting its usefulness.

Is it possible to exempt files visited by cnext and cprevious from being considered visited from the perspective of oldfiles.

The ideal solution would be something like an argument to cnext or cprevious that controls whether the file counts as visited or not for the purposes of oldfiles, i.e. cnext-silent or something like that if it existed.

" begin .vimrc
nnoremap ( :cprevious<cr>                                                                                                            
nnoremap ) :cnext<cr>
" end .vimrc

For instance, suppose we have GHC (the Glasgow Haskell Compiler) checked out at ~/git/ghc and we search for unwind on a whim by executing :grep -r unwind ..

if we press ) (rebound in the vimrc), this ends up polluting the oldfiles list with things I didn't actually work on.

1: ~/git/ghc/utils/genapply/Main.hs                                                                                
2: ~/git/ghc/rts/StgMiscClosures.cmm
3: ~/git/ghc/rts/StgStartup.cmm
4: ~/git/ghc/rts/win32/veh_excn.c
5: ~/git/ghc/testsuite/tests/typecheck/should_compile/T4524.hs
6: ~/git/ghc/rts/Exception.cmm
7: ~/git/ghc/rts/Libdw.h
8: ~/git/ghc/rts/PrimOps.cmm
9: ~/git/ghc/rts/RaiseAsync.c
10: ~/git/ghc/rts/StgCRun.c
11: ~/git/ghc/compiler/nativeGen/X86/Ppr.hs  

1 Answer 1


A few ideas, none of which do what you actually want, but all rely on increased specificity:

  1. :copen, and be more selective of the matches you select
  2. :grep -r more_specific_pattern more_specific_file_list—this keeps the results relevant. (A favorite technique of mine is to populate the :args list with files I care about—backtick expansion à la
    :args `ack -f --md`
    is particularly handy there—and then to :vimgrep /pattern/ ## to only search those files.)
  3. Try the (new-ish) cfilter plugin with
:packadd cfilter
:Cfilter /pattern/

Actually answering the question: :help oldfiles gives

                                        *v:oldfiles* *oldfiles-variable*
v:oldfiles      List of file names that is loaded from the |viminfo| file on
                startup.  These are the files that Vim remembers marks for.
                The length of the List is limited by the ' argument of the
                'viminfo' option (default is 100).
                When the |viminfo| file is not used the List is empty.
                Also see |:oldfiles| and |c_#<|.
                The List can be modified, but this has no effect on what is
                stored in the |viminfo| file later.  If you use values other
                than String this will cause trouble.
                {only when compiled with the |+viminfo| feature}

So, theoretically, you can do

" ~/.vim/autoload/qf.vim
function! qf#silent(direction) abort
  let command = 'c' . get({
        \ 'n': 'next',
        \ 'p': 'prev'},
        \ direction,
        \ 'next')
  exec command
  call remove(v:oldfiles, -1)

" ~/.vim/vimrc or whatever
nnoremap ) :call qf#silent('n')<CR>
nnoremap ( :call qf#silent('p')<CR>

I don't have much experience defining commands, so there are probably easier ways to do that and get more features out of it.

Also, I'm assuming that v:oldfiles[-1] (the last element) is the current buffer, not the most recent. That might be wrong.

I say theoretically because, well, it seems like it might have little effect (see the quote).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.