restore-view.vim (2399B)
1 " FILE: restore_view.vim 2 " Language: vim script 3 " Maintainer: Yichao Zhou (broken.zhou AT gmail dot com) 4 " Version: 1.3 5 " Description: 6 " This is a simple script to autosave cursor position and fold 7 " information using vim's mkview. Although you can easily do this job by 8 " just add serveral line to {.,_}vimrc, write a script plugin can make it 9 " more clean and nice. We assume you use a new enough Vim to enjoy 10 " these feature. Hope you love it:) 11 " 12 " Views will be saved when you save/write a file or EXIT VIM. 13 " 14 " Suggested Setting: 15 " Please put them in you vimrc file. 16 " set viewoptions=cursor,folds,slash,unix 17 " 18 " Set it in a plugin file looks dirty to me. So you'd better do it your 19 " self. This only keywords not in viewoptions is "options". I believe it 20 " does not belong to a view. If you think you need it, feel free to 21 " put it in. If you do not want views of some files to be saved, please 22 " set g:loaded_restore_view. The longer time you use, the bigger view 23 " folder you will have. So if you use UNIX environment, you may need to 24 " use cron to do some clean job. 25 " 26 " Most of code is from wiki. 27 28 29 if exists("g:loaded_restore_view") 30 finish 31 endif 32 let g:loaded_restore_view = 1 33 34 if !exists("g:skipview_files") 35 let g:skipview_files = [] 36 endif 37 38 function! MakeViewCheck() 39 if &l:diff | return 0 | endif 40 if &buftype != '' | return 0 | endif 41 if expand('%') =~ '\[.*\]' | return 0 | endif 42 if empty(glob(expand('%:p'))) | return 0 | endif 43 if &modifiable == 0 | return 0 | endif 44 if len($TEMP) && expand('%:p:h') == $TEMP | return 0 | endif 45 if len($TMP) && expand('%:p:h') == $TMP | return 0 | endif 46 47 let file_name = expand('%:p') 48 for ifiles in g:skipview_files 49 if file_name =~ ifiles 50 return 0 51 endif 52 endfor 53 54 return 1 55 endfunction 56 57 augroup AutoView 58 autocmd! 59 " Autosave & Load Views. 60 autocmd BufWritePre,BufWinLeave ?* if MakeViewCheck() | silent! mkview | endif 61 " the loadview could change foldmethod. in this case foldmethod is 62 " correctly reset using filetype rules 63 " for the lightline part see https://github.com/itchyny/lightline.vim/issues/484 64 autocmd BufWinEnter ?* if MakeViewCheck() | silent! loadview | let &l:filetype = &l:filetype | endif 65 augroup END