174

Is there a way to view the list of recent documents you've opened in Vim?

I realize I could view the cursor jump list, :ju, and then go to a cursor position in the list but this is not ideal because there will be multiple listings of the same document in the list.

Is there another command which would do what I'm looking for?

informatik01
  • 16,038
  • 10
  • 74
  • 104
chris
  • 2,740
  • 3
  • 25
  • 23

10 Answers10

249

Don't use a plugin, unless you want a nice menu. From Vim Documentation: Starting (or :help old):

:ol[dfiles]

Then to open one of the listed files, use: '0, '1, '2, ... '9


List the files that have marks stored in the viminfo file.

:bro[wse] ol[dfiles][!]

List file names as with :oldfiles, and then prompt for a number. When the number is valid that file from the list is edited. Use ! to abandon a modified buffer.

Lucas Pottersky
  • 1,844
  • 6
  • 22
  • 38
Alex Bolotov
  • 8,781
  • 9
  • 53
  • 57
  • 1
    Thank you for your answer but when I view the help for Starting this command does not exist like it does in the link you supplied. Also attempting to use this command yields the error "Not an editor command". Why is this? – chris Feb 08 '11 at 17:46
  • 1
    Works for me. @chris You know that the bits inside the brackets are optional, right? i.e. ":bro ol" is an abbreviation of ":browse oldfiles" – Mr_Chimp Sep 22 '11 at 16:04
  • 31
    To open one of the listed files, use: '0, '1, '2, ... '9. (I had to go to another question to find that answer, so I'm posting it here.) – Stew Oct 08 '13 at 22:01
  • 4
    @Stew link to that question? Those don't seem to work for me. [UPDATE] actually it works for me with `:browse oldfiles` – sorry! – Aaron Gibralter Feb 20 '14 at 06:11
  • 1
    @Stew : thanks for your comment which showed me the way but with a slightly different approach. If I press ' then I get options for paging through the list only when I've paged to the bottom of the list am I prompted to asking me for a file number, then the file opens ! – glaucon Jun 16 '15 at 00:07
  • @Stew: I should have made it clearer that in my case the list that results from "bro ol" is more than one screens worth - I guess that's not true for everyone. – glaucon Jun 16 '15 at 00:13
  • Any idea how to get ":bro ol" to display files that have been opened via Quickfix? When I open a document via quick fix, it doesn't appear in the bro ol list? – nenchev Jul 01 '15 at 19:57
  • 12
    You don't have to scroll all the way to the bottom of the old file list in order to open a listed file. You can remember the number then press q to exit. Then in normal mode, press '0, '1, '2, ... to open the file. I often find pressing q to be faster than scrolling to the bottom. – James Lawson Dec 14 '15 at 16:36
  • @JamesLawson That must be a part of the answer! – Loves Probability Feb 17 '17 at 08:31
  • For me, I have to run `sudo chmod a+wr ~/.viminfo` to make this work. Information on recent files are stored in `~/.viminfo`, whose permission by default is `-rw-------` in my case. – user27490 Jun 17 '19 at 08:28
  • I've just added `nnoremap h :browse oldfiles` to my vimrc – SergioAraujo Mar 21 '20 at 18:51
  • This seems cool, but the list definitely isn't complete. There are tonnes of files that I know I created recently which are missing. What's the deal? – Ben Farmer Apr 04 '20 at 02:52
  • 1
    Pressing `'` simply returns to Normal mode. How can I open a file from the list on vim 8? – Luís de Sousa Jun 11 '20 at 08:00
  • @BenFarmer the oldfiles list is only updated once you have exited vim. Look at the answer below for MRU (Most Recently Used) plugin if you want options to do so from within the current session. – Chris Jun 09 '21 at 07:43
  • @LuísdeSousa literally press a number key afterwards. `'` is to reference a mark, and the numbers listed in oldfiles can be referenced as special marks. See: https://vim.fandom.com/wiki/Using_marks – Chris Jun 09 '21 at 07:47
31

The Most Recently Used (MRU) plugin provides an easy access to a list of recently opened/edited files in Vim. This plugin automatically stores the file names as you open/edit them in Vim.

http://www.vim.org/scripts/script.php?script_id=521

Craig Lebakken
  • 1,853
  • 15
  • 10
  • 2
    That works nicely. I guess I'm surprised to learn a plugin is necessary. – chris Nov 21 '08 at 18:53
  • 2
    The nice think about MRU rather than the built-in command, is that it allows filtering on file names, and tab completion, rather than dealing with a clunky giant list every time. – Ben Aug 15 '14 at 15:04
28

Besides :oldfiles, fzf.vim has :History.

user1338062
  • 11,939
  • 3
  • 73
  • 67
20

Start Vim and hit Ctrl-o-o to open previously edited file. Keep hitting o (while still pressing the Ctrl key) to cycle back through earlier files. See https://dev.to/jovica/3-little-known-but-useful-vim-tips-1pbg

Lyle Z
  • 1,269
  • 10
  • 7
2

vim plugin: minibufexpl may help you. the opened file list is displayed on the top or bottom of the screen:

enter image description here

in vim normal mode, type :b${bufid} to jump to the ${bufid}_th buffer, for example: type :b13 to jump to the 13th buffer, ie. ngx_mail_ssl_module.c.

besidies, you can map some convenient bindings in your vimrc, such as:

" ------------------------------- minibufexpl mappings -----------------------------------
"let g:miniBufExplSplitBelow=1
nnoremap <silent> <leader>bn :bn<cr>
nnoremap <silent> <leader>bp :bp<cr>
nnoremap <silent> <leader>bf :bf<cr>
nnoremap <silent> <leader>bl :bl<cr>
nnoremap <silent> <leader>bt :TMiniBufExplorer<cr>

Get the plugin from here: https://github.com/fholgado/minibufexpl.vim

ZHOU Ling
  • 564
  • 1
  • 7
  • 13
2

In addition to oldfiles there's a nice thing called tinyMRU.

Vim-tinyMRU's only purpose is to provide an intuitive alternative to the built-in :oldfile command. Nothing more, nothing less.

It's very simple:

https://github.com/romainl/vim-tinyMRU/blob/master/plugin/tinymru.vim

shmup
  • 884
  • 9
  • 19
2

A good plugin is https://github.com/Shougo/denite.nvim

You can call :Denite file_old in order to have fuzzy search on the list of old files. In particular, just hitting Enter will re-open the last opened file. Assigning a shortcut to this is useful:

nnoremap <leader>o :Denite<space>file_old<CR>

This saves few keystrokes compared to :browse oldfiles, q, 1, Enter

Hope
  • 1,051
  • 13
  • 17
1

:ol works, but why not use fuzzy search to get the exact match quickly from a long list of file?

There's a very handy plugin, ctrlp which allows you to use :CtrlPMRU, and you can quickly get what you looking for.

For many many years, ctrlp is one of the first thing I would install for vim!

https://github.com/ctrlpvim/ctrlp.vim

ken
  • 13,869
  • 6
  • 42
  • 36
0

The easiest way for me to access recent files is to add the following to one's .gvimrc file:

let g:netrw_sort_by           = 'time'

let g:netrw_sort_direction    = 'r'

These lines get netrw to sort files by those most recently modified. Then one simply calls :e. and selects the file one wants.

This solution presupposes files are saved in one main directory so specified in .gvimrc. E.g.

cd ~/vim
Nick
  • 138,499
  • 22
  • 57
  • 95
Edman
  • 157
  • 6
0

No directly the answer but related.

you can define aliases to open the last opened file(s) by vim:

alias vil='vim -c "normal! '\''0"' # open the last file
alias vil1='vim -c "normal! '\''1"'  # open the second last file ...
alias vil2='vim -c "normal! '\''2"'
Ahmad
  • 8,811
  • 11
  • 76
  • 141