0

On Windows 10, I installed gVim 8.1, so far I have made the following progress to make it usable for me:

  1. I made changes directly to _vimrc file, in spite I know there is also a gVim specific configuration file, because I like it in one place.

  2. I downloaded a color theme I really liked, and use it like so (for CLI version, the #3 below should be applied too):

    " load Atomified theme downloaded from:
    " http://vimcolors.com/797/atomified/dark
    " https://github.com/Siphalor/vim-atomified
    
    colorscheme atomified
    
  3. In CLI version, I found there is a limited number of colors used, this can be remedied with:

    " switch CLI to full color support
    
    set termguicolors
    
  4. I turned on line numbering with:

    " turn on line numbering
    
    set number
    
  5. Regarding the GUI only, I have set:

    • to remove toolbar, as it is too small anyways on my HiDPI display, rendering it useless

    • to set proper font and its size for my HiDPI display

    • maximize gVim on startup

      " Do the following only if launching GUI VIM
      
      if has('gui_running')
      
        " remove toolbar, as it is too small anyways on my HiDPI display, rendering it useless
        set guioptions-=T
      
        " set proper font and its size for my HiDPI display
        set guifont=Consolas:h10
      
        " maximize gVim on startup
        " use ~x on an English Windows version, other languages see Alt + Spacebar for underlined character in your Close
        autocmd GUIEnter * simalt ~x
      
      endif
      

Thus far no problem, but once I added the following code to remember and restore open files on startup, it won't maximize fully, and it is also strangely resizing the main window down the startup:

" Go to last file(s) if invoked without arguments.
autocmd VimLeave * nested if (!isdirectory($HOME . "/.vim")) |
    \ call mkdir($HOME . "/.vim") |
    \ endif |
    \ execute "mksession! " . $HOME . "/.vim/Session.vim"

autocmd VimEnter * nested if argc() == 0 && filereadable($HOME . "/.vim/Session.vim") |
    \ execute "source " . $HOME . "/.vim/Session.vim"

What did I make wrong and how to remedy it?

0

I found working, but completely different code on Wiki, from where I also used the strangely working code in the question.

As I don't fully understand it, I can't tell, if it's alright or not, but from today's testing it passed.

" Open last active file(s) if VIM is invoked without arguments

" This saves all open files / tabs on close
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")


" This restores all open files / tabs on startup
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
0

It seems the original solution has one or more flaws. I noticed that the saving procedure writes relative paths and / or only file names, which was a big problem, because when then loading the file list, of course it could not find the file(s).

This line is responsible for writing the file paths (names) into buflist.txt.

\     let buflist += [ bufname(buffernr) ] |

I have re-written it, so that it writes full path to the file:

\     let buflist += [ fnamemodify(bufname(buffernr), ':p') ] |

And it so far, after an hour of testing, it works without error.

I will leave the original solution in place in order for you to be able to compare it, and if possible, revise it even better.


The whole, current, solution follows:

" Open last active file(s) if VIM is invoked without arguments

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


" This restores the tabs on startup
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

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.