4

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

所以我的想法是:

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

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

4

2 回答 2

5

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

:1,$d|0r ~/.hck/input

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

:%!cat /tmp/foo
于 2013-05-21T00:40:12.940 回答
0

你可以使用set autoread. 它检测文件何时在磁盘上发生更改并从文件中重新加载当前缓冲区。因此,您可以保存文件,运行脚本,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<
于 2013-05-21T00:48:52.160 回答