Хорошие новости, :mkview
уже имеет это (см. Выдержку из документации ниже).
Наиболее конкретно :loadview
восстанавливает положение прокрутки, а также состояние сворачивания, если viewoptions
включает cursor,folds
.
Еще лучше то, что вы можете прозрачно включать представления для всех открытых файлов, если хотите. Например. чтобы включить сохранение представления для всех исходных файлов C, добавьте это в $ MYVIMRC:
au BufWinLeave *.c mkview
au BufWinEnter *.c silent loadview
Редактировать В соответствии с комментариями Хонъяна, в сочетании с некоторыми плагинами, это может работать лучше, если вы использовали команду 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.