如何让 Vim 带您回到上次编辑文件时的位置?

发布于 2024-08-09 15:57:33 字数 461 浏览 2 评论 0原文

可能的重复:
在 vim 中,如何在上次关闭文件的行号处打开文件?

如何使 Vim 带您回到上次编辑文件时的位置?

我的工作电脑有这个功能,但我的家用电脑没有!如何设置 Vim 记住上次编辑文件时位于文件的哪个部分?

编辑:更准确地说,我希望在打开新文件或启动时出现这种行为。

Possible Duplicate:
In vim, how do I get a file to open at the same line number I closed it at last time?

How do you make Vim take you back where you were when you last edited a file?

My work computer has this feature, but not my home computer! How do you set Vim to remember in which part of a file you were when you last edited it?

EDIT: just to be more precise, I want this behavior when opening a new file, or on startup.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

白衬杉格子梦 2024-08-16 15:57:33

我的 .vimrc 中有这个并且它有效:

" go to the position I was when last editing the file
au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") | exe "normal g'\"" | endif

I have this in my .vimrc and it works:

" go to the position I was when last editing the file
au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") | exe "normal g'\"" | endif
勿挽旧人 2024-08-16 15:57:33

这是通过 viminfo 文件完成的。只需启用此功能(并确保文件可写)就足够了。我使用:

set viminfo='25,\"50,n~/.viminfo

...它将 viminfo 数据存储到 ~/.viminfo 中。您可以在此处阅读有关其他自定义选项

This is done with the viminfo file. It should be sufficient simply to enable this feature (and ensure that the file is writable). I use:

set viminfo='25,\"50,n~/.viminfo

...which stores viminfo data into ~/.viminfo. You can read about the other customization options here.

阪姬 2024-08-16 15:57:33

首先,检查您的 .vimrc 文件是否可写。

如果这还不够,请将其添加到您的 .vimrc 中:

if has("autocmd")
    autocmd BufReadPost *
    \ if line("\'") > 0 && line("\'") <= line("$") |
        \ exe "normal g`" |
    \ endif
endif

First, check that your .vimrc file is writable.

If that isn't sufficient, add this to your .vimrc:

if has("autocmd")
    autocmd BufReadPost *
    \ if line("\'") > 0 && line("\'") <= line("$") |
        \ exe "normal g`" |
    \ endif
endif
剪不断理还乱 2024-08-16 15:57:33

'0 //(单引号后跟零)带您到上次编辑的位置

'0 // (single quote followed by zero) take you to place you last edited

最舍不得你 2024-08-16 15:57:33

这是一个肮脏的解决方案。

每当您编辑完文件并准备退出时,只需使用 m 标记该位置即可。现在,您使用 mks! 为此创建一个会话文件。下次打开此文件时,只需执行“``即可到达该位置。

您可以使用不同的字母来到达文件中的不同位置。例如,e 代表您上次编辑的位置,或者 f 代表您上次处理的函数定义,等等。这取决于你的口味。

此解决方案的一个缺点是 mks! 将在当前目录中创建一个 Session.vim 文件。

This is a dirty solution.

Whenever you are done editing a file and you are going to quit, just mark that place using m<letter>. Now you create a session file for this using mks!. Next time you open this file, just do ``` to reach that place.

You can have different letters to reach different places in the file. e.g. e for the place you were last editing or f function definition you were last working on etcetera. It depends upon your taste.

One thing bad about this solution is that mks! will create a Session.vim file in the current directory.

沉默的熊 2024-08-16 15:57:33

另请参阅 Vim Wikia 文章 将光标恢复到上一个​​编辑会话中的文件位置。 它看起来像它列出了此处已列出的几个解决方案,以及一个专门存储您编辑的最后 10 个文件的最后位置的较大脚本;您可以更改该值以满足您的需求。

See also the Vim Wikia article on Restore Cursor to File Position in Previous Editing Session. It looks like it lists a couple of the solutions already listed here, in addition to one larger script that specifically stores the last position for the last 10 files you've edited; you can change that value to suit your needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文