I would like to have a file that contains all files that I opened with vim in the past. I know that there is the viminfo file but I want to have just a file with the filenames.

For this I would like to write the list of open buffers with autocmd VimLeave to a file.

I tried :ls | w >> test.txt but this does not work correctly.

  • I know that there is the viminfo file Do you also know that there's :oldfiles command? Doesn't it make the whole idea of having "an own file" useless? – Matt Nov 21 '19 at 10:17
  • 4
    That does sound like a XY problem maybe you should ask a question about what you want to do with this list of files and there is probably a better solution to use it than writting it by yourself :) – statox Nov 21 '19 at 10:26
  • 1
    I wonder if sessions would be useful to you – D. Ben Knoble Nov 21 '19 at 13:38

Try using redir command (:h redir)

For example:

func! MyRedir() abort
    redir! > ~/test.txt
    redir END

augroup save_files | au!
    au VimLeave * call MyRedir()
augroup end

  • Or oldfiles instead of ls – D. Ben Knoble Nov 21 '19 at 13:37
  • 1
    There's also execute(), which I often find more convenient than :redir, especially for a single command... – filbranden Nov 21 '19 at 15:43

You can have all buffer names that haven't been unloaded (or wiped out?) with:

let files = filter(map(range(1,bufnr('$')), 'bufname(v:val)'), '!empty(v:val)')

You can save it with

call writefile(files, '/path/to/filename.txt')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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