避免读取文件成为 Vim 中的备用缓冲区
我对 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自
:help :read
,强调我的:From
:help :read
, emphasis mine:这里你有两个子问题:
一些可能的选项(无特定顺序)
:h :read
命令,而使用:h readfile()
和:h append()
或这样的;:h 'cpoptions'
中删除a
;:h :keepalt
添加到您的命令中。请注意,3) 仍然创建一个新缓冲区,尽管它不会成为备用缓冲区。
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:
Some possible options in no particular order
:h :read
command in favor of:h readfile()
and:h append()
or such;a
from:h 'cpoptions'
;:h :keepalt
to your command.Note that 3) still creates a new buffer, although it doesn't become the alternate one.
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
, where42
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.