问题描述:

I'm trying to setup a system where I can establish a "checkpoint" in my Vim code contents such that when I hop back to that state by pressing a keyboard command. If anyone has ever played with an emulator then this is effectively what a save state is. It would be ideal if I can jump back to a state and have my buffers, files and the exact file contents (even if they're not the same contents as the files on disk) back to where they were.

This feature is similar to undo, but the state history can be persisted when vim is closed.

网友答案:

Since version 7.3, Vim has persistent undo. You enable this via

:set undofile

See :help undo-persistence for details.

The "checkpoint identifier" can be retrieved via the :undolist command, or via

:echo undotree().seq_cur

You can then go back to that state [N] via :[N]undo, or the :earlier / :later commands. Or use a plugin like undotree.vim - Display your undo history in a graph or Gundo - Visualize your undo tree, which offer more comfortable browsers for the undo history.

Note that this state is still per-buffer only, Vim only has the unrelated session functionality to group sets of files for editing, but those won't automatically store your checkpoint.

网友答案:

We know what you want but you didn't really explain why you'd want it: what problem are you trying to solve? Persistence between sessions? Not knowing how to use undo?

If you want persistence between sessions, you could use screen or tmux. They are terminal multiplexers that allow you (among other things) to put your current session on hold and get it back days later from a different host or whatever:

$ tmux
$ vim -O foo.js bar.js
C-b d
(time passes, work is done elsewhere…)
$ tmux attach

$ screen
$ vim -O foo.js bar.js
C-a d
(time passes, work is done elsewhere…)
$ screen -r

You could also use Vim's session management:

:mksession
(time passes, work is done elsewhere…)
:source Session.vim
(time passes, work is done elsewhere…)
:q
(time passes, work is done elsewhere…)
$ vim -S

See :help :mksession.

相关阅读:
Top