Vim 如何用文件的内容替换当前缓冲区的内容?

Vim 如何用文件的内容替换当前缓冲区的内容?,vim,Vim,我有一个外部脚本,它接收一个Javascript文件并自动修复一些样式问题,我想在写入之前将其应用于当前缓冲区(BufWritePre,FileWritePre) 因此,我的想法是: w/tmp/foo将当前缓冲区内容写入临时文件 安静!fixStyle/tmp/foo在该文件上运行脚本 将当前缓冲区的内容替换为/tmp/foo 问题是我不知道如何执行第三步。一种方法是删除当前内容(1,$d,即在第1行和最后一行之间删除),并从第0行开始读取目标文件(在第1行之前,以便没有空行): 另一种方

我有一个外部脚本,它接收一个Javascript文件并自动修复一些样式问题,我想在写入之前将其应用于当前缓冲区(
BufWritePre,FileWritePre

因此,我的想法是:

  • w/tmp/foo
    将当前缓冲区内容写入临时文件
  • 安静!fixStyle/tmp/foo
    在该文件上运行脚本
  • 将当前缓冲区的内容替换为
    /tmp/foo

问题是我不知道如何执行第三步。

一种方法是删除当前内容(
1,$d
,即在第1行和最后一行之间删除),并从第0行开始读取目标文件(在第1行之前,以便没有空行):

另一种方法是使用筛选器(本例中为cat)将所有内容(
%
)替换为筛选器的输出:

:%!cat /tmp/foo
你可以用。它检测文件在磁盘上的更改时间,并从文件中重新加载当前缓冲区。因此,您可以保存文件,运行脚本,vim将用更改内容重新加载缓冲区

下面复制了Autoread的帮助

                 *'autoread'* *'ar'* *'noautoread'* *'noar'*
'autoread' 'ar'     boolean (default off)
            global or local to buffer |global-local|
            {not in Vi}
    When a file has been detected to have been changed outside of Vim and
    it has not been changed inside of Vim, automatically read it again.
    When the file has been deleted this is not done.  |timestamp|
    If this option has a local value, use this command to switch back to
    using the global value: >
        :set autoread<
*'autoread'**'ar'**'noautoread'**'noar'*
“自动读取”“ar”布尔值(默认为关闭)
全局或本地到缓冲区|全局本地|
{不在Vi}
当检测到文件已在Vim外部更改时,以及
未在Vim内部更改,自动重新读取。
当文件已被删除时,不会执行此操作|时间戳|
如果此选项具有本地值,请使用此命令切换回
使用全局值:>
:设置自动读取<

它没有:(它是一个外部脚本,我对它没有任何控制权。第二个选项留给我一个空缓冲区(脚本不会向标准输出任何内容,它会修改文件)非常感谢,谢谢。我使用了第二个。如果你不介意的话,你能解释一下每个命令吗?不使用该程序作为筛选器(
:%!fixStyle
)有什么特别的原因吗?脚本不输出任何内容,它只在适当的位置修改文件。
                 *'autoread'* *'ar'* *'noautoread'* *'noar'*
'autoread' 'ar'     boolean (default off)
            global or local to buffer |global-local|
            {not in Vi}
    When a file has been detected to have been changed outside of Vim and
    it has not been changed inside of Vim, automatically read it again.
    When the file has been deleted this is not done.  |timestamp|
    If this option has a local value, use this command to switch back to
    using the global value: >
        :set autoread<