代码之家  ›  专栏  ›  技术社区  ›  Daniel YC Lin

在vim中写入文件之前,使用外部工具重新格式化文件

vim
  •  0
  • Daniel YC Lin  · 技术社区  · 6 年前

    我要维姆呼叫':!gn format BUILD.gn'当我写BUILD.gn文件时,我的.vimrc是(版本1)

    autocmd BufWritePre  *.{gn,gni} call GnFormat()
    function! GnForamt()
      exec '!gn format %'
    endfunction
    

    但当我按“:w”时它不能工作,它显示错误消息

    Error detected while processing BufWrite Autocommands for "*.gn":
    "BUILD.gn" 67L, 1423C written
    

    我也尝试了以下方法,但也失败了(版本2)

    autocmd BufWritePre  *.{gn,gni} call GnFormat()
    function! GnForamt()
      exec '%!gn format --stdin'
    endfunction
    

    的版本2.vimrc的输出结果 :11verbose w

    Executing BufWrite Autocommands for "*.{gn,gni}"
    autocommand call GnFormat()
    Error detected while processing BufWrite Autocommands for "*.{gn,gni}":
    "BUILD.gn" 27L, 538C written
    Executing BufWritePost Autocommands for "*"
    autocommand cal s:record(expand('<abuf>', 1))
    Executing BufWritePost Autocommands for "*"
    autocommand call s:BufWritePostHook(expand('<afile>', 1))
    

    在版本2中,我可以使用 :call GnForamt() 手动重新格式化缓冲区而不显示任何错误消息。

    只知道原因

    autocmd BufWritePre  *.{gn,gni} call GnFormat()
    function! GnFormat()   " <--- typo of Format as Foramt
      exec '%!gn format --stdin'
    endfunction
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   B.G.    6 年前

    尝试 :debug w :h debug )或者 :13verbose w . 看见 :h verbose :h 'verbose' .

    :[level]verbose 将允许您为单个操作激活详细模式