Recent file history in Vim?
Asked Answered
I

15

157

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?

Interlace answered 3/7, 2010 at 11:5 Comment(1)
Similar to question: #310223Sawtelle
A
262

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.

Allysonalma answered 3/7, 2010 at 11:21 Comment(5)
If you want to use 0-9 as marks for navigation,do not mark them manuallyAsyndeton
use :browse old get file-list. and enter q to choose which file to edit.Eliaseliason
:e #<1 opens last file, see :h c_#<.Veronikaveronike
:bro ol works.. but you have to type a lot of keys.. first to get a list of files, then to press q, and then to select a number. I believe the MRU plugin mentioned below is much better: https://mcmap.net/q/150857/-recent-file-history-in-vim you have to press <leader> f, then either browse to the file with hjkl or other keys/search for the file name. Then press o, enter, t, or v to quickly open the file under the cursor.Cubital
I recovered a file i mistakenly delete using rm using the aforementioned method.Anemology
C
70

The best way that I use is

:browse oldfiles

Easiest way on vim.

Canasta answered 10/11, 2013 at 3:14 Comment(3)
Nice! I mapped that expression as "nmap bo :browse oldfiles<CR>" (without the quotations) in my ~/.vimrc. Simply typing that mapping (bo) in Normal mode executes that commandNunley
Any way to change the ordering though? As it is right now, I'm spending a lot of times looking through for something I opened 2 files agoAlbanese
@VictoriaStuart it is better to use mappings without recursion: nnoremap bo :browse oldfiles<CR> when recursion is not needed. nore here means no recursion.Isopiestic
E
46

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

Extempore answered 3/7, 2010 at 11:20 Comment(0)
L
22

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 ...

Larvicide answered 21/1, 2013 at 0:11 Comment(1)
It's useful to know how to actually open a file after you know it's number from the list of :oldfiles. One can do this interactively using :browse command, as explained by @Allysonalma but it's also possible to do this manually by using :e #<N where N is a number from the oldfiles list.Transfuse
R
15

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

Reputation answered 1/7, 2015 at 13:12 Comment(0)
C
11

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.

Carboniferous answered 9/6, 2012 at 19:4 Comment(2)
CtrlP interface is non intuitive for vim users? Not worth the trouble learning the complicated interface to do simple thing as to find the MRU file =~ /regexp/ (easily done with perl/grep regexp ~/_viminfo).Eulau
Yeah.. I have been trying to understand how to use Ctrlp, but MRU seems to be much easier to use, if you have a simple set of files (https://mcmap.net/q/150857/-recent-file-history-in-vim). In MRU you can also do VIM regexp search to find the file you need...Cubital
M
8

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.

Marotta answered 17/5, 2019 at 11:19 Comment(0)
D
5

MRU has lot of features as explained here: http://www.thegeekstuff.com/2009/08/vim-editor-how-to-setup-most-recently-used-documents-features-using-mru-plugin/

Discredit answered 3/7, 2010 at 20:32 Comment(0)
J
5

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.

Jessiejessika answered 14/4, 2014 at 20:0 Comment(1)
For those that are resistant about re-maping Ctrl-P's default functionally: You could open Ctrl-P normally and use Ctrl-F or Ctrl-B to switch between modes (MRU being one of them)Bingle
T
3

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\"','"
Thetic answered 11/1, 2018 at 10:8 Comment(0)
B
2

As seen in the comments here (https://mcmap.net/q/153210/-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
Belligerent answered 30/1, 2012 at 10:56 Comment(3)
Yes, this works. But surely an editor's job is to make things easy? If you have to issue a command, visually identify something, remember an Id, then issue another command that's a real waste of user time. Your answer is correct, and I don't mean offence, but it doesn't make for one of Vim's killer features!Kliber
I had the same problem. have you tried buffexplorer? It combines ls and :tabnew +Nbuf. Now you can open the list of buffers, and open it in a new tab etc.. with one keystroke (o, t, v) etcc.Cubital
Another option would be: nnoremap <Leader>b :buffers<CR>:b<Space>Longley
R
2

: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.

Remarkable answered 12/10, 2018 at 17:45 Comment(0)
T
1

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.

Transfuse answered 14/4, 2014 at 21:12 Comment(1)
Couldn't even get startify to install - need more docs on basic install and usage. Using MRU, because it was easy to install and use.Eulau
A
1

Also you can go back with ctrl+O.

Alialia answered 16/4, 2019 at 7:1 Comment(0)
I
0

Redir function in the repository below will be able to show recent files list in a buffer similar to less. It will be handy with it to find file for opening.

Redirect the output of a Vim or external command into a scratch buffer

I will copy content of the current version of function Redir in the end of current post. Its code needs to be added to ~/.vimrc.

Add mappings to ~/.vimrc:

" show recent files, q to enter file number
nnoremap or :browse oldfiles<CR>
nnoremap rf :Redir browse oldfiles<CR>

In normal mode the following commands can be used:

  • or (open recent file) - to open recent file, q to enter file number to open
  • rf (recent files) - show full list with recent files, to search in it

Code of Redir function:

function! Redir(cmd, rng, start, end)
    for win in range(1, winnr('$'))
        if getwinvar(win, 'scratch')
            execute win . 'windo close'
        endif
    endfor
    if a:cmd =~ '^!'
        let cmd = a:cmd =~' %'
            \ ? matchstr(substitute(a:cmd, ' %', ' ' . shellescape(escape(expand('%:p'), '\')), ''), '^!\zs.*')
            \ : matchstr(a:cmd, '^!\zs.*')
        if a:rng == 0
            let output = systemlist(cmd)
        else
            let joined_lines = join(getline(a:start, a:end), '\n')
            let cleaned_lines = substitute(shellescape(joined_lines), "'\\\\''", "\\\\'", 'g')
            let output = systemlist(cmd . " <<< $" . cleaned_lines)
        endif
    else
        redir => output
        execute a:cmd
        redir END
        let output = split(output, "\n")
    endif
    vnew
    let w:scratch = 1
    setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile
    call setline(1, output)
endfunction

" This command definition includes -bar, so that it is possible to "chain" Vim commands.
" Side effect: double quotes can't be used in external commands
command! -nargs=1 -complete=command -bar -range Redir silent call Redir(<q-args>, <range>, <line1>, <line2>)

" This command definition doesn't include -bar, so that it is possible to use double quotes in external commands.
" Side effect: Vim commands can't be "chained".
command! -nargs=1 -complete=command -range Redir silent call Redir(<q-args>, <range>, <line1>, <line2>)
Isopiestic answered 3/4 at 19:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.