Recent file history in Vim?

Vim

Vim Problem Overview


I would like to access recent files that I had opened and then closed in GVim. I open and close GVim frequently. I would like to access recent files from previous sessions as well.

Does GVim store recent files somewhere as Word and many other desktop apps store? How to access them?

Vim Solutions


Solution 1 - Vim

At least terminal vim stores the previous ten files into ~/.viminfo in the filemarks section. You can use '0, '1, '2, ... '9 to jump among them.

(Probably only useful for '0 to get back to the last file you were editing, unless your memory is stronger than mine.)

You can also use the :browse oldfiles command to get a menu with numbers.

Solution 2 - Vim

The best way that I use is

:browse oldfiles

Easiest way on vim.

Solution 3 - Vim

There is mru.vim, which adds the :MRU command.

Solution 4 - Vim

Very late answer here ... expounding on @sarnolds answer - You can view the file history with the oldfiles command @see :h oldfiles or :h viminfo

:oldfiles 

Furthermore, you can have fine-grained file management with views and sessions ... @see :h mkview and :h mksession for specifics ...

Solution 5 - Vim

Use :bro ol then press the number that corresponds to the file you want to open.

Solution 6 - Vim

There is an Swiss knife of file switching CtrlP plugin, which is also part of janus distributive. It has :CtrlPMRU command with smart lookup among recently used files.

Note: CtrlP maintains its own list of most recent used files in g:ctrlp_cache_dir."mru/cache.txt". It is not reusing viminfo (set viminfo?) which contains a list of file marks. This is useful if you want to clear this list.

Solution 7 - Vim

Adding my 2 cents here because fzf was was not mentioned in earlier answers, which is such a wonderful tool:
fzf.vim has a :History command that lets you search the most recent used files in a fuzzy and search while you type manner.
I customize the (default) behavior of this command by not letting fzf reorder the search results list to the best match: I want the order of all matching filenames to keep being the order in which these files were last used.
To accomplish this customization, I added the following in my .vimrc to override the default History command defined by the fzf.vim plugin:

    command! -bang -nargs=* History
      \ call fzf#vim#history({'options': '--no-sort'})

EDIT:
Currently I'm using a neovim only plugin telescope.nvim which is very similar to fzf.vim, it has the command :Telescope old_files. And it can use the fzf algorithm as a sorting algorithm in the backend (which is currently recommended over the default sorter).
It looks a bit nicer, but can be a bit slower depending on the context. It is not as mature as fzf, but to me easier to customize, it is all lua script.
If you are a neovim only user, definitely worth checking out imho.

Solution 8 - Vim

Solution 9 - Vim

The CtrlP plugin lets you search through your recently used files as well as files in the current directory with this command:

nnoremap <c-p> :CtrlPMixed<cr>

This saves you the hassle of having to deal with built in Vim commands and the MRU plugin, neither of which let you do fuzzy file searching, which is critical when working on larger projects.

Solution 10 - Vim

You might be able to access the list from the command line with:

grep '^>' ~/.viminfo|cut -c3-|sed 's,~,'"$HOME"','

Explanation:

grep '^>' ~/.viminfo  #find the list of recent files
cut -c3-              #remove the first 2 characters
sed 's,~,'"$HOME"','  #replace ~ with absolute path

You could have a bash alias if you use this regularly

alias vim_mru="grep '^>' ~/.viminfo|cut -c3-|sed 's,~,'\"$HOME\"','"

Solution 11 - Vim

As seen in the comments here (http://stackoverflow.com/questions/571955/undo-close-tab-in-vim), your file is probably still open in a buffer:

:ls " get the buffer number
:tabnew +Nbuf " where N is the buffer number

For example you can reopen the third buffer in a new tab (use :e instead if you don't use tabs):

:tabnew +3buf

Solution 12 - Vim

One more plugin that let's you choose file from the list of last modified ones is staritfy. It replaces your start screen with a list of most recently modified files. You can always open this page later using :Startify command.

Solution 13 - Vim

:ls to list recent files with buffer number on left-hand column.

Then do :b{buffer-number} to jump there.

Example: :ls shows list of files. I want to jump to third-last file I visited. :b3 will take me there.

For faster searching, map :ls to something, e.g. <Leader>. in your .vimrc file.

Solution 14 - Vim

Also you can go back with ctrl+O.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMert NuhogluView Question on Stackoverflow
Solution 1 - VimsarnoldView Answer on Stackoverflow
Solution 2 - Vimandor kesselmanView Answer on Stackoverflow
Solution 3 - VimpmrView Answer on Stackoverflow
Solution 4 - VimEdward J BeckettView Answer on Stackoverflow
Solution 5 - VimnenchevView Answer on Stackoverflow
Solution 6 - VimAlexeyView Answer on Stackoverflow
Solution 7 - VimEmile VrijdagsView Answer on Stackoverflow
Solution 8 - VimthegeekView Answer on Stackoverflow
Solution 9 - VimAndy RayView Answer on Stackoverflow
Solution 10 - VimAlexx RocheView Answer on Stackoverflow
Solution 11 - Vimuser886333View Answer on Stackoverflow
Solution 12 - VimKrzysztof AdamskiView Answer on Stackoverflow
Solution 13 - VimBryan PerezView Answer on Stackoverflow
Solution 14 - VimIvanDiView Answer on Stackoverflow