如何获得同一文件的静态视图?

如何获得同一文件的静态视图?

我正在 vim 中编辑一个文件,我想创建一个新窗口(以类似于的方式:vsplit),其中包含我开始编辑之前的文件视图。

换句话说,我想编辑左边窗口中的文本,但我希望右边窗口中的文本保持不变。

有没有办法在 vim 中做到这一点,或者我是否必须将文件复制到临时文件并在新窗口中打开它?

答案1

您可以创建一个类似于 DiffOrig 的命令(参见 Daniel Andersson 的回答和“:help DiffOrig”),该命令会将原始文件简单地读入新缓冲区:

command ShowOrig vert new | set bt=nofile | r ++edit # | 0d_

要将光标放回原始窗口,请添加wincmd p

command ShowOrig vert new | set bt=nofile | r ++edit # | 0d_ | wincmd p

++edit选项也是根据 MikeSep 的回答和最近发布的“:help DiffOrig”添加的Vim

答案2

:DiffOrig可能是如果使用。它至少会给你一个与原始版本不同的信息:

Since 'diff' is a window-local option, it's possible to view the same buffer
in diff mode in one window and "normal" in another window.  It is also
possible to view the changes you have made to a buffer since the file was
loaded.  Since Vim doesn't allow having two buffers for the same file, you
need another buffer.  This command is useful:
     command DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis
        \ | wincmd p | diffthis
(this is in |vimrc_example.vim|).  Use ":DiffOrig" to see the differences
between the current buffer and the file it was loaded from.

A buffer that is unloaded cannot be used for the diff.  But it does work for
hidden buffers.  You can use ":hide" to close a window without unloading the
buffer.  If you don't want a buffer to remain used for the diff do ":set
nodiff" before hiding it.

答案3

免责声明:我从来不需要这样做,但是......

我认为这句话也许能起到作用:

:new +read\ ++edit\ FILENAME

我还没弄清楚如何自动填充实际文件名...我正在尝试使用文件名修饰符,例如%:p。我认为在切换窗口之前需要将名称存储到变量中。

解释一下,:new打开一个新窗口,然后:read在该窗口内运行,将内容FILENAME直接读入该窗口的缓冲区。缓冲区保持“未命名”,因此不会发生冲突:split FILENAME(只是打开同一缓冲区的另一个副本)。

将其设为缓冲区也许也很有意义scratch(参见的末尾:help buffers),但我还没有对此进行过太多实验。

相关内容