Wikia

Vim Tips Wiki

Changes: Restore screen size and position

Edit

Back to page

(Comments)
(Adjust previous/next navigation + minor manual clean)
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
This Tip is useful if you always want Vim to startup in the same location and size as the time you exit it. Most applications (i.e. FireFox) do this already. Another reason to use this tip is if you often have more than one Vim (GUI on Windows) running. Quite often I will have one per project, each instance editing multiple buffers. When I start Vim, I name it (based on the project or tool I launched it from).
+
This tip is useful if you always want Vim to startup in the same location and size as the time you exit it. Most applications (i.e. FireFox) do this already. Another reason to use this tip is if you often have more than one Vim (GUI on Windows) running. Quite often I will have one per project, each instance editing multiple buffers. When I start Vim, I name it (based on the project or tool I launched it from).
 
<pre>
 
<pre>
 
gvim myFile1 myFile2
 
gvim myFile1 myFile2
Line 22: Line 22:
 
They offer 2 configuration options which can be set at the bottom before the autocmds:
 
They offer 2 configuration options which can be set at the bottom before the autocmds:
 
<pre>
 
<pre>
" To enable the saving and restoring of screen positions
+
" To enable the saving and restoring of screen positions.
let g:screen_size_restore_pos = 1
+
let g:screen_size_restore_pos = 1
   
" To save and restore screen for each Vim instance.
+
" To save and restore screen for each Vim instance.
" This is useful if you routinely run more than one Vim
+
" This is useful if you routinely run more than one Vim instance.
" instance.
+
" For all Vim to use the same settings, change this to 0.
" For all Vim to use the same settings, change this to 0.
+
let g:screen_size_by_vim_instance = 1
let g:screen_size_by_vim_instance = 1
 
 
</pre>
 
</pre>
   
+
==Version 1 (Vim 7 and above)==
==Version 1 (Vim 7.x and above)==
+
This version uses Vim's <tt>readfile()</tt> and <tt>writefile()</tt> functions to access the file used to save the size/position. That in addition to the use of the <tt>split()</tt> function makes this version unusable in Vim 6.x.
This version uses Vim's readfile() and writefile() functions to access the file used to save the size/position. That in addition to the use of the split() function makes this version unusable by Vim 6.x.
 
 
<pre>
 
<pre>
 
if has("gui_running")
 
if has("gui_running")
function! ScreenFilename()
+
function! ScreenFilename()
if has('amiga')
+
if has('amiga')
return "s:.vimsize"
+
return "s:.vimsize"
elseif has('win32')
+
elseif has('win32')
return $HOME.'\_vimsize'
+
return $HOME.'\_vimsize'
else
+
else
return $HOME.'/.vimsize'
+
return $HOME.'/.vimsize'
endif
+
endif
endfunction
+
endfunction
+
function! ScreenRestore()
+
function! ScreenRestore()
" Restore window size (columns and lines) and position
+
" Restore window size (columns and lines) and position
" from values stored in vimsize file.
+
" from values stored in vimsize file.
" Must set font first so columns and lines are based on font size.
+
" Must set font first so columns and lines are based on font size.
let f = ScreenFilename()
+
let f = ScreenFilename()
if has("gui_running") && g:screen_size_restore_pos && filereadable(f)
+
if has("gui_running") && g:screen_size_restore_pos && filereadable(f)
let vim_instance = (g:screen_size_by_vim_instance==1?(v:servername):'GVIM')
+
let vim_instance = (g:screen_size_by_vim_instance==1?(v:servername):'GVIM')
for line in readfile(f)
+
for line in readfile(f)
let sizepos = split(line)
+
let sizepos = split(line)
if len(sizepos) == 5 && sizepos[0] == vim_instance
+
if len(sizepos) == 5 && sizepos[0] == vim_instance
silent! execute "set columns=".sizepos[1]." lines=".sizepos[2]
+
silent! execute "set columns=".sizepos[1]." lines=".sizepos[2]
silent! execute "winpos ".sizepos[3]." ".sizepos[4]
+
silent! execute "winpos ".sizepos[3]." ".sizepos[4]
return
+
return
endif
 
endfor
 
 
endif
 
endif
endfunction
+
endfor
 
function! ScreenSave()
 
" Save window size and position.
 
if has("gui_running") && g:screen_size_restore_pos
 
let vim_instance = (g:screen_size_by_vim_instance==1?(v:servername):'GVIM')
 
let data = vim_instance . ' ' . &columns . ' ' . &lines . ' ' .
 
\ (getwinposx()<0?0:getwinposx()) . ' ' .
 
\ (getwinposy()<0?0:getwinposy())
 
let f = ScreenFilename()
 
if filereadable(f)
 
let lines = readfile(f)
 
call filter(lines, "v:val !~ '^" . vim_instance . "\\>'")
 
call add(lines, data)
 
else
 
let lines = [data]
 
endif
 
call writefile(lines, f)
 
endif
 
endfunction
 
 
if !exists('g:screen_size_restore_pos')
 
let g:screen_size_restore_pos = 1
 
 
endif
 
endif
if !exists('g:screen_size_by_vim_instance')
+
endfunction
let g:screen_size_by_vim_instance = 1
+
  +
function! ScreenSave()
  +
" Save window size and position.
  +
if has("gui_running") && g:screen_size_restore_pos
  +
let vim_instance = (g:screen_size_by_vim_instance==1?(v:servername):'GVIM')
  +
let data = vim_instance . ' ' . &columns . ' ' . &lines . ' ' .
  +
\ (getwinposx()<0?0:getwinposx()) . ' ' .
  +
\ (getwinposy()<0?0:getwinposy())
  +
let f = ScreenFilename()
  +
if filereadable(f)
  +
let lines = readfile(f)
  +
call filter(lines, "v:val !~ '^" . vim_instance . "\\>'")
  +
call add(lines, data)
  +
else
  +
let lines = [data]
  +
endif
  +
call writefile(lines, f)
 
endif
 
endif
autocmd VimEnter * if g:screen_size_restore_pos == 1 | call ScreenRestore() | endif
+
endfunction
autocmd VimLeavePre * if g:screen_size_restore_pos == 1 | call ScreenSave() | endif
+
  +
if !exists('g:screen_size_restore_pos')
  +
let g:screen_size_restore_pos = 1
  +
endif
  +
if !exists('g:screen_size_by_vim_instance')
  +
let g:screen_size_by_vim_instance = 1
  +
endif
  +
autocmd VimEnter * if g:screen_size_restore_pos == 1 | call ScreenRestore() | endif
  +
autocmd VimLeavePre * if g:screen_size_restore_pos == 1 | call ScreenSave() | endif
 
endif
 
endif
 
</pre>
 
</pre>
   
==Version 2 (Vim 6.x and above - like Version 1)==
+
==Version 2 (Vim 6 and above)==
 
Here is an alternative script which uses a regular Vim buffer to manipulate the vimsize file instead of Vim 7's readfile() and writefile() functions.
 
Here is an alternative script which uses a regular Vim buffer to manipulate the vimsize file instead of Vim 7's readfile() and writefile() functions.
 
<pre>
 
<pre>
Line 75: Line 75:
 
" Saves data in a separate file, and so works with multiple instances of Vim.
 
" Saves data in a separate file, and so works with multiple instances of Vim.
 
if has("gui_running")
 
if has("gui_running")
function! ScreenFilename()
+
function! ScreenFilename()
if has('amiga')
+
if has('amiga')
return "s:.vimsize"
+
return "s:.vimsize"
elseif has('win32')
+
elseif has('win32')
return $HOME.'\_vimsize'
+
return $HOME.'\_vimsize'
else
+
else
return $HOME.'/.vimsize'
+
return $HOME.'/.vimsize'
endif
 
endfunction
 
 
function! ScreenRestore()
 
" - Remembers and restores winposition, columns and lines stored in
 
" a .vimsize file
 
" - Must follow font settings so that columns and lines are accurate
 
" based on font size.
 
if !has("gui_running")
 
return
 
endif
 
if g:screen_size_restore_pos != 1
 
return
 
endif
 
let vim_instance = (g:screen_size_by_vim_instance==1?(v:servername):'GVIM')
 
" read any existing variables from .vimsize file
 
silent! execute "sview " . escape(ScreenFilename(),'%#\ $')
 
silent! execute "0/^" . vim_instance . " /"
 
let vim_name = matchstr(getline('.'), '^\w\+')
 
let vim_cols = matchstr(getline('.'), '^\w\+\s\+\zs\d\+')
 
let vim_lines = matchstr(getline('.'), '^\w\+\s\+\d\+\s\+\zs\d\+')
 
let vim_posx = matchstr(getline('.'), '^\w\+\s\+\d\+\s\+\d\+\s\+\zs\d\+')
 
let vim_posy = matchstr(getline('.'), '^\w\+\s\+\d\+\s\+\d\+\s\+\d\+\s\+\zs\d\+')
 
if vim_name == vim_instance
 
execute "set columns=".vim_cols
 
execute "set lines=".vim_lines
 
silent! execute "winpos ".vim_posx." ".vim_posy
 
endif
 
silent! q
 
endfunction
 
 
function! ScreenSave()
 
" used on exit to retain window position and size
 
if !has("gui_running")
 
return
 
endif
 
if !g:screen_size_restore_pos
 
return
 
endif
 
let vim_instance = (g:screen_size_by_vim_instance==1?(v:servername):'GVIM')
 
silent! execute "split " . escape(ScreenFilename(),'%#\ $')
 
silent! execute "0/^" . vim_instance . " /"
 
let vim_name = matchstr(getline('.'), '^\w\+')
 
if vim_name == vim_instance
 
delete _
 
endif
 
$put = vim_instance . ' ' . &columns . ' ' . &lines . ' ' .
 
\ (getwinposx()<0?0:getwinposx()) . ' ' .
 
\ (getwinposy()<0?0:getwinposy())
 
silent! x!
 
endfunction
 
 
if !exists('g:screen_size_restore_pos')
 
let g:screen_size_restore_pos = 1
 
 
endif
 
endif
if !exists('g:screen_size_by_vim_instance')
+
endfunction
let g:screen_size_by_vim_instance = 1
+
  +
function! ScreenRestore()
  +
" - Remembers and restores winposition, columns and lines stored in
  +
" a .vimsize file
  +
" - Must follow font settings so that columns and lines are accurate
  +
" based on font size.
  +
if !has("gui_running")
  +
return
 
endif
 
endif
autocmd VimEnter * call ScreenRestore()
+
if g:screen_size_restore_pos != 1
autocmd VimLeavePre * call ScreenSave()
+
return
  +
endif
  +
let vim_instance = (g:screen_size_by_vim_instance==1?(v:servername):'GVIM')
  +
" read any existing variables from .vimsize file
  +
silent! execute "sview " . escape(ScreenFilename(),'%#\ $')
  +
silent! execute "0/^" . vim_instance . " /"
  +
let vim_name = matchstr(getline('.'), '^\w\+')
  +
let vim_cols = matchstr(getline('.'), '^\w\+\s\+\zs\d\+')
  +
let vim_lines = matchstr(getline('.'), '^\w\+\s\+\d\+\s\+\zs\d\+')
  +
let vim_posx = matchstr(getline('.'), '^\w\+\s\+\d\+\s\+\d\+\s\+\zs\d\+')
  +
let vim_posy = matchstr(getline('.'), '^\w\+\s\+\d\+\s\+\d\+\s\+\d\+\s\+\zs\d\+')
  +
if vim_name == vim_instance
  +
execute "set columns=".vim_cols
  +
execute "set lines=".vim_lines
  +
silent! execute "winpos ".vim_posx." ".vim_posy
  +
endif
  +
silent! q
  +
endfunction
  +
  +
function! ScreenSave()
  +
" used on exit to retain window position and size
  +
if !has("gui_running")
  +
return
  +
endif
  +
if !g:screen_size_restore_pos
  +
return
  +
endif
  +
let vim_instance = (g:screen_size_by_vim_instance==1?(v:servername):'GVIM')
  +
silent! execute "split " . escape(ScreenFilename(),'%#\ $')
  +
silent! execute "0/^" . vim_instance . " /"
  +
let vim_name = matchstr(getline('.'), '^\w\+')
  +
if vim_name == vim_instance
  +
delete _
  +
endif
  +
$put = vim_instance . ' ' . &columns . ' ' . &lines . ' ' .
  +
\ (getwinposx()<0?0:getwinposx()) . ' ' .
  +
\ (getwinposy()<0?0:getwinposy())
  +
silent! x!
  +
endfunction
  +
  +
if !exists('g:screen_size_restore_pos')
  +
let g:screen_size_restore_pos = 1
  +
endif
  +
if !exists('g:screen_size_by_vim_instance')
  +
let g:screen_size_by_vim_instance = 1
  +
endif
  +
autocmd VimEnter * call ScreenRestore()
  +
autocmd VimLeavePre * call ScreenSave()
 
endif
 
endif
 
</pre>
 
</pre>
 
   
 
==Comments==
 
==Comments==
 
Feel free to add comments here for script improvements.
 
Feel free to add comments here for script improvements.
   
For the Version 2 script, since it uses Vim buffers for the manipulations it would be useful to disable all autocmds before opening splitting the buffer and re-enable them when closing the buffer. Many people have lots of plugins installed which can make opening buffers somewhat expensive (and therefore unnecessary time delay). Using the Vim 7 readfile() and writefile() functions avoid this overhead.
+
For the Version 2 script, since it uses Vim buffers for the manipulations it would be useful to disable all autocmds before opening splitting the buffer and re-enable them when closing the buffer. Many people have lots of plugins installed which can make opening buffers somewhat expensive (and therefore unnecessary time delay). Using the Vim 7 readfile() and writefile() functions avoid this overhead.
 
 
----
 

Revision as of 10:57, September 23, 2009

Tip 1569 Printable Monobook Previous Next

created June 4, 2008 · complexity basic · author Dfishburn · version 7.0


This tip is useful if you always want Vim to startup in the same location and size as the time you exit it. Most applications (i.e. FireFox) do this already. Another reason to use this tip is if you often have more than one Vim (GUI on Windows) running. Quite often I will have one per project, each instance editing multiple buffers. When I start Vim, I name it (based on the project or tool I launched it from).

gvim myFile1 myFile2
gvim --servername MyProject filename1 filename2 filename3
gvim --servername VS_NET filename

When I re-open Vim I like its screen position and size to be the same as they were when Vim was closed. The scripts below can be added to your .vimrc file. They offer 2 configuration options which can be set at the bottom before the autocmds:

" To enable the saving and restoring of screen positions.
let g:screen_size_restore_pos = 1

" To save and restore screen for each Vim instance.
" This is useful if you routinely run more than one Vim instance.
" For all Vim to use the same settings, change this to 0.
let g:screen_size_by_vim_instance = 1

Version 1 (Vim 7 and above)

This version uses Vim's readfile() and writefile() functions to access the file used to save the size/position. That in addition to the use of the split() function makes this version unusable in Vim 6.x.

if has("gui_running")
  function! ScreenFilename()
    if has('amiga')
      return "s:.vimsize"
    elseif has('win32')
      return $HOME.'\_vimsize'
    else
      return $HOME.'/.vimsize'
    endif
  endfunction

  function! ScreenRestore()
    " Restore window size (columns and lines) and position
    " from values stored in vimsize file.
    " Must set font first so columns and lines are based on font size.
    let f = ScreenFilename()
    if has("gui_running") && g:screen_size_restore_pos && filereadable(f)
      let vim_instance = (g:screen_size_by_vim_instance==1?(v:servername):'GVIM')
      for line in readfile(f)
        let sizepos = split(line)
        if len(sizepos) == 5 && sizepos[0] == vim_instance
          silent! execute "set columns=".sizepos[1]." lines=".sizepos[2]
          silent! execute "winpos ".sizepos[3]." ".sizepos[4]
          return
        endif
      endfor
    endif
  endfunction

  function! ScreenSave()
    " Save window size and position.
    if has("gui_running") && g:screen_size_restore_pos
      let vim_instance = (g:screen_size_by_vim_instance==1?(v:servername):'GVIM')
      let data = vim_instance . ' ' . &columns . ' ' . &lines . ' ' .
            \ (getwinposx()<0?0:getwinposx()) . ' ' .
            \ (getwinposy()<0?0:getwinposy())
      let f = ScreenFilename()
      if filereadable(f)
        let lines = readfile(f)
        call filter(lines, "v:val !~ '^" . vim_instance . "\\>'")
        call add(lines, data)
      else
        let lines = [data]
      endif
      call writefile(lines, f)
    endif
  endfunction

  if !exists('g:screen_size_restore_pos')
    let g:screen_size_restore_pos = 1
  endif
  if !exists('g:screen_size_by_vim_instance')
    let g:screen_size_by_vim_instance = 1
  endif
  autocmd VimEnter * if g:screen_size_restore_pos == 1 | call ScreenRestore() | endif
  autocmd VimLeavePre * if g:screen_size_restore_pos == 1 | call ScreenSave() | endif
endif

Version 2 (Vim 6 and above)

Here is an alternative script which uses a regular Vim buffer to manipulate the vimsize file instead of Vim 7's readfile() and writefile() functions.

" Restore screen size and position
" Saves data in a separate file, and so works with multiple instances of Vim.
if has("gui_running")
  function! ScreenFilename()
    if has('amiga')
      return "s:.vimsize"
    elseif has('win32')
      return $HOME.'\_vimsize'
    else
      return $HOME.'/.vimsize'
    endif
  endfunction

  function! ScreenRestore()
    " - Remembers and restores winposition, columns and lines stored in
    "   a .vimsize file
    " - Must follow font settings so that columns and lines are accurate
    "   based on font size.
    if !has("gui_running")
      return
    endif
    if g:screen_size_restore_pos != 1
      return
    endif
    let vim_instance = (g:screen_size_by_vim_instance==1?(v:servername):'GVIM')
    " read any existing variables from .vimsize file
    silent! execute "sview " . escape(ScreenFilename(),'%#\ $')
    silent! execute "0/^" . vim_instance . " /"
    let vim_name  = matchstr(getline('.'), '^\w\+')
    let vim_cols  = matchstr(getline('.'), '^\w\+\s\+\zs\d\+')
    let vim_lines = matchstr(getline('.'), '^\w\+\s\+\d\+\s\+\zs\d\+')
    let vim_posx  = matchstr(getline('.'), '^\w\+\s\+\d\+\s\+\d\+\s\+\zs\d\+')
    let vim_posy  = matchstr(getline('.'), '^\w\+\s\+\d\+\s\+\d\+\s\+\d\+\s\+\zs\d\+')
    if vim_name == vim_instance
      execute "set columns=".vim_cols
      execute "set lines=".vim_lines
      silent! execute "winpos ".vim_posx." ".vim_posy
    endif
    silent! q
  endfunction

  function! ScreenSave()
    " used on exit to retain window position and size
    if !has("gui_running")
      return
    endif
    if !g:screen_size_restore_pos
      return
    endif
    let vim_instance = (g:screen_size_by_vim_instance==1?(v:servername):'GVIM')
    silent! execute "split " . escape(ScreenFilename(),'%#\ $')
    silent! execute "0/^" . vim_instance . " /"
    let vim_name  = matchstr(getline('.'), '^\w\+')
    if vim_name == vim_instance
      delete _
    endif
    $put = vim_instance . ' ' . &columns . ' ' . &lines . ' ' .
          \ (getwinposx()<0?0:getwinposx()) . ' ' .
          \ (getwinposy()<0?0:getwinposy())
    silent! x!
  endfunction

  if !exists('g:screen_size_restore_pos')
    let g:screen_size_restore_pos = 1
  endif
  if !exists('g:screen_size_by_vim_instance')
    let g:screen_size_by_vim_instance = 1
  endif
  autocmd VimEnter * call ScreenRestore()
  autocmd VimLeavePre * call ScreenSave()
endif

Comments

Feel free to add comments here for script improvements.

For the Version 2 script, since it uses Vim buffers for the manipulations it would be useful to disable all autocmds before opening splitting the buffer and re-enable them when closing the buffer. Many people have lots of plugins installed which can make opening buffers somewhat expensive (and therefore unnecessary time delay). Using the Vim 7 readfile() and writefile() functions avoid this overhead.

Around Wikia's network

Random Wiki