【问题标题】:In vim I want to run astyle on the file i'm on when I save在vim中,我想在保存时对我所在的文件运行astyle
【发布时间】:2012-08-25 22:54:20
【问题描述】:

当我保存在 vim 中时,我希望能够运行“astyle sourceCodeThatImCurrentlyIn.cpp”。那么 :w 和 :!astyle source.c 的组合?这可能吗?

谢谢, 伊杰

【问题讨论】:

    标签: save vim astyle


    【解决方案1】:

    看看 Vim auto-commands:

    :autocmd BufWritePost *.c execute '!astyle' shellescape(expand('%'), 1)
    

    % 替换为当前文件的路径。 BufWritePost 在文件保存后调用。还有BufWritePre在文件保存前做事。

    如果您想永久使用它,请将其放入您的 .vimrc 配置文件中。

    【讨论】:

    • @romainl:正确,已修复。谢谢!
    • 不要使用%。使用 execute '!astyle' shellescape(expand('%'), 1): in !astyle % in % 特殊字符不会被转义(甚至空格也是特殊的),因此在您想要共享的任何代码中它完全无用。
    • 如果我没看错,这只会在 .c 文件上运行 astyle 吗?多种格式怎么样? *.c *.cpp *.cc 等等...?我会这样做吗:auto BufWritePost *.c *.cpp execute '!astyle %'
    • @EjayTumacder 在以.c 结尾的文件上。您可以指定由, 分隔的模式:autocmd BufWritePost *.c,*.cpp,*.cc …,或者,更好的是,执行autocmd BufWritePost * if empty(&bt) && index(['cpp', 'c'], &ft)!=-1 | execute '!astyle' shellescape(@%, 1) | endif:这将对任何标识为 C/C++ 源代码文件的文件执行 astyle(包括 *.cc 和您手动执行的文件 @ 987654337@),但不包括非文件缓冲区(这是empty(&bt) 的用途)。
    猜你喜欢
    • 2020-01-31
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 2018-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    相关资源
    最近更新 更多