避免读取文件成为 Vim 中的备用缓冲区

发布于 2025-01-16 12:15:38 字数 631 浏览 2 评论 0原文

我对 Vim、NeoVim 有疑问,在这种情况下,如果它有影响的话。我有一个 autocmd,它在创建某种类型的文件(在本例中为 Vue 文件)时包含一个样板。这是代码:

autocmd BufNewFile *.vue silent! 0r ~/.dotfiles/skeletons/vue-pug-scss.vue

这个简单的东西效果很好,除了使用 CTRL-^ 时它会返回到样板文件,而我不想要这个。可以这么说,我不希望样板文件出现在我的访问文件历史记录中。

我已经尝试了很多方法,但我很难理解缓冲区列表如何与 CTRL-^ 一起使用。问题是,如果我运行 :ls,样板文件不会出现,即使它是我在执行 CTRL-^ 时得到的。当我重新使用它时,它才会出现在 :ls 中。

我认为可行的是添加 | bd# 但它说没有删除缓冲区。当我使用新创建的文件时, :bd# 不起作用,而 :b# 起作用。这对我来说听起来完全不合逻辑,或者充其量是不一致的。尽管我可能混淆了备用缓冲区和先前的缓冲区之类的东西。

有谁知道我怎样才能做到这一点?

I am having an issue with Vim, NeoVim in this case if it makes a difference. I have an autocmd that includes a boilerplate when creating a file of a certain type, a Vue file in this case. Here is the code:

autocmd BufNewFile *.vue silent! 0r ~/.dotfiles/skeletons/vue-pug-scss.vue

This simple thing works great except when using CTRL-^ it goes back to the boilerplate file and I don't want this. I don't want the boilerplate file to be in my history of visited files so to speak.

I've tried many things but I struggle to understand how the buffer list works together with CTRL-^. The thing is that if I run :ls, the boilerplate file does not appear even though it is what I get when doing CTRL-^. It only appears in :ls once I've been back on it.

What I thought would work is to add | bd# but it says no buffer was deleted. When I am on the newly created file, :bd# does not work whereas :b# works. This sounds completely illogical to me, or inconsistent at best. Although I am probably mixing up an alternate buffer and previous buffer or something.

Does anybody know how I can make this work?

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

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

发布评论

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

评论(2

你如我软肋 2025-01-23 12:15:38

来自 :help :read,强调我的:

如果文件名带有“:r”,则它成为备用文件。这可以是
例如,当您想编辑该文件时使用:“:e!#”。 这个可以
通过从“cpoptions”选项中删除“a”标志来关闭

From :help :read, emphasis mine:

If a file name is given with ":r", it becomes the alternate file. This can be
used, for example, when you want to edit that file instead: ":e! #". This can
be switched off by removing the 'a' flag from the 'cpoptions' option
.

(り薆情海 2025-01-23 12:15:38

这里你有两个子问题:

可以这么说,我不希望样板文件出现在我的访问文件历史记录中。

一些可能的选项(无特定顺序)

  1. 避免使用 :h :read 命令,而使用 :h readfile():h append() 或这样的;
  2. :h 'cpoptions' 中删除 a
  3. :h :keepalt 添加到您的命令中。

请注意,3) 仍然创建一个新缓冲区,尽管它不会成为备用缓冲区。

问题是,如果我运行 :ls,样板文件不会出现

当我使用新创建的文件时,:bd# 不起作用,而 :b# 起作用。这对我来说听起来完全不合逻辑,或者充其量是不一致的。

Vim 中的缓冲区具有“已加载”和“已列出”的布尔状态。两者都是相互独立的。

:h :ls 默认情况下仅显示“列出的”缓冲区。要查看“未列出”的缓冲区,也必须输入 :ls!

:h :bdelete 将缓冲区状态同时更改为“未列出”和“已卸载”。由于您的缓冲区已经“未列出”和“已卸载”,因此您得到了 :h E516。您可以使用 :h :bwipeout 完全删除缓冲区,但在这种情况下按 :h CTRL-^ 将导致 :h E23错误。要设置新的备用缓冲区,您可以输入 :let @# = 42,其中 42 是一些现有缓冲区编号或名称。或者,:balt foobar 可以创建一个名为 foobar 的新卸载列出缓冲区,并将其设置为备用缓冲区。

Here you have two sub-questions:

I don't want the boilerplate file to be in my history of visited files so to speak.

Some possible options in no particular order

  1. Avoid using :h :read command in favor of :h readfile() and :h append() or such;
  2. Remove a from :h 'cpoptions';
  3. Prepend :h :keepalt to your command.

Note that 3) still creates a new buffer, although it doesn't become the alternate one.

The thing is that if I run :ls, the boilerplate file does not appear

When I am on the newly created file, :bd# does not work whereas :b# works. This sounds completely illogical to me, or inconsistent at best.

A buffer in Vim has boolean states of "loaded" and "listed". Both are independent one from another.

:h :ls only shows "listed" buffers by default. To see "unlisted" buffers too one has to type :ls!

:h :bdelete changes buffer state to be "unlisted" and "unloaded" at the same time. As your buffer is already "unlisted" and "unloaded" you've got :h E516. You may use :h :bwipeout to remove a buffer completely, but in this case pressing :h CTRL-^ will result in :h E23 error. To set new alternate buffer you can type :let @# = 42, where 42 is some existing buffer number or name. Alternatively, :balt foobar can create a new unloaded and listed buffer named foobar and set it as the alternate one.

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