37

I know how to set the initial window size in gVim, but how do I get it to automatically restore the window dimensions from last session? Is this even possible?

Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
Charles Roper
  • 20,125
  • 20
  • 71
  • 101

6 Answers6

16

Edit: Corrected my answer. The mentioned winsize sessionoption only refers to the vim internal window layout, not the external dimensions.


If you use

:mksession

and load the session on vim startup with

gvim -S Session.vim

you can include the window position and size into the session by including winpos and resize in the sessionoptions, see

:help  'sessionoptions

With autocommands you could even automate the saving and restoring of the session on Vim entry and exit.

user55400
  • 3,929
  • 1
  • 21
  • 13
  • 1
    Thanks for the answer! I'm still quite new to Vim, so I'm not terribly experienced with autocommands. How would I get them to fire on entry and exit? – Charles Roper Feb 27 '09 at 14:38
  • See :help autocommand, especially the GUIEnter, VIMEnter, VIMLeave events look promising. – user55400 Feb 27 '09 at 14:42
  • 1
    Session saving also saves last opened files and tends to ignore these, when opening single files with Vim. There's a script in Wikia, which does it in a cleaner manner (i.e. without messing around with other session elements): http://vim.wikia.com/wiki/Restore_screen_size_and_position – Andrei Sosnin Sep 09 '11 at 13:39
12

These lines save and restore just the position and size:

set sessionoptions+=resize,winpos
autocmd VIMEnter * :source C:/session.vim
autocmd VIMLeave * :mksession! C:/session.vim
Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
Andrej Mitrović
  • 3,322
  • 3
  • 20
  • 33
  • 4
    This a nice idea, but session saving also saves last opened files and tends to ignore these, when opening single files with Vim. There's a script in Wikia, which does it in a cleaner manner (i.e. without messing around with other session elements): http://vim.wikia.com/wiki/Restore_screen_size_and_position – Andrei Sosnin Sep 09 '11 at 13:37
7

Additionally:

In your .vimrc:

set ssop+=resize,winpos,winsize,blank,buffers,curdir,folds,help,options,tabpages

Then, use the script from this article. It works beautifully!

Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
4

I had the same question, and to expand on the above answer, you can simply add the following to your .vimrc to get the behaviour you want:

set sessionoptions+=resize,winpos

see :h ssop

Hotschke
  • 9,402
  • 6
  • 46
  • 53
Jeremy Sharpe
  • 345
  • 1
  • 3
  • 12
2
gvim -geom 85x55

as in, putting this in your .bashrc:

alias G='gvim -geom 85x55'
kajaco
  • 2,547
  • 3
  • 24
  • 33
1

If you just want vim to open to the same size every time, you can edit your user's vimrc in C:/Users/<yourUserName>/_vimrc (this is preferred to editing the system _vimrc in your vim installation folder) to include the line set lines=<yourHeight> columns=<yourWidth>

Aaron P
  • 31
  • 4