Ответ 1
Хорошие новости, :mkview
уже есть (см. выдержки из документации ниже).
В частности, :loadview
восстанавливает положение прокрутки, а также состояние складывания, если viewoptions
включает cursor,folds
.
Еще лучшая новость заключается в том, что вы можете прозрачно включать представления для всех открытых файлов, если хотите. Например. для включения сохранения вида для всех исходных файлов C, добавьте это в $MYVIMRC:
au BufWinLeave *.c mkview
au BufWinEnter *.c silent loadview
Изменить. Согласно комментариям Hongying в сочетании с некоторыми плагинами, это может сработать лучше, если вы использовали команду VimEnter
auto для загрузки представления.
Опционально используйте параметр viewdir
для определения местоположения для сохраненных представлений.
Обязательно посмотрите на :mksession
, потому что он еще более эффективен, так как он может восстанавливать несколько окон, вкладок и их позиций, сопоставлений, регистров, параметров, состояния сбрасывания и т.д. и т.д..
Как это работает
Vim :mkview
сохраняет ex-команды для восстановления местоположения следующим образом:
silent! normal! zE
let s:l = 88 - ((4 * winheight(0) + 4) / 9)
if s:l < 1 | let s:l = 1 | endif
exe s:l
normal! zt
88
normal! 025l
:loadview
просто запускает эти команды, как любой vimscript.
Из документов
Примечание. Это вырезано из документов, убедитесь, что прочитано больше he :mkview
*:mkvie* *:mkview*
:mkvie[w][!] [file] Write a Vim script that restores the contents of the
current window.
When [!] is included an existing file is overwritten.
When [file] is omitted or is a number from 1 to 9, a
name is generated and 'viewdir' prepended. When the
last directory name in 'viewdir' does not exist, this
directory is created.
An existing file is always overwritten then. Use
|:loadview| to load this view again.
When [file] is the name of a file ('viewdir' is not
used), a command to edit the file is added to the
generated file.
The output of ":mkview" contains these items:
1. The argument list used in the window. When the global argument list is
used it is reset to the global list.
The index in the argument list is also restored.
2. The file being edited in the window. If there is no file, the window is
made empty.
3. Restore mappings, abbreviations and options local to the window if
'viewoptions' contains "options" or "localoptions". For the options it
restores only values that are local to the current buffer and values local
to the window.
When storing the view as part of a session and "options" is in
'sessionoptions', global values for local options will be stored too.
4. Restore folds when using manual folding and 'viewoptions' contains
"folds". Restore manually opened and closed folds.
5. The scroll position and the cursor position in the file. Doesn't work very
well when there are closed folds.
6. The local current directory, if it is different from the global current
directory.