0

So I'm using the gVim editor to code in c++, and I would like that everytime I re-open gVim the previous tabs+files get restored. I tried adding that to my _vimrc:

autocmd VimLeave * nested let buffernr = bufnr("$") |
\ let buflist = [] |
\ while buffernr > 0 |
\   if buflisted(buffernr) |
\       let buflist += [ bufname(buffernr) ] |
\   endif |
\   let buffernr -= 1 |
\ endwhile |
\ if (!isdirectory($HOME . "/.vim")) |
\   call mkdir($HOME . "/.vim") |
\ endif |
\ call writefile(reverse(buflist), $HOME . "/.vim/buflist.txt")

autocmd VimEnter * nested if argc() == 0 && filereadable($HOME . "/.vim/buflist.txt") |
\   for line in readfile($HOME . "/.vim/buflist.txt") |
\       if filereadable(line) |
\       execute "tabedit " . line |
\       set bufhidden=delete |
\       endif |
\   endfor |
\   tabclose 1 |
\ endif

However this does not work, I took it from here

Any ideas?

Thanks in advance.

Alex
  • 840
  • 7
  • 23
  • 1
    `vim "+'0"` works from the command line. Adapted from https://stackoverflow.com/a/23397895/7976758, found in https://stackoverflow.com/search?q=%5Bvim%5D+last+edited+file – phd May 17 '20 at 17:31
  • 1
    Also think about [sessions](http://vimdoc.sourceforge.net/htmldoc/starting.html#views-sessions); https://stackoverflow.com/search?q=%5Bvim%5D+sessions – phd May 17 '20 at 17:33
  • Thank you @phd however I need to put that inside my vimrc for my vim editor interface gVim. –  May 17 '20 at 18:05
  • 1
    Do not reinvent the wheel. Save session in `VimLeavePre` with `mksession!` and restore it with `gvim -S`. – Matt May 18 '20 at 02:16
  • I have an alias to open the last edited file: `lvim='vim -c "normal '\''0"'` – SergioAraujo May 18 '20 at 15:13
  • @Matt Thank you for your response, could you provide an answer, I can't quite see how I could do that... Thanks. – Alex May 18 '20 at 17:26
  • Have you read `:h Session`? – Matt May 18 '20 at 18:02
  • @Matt No I haven't let me do that now – Alex May 18 '20 at 18:05
  • @Matt Oh actually that's a really good idea! Let me try to do that. I'll post an answer if i managed to get it working... – Alex May 18 '20 at 18:06
  • @Alex you might be interested in tpope’s obsession plugin and the command flag vim -S – D. Ben Knoble May 19 '20 at 13:22

0 Answers0