VIM:执行复杂文件类型的脚本和显示结果

VIM: Execute script and display result for complex file type

本文关键字:脚本 显示 结果 类型 执行 复杂 文件 VIM      更新时间:2023-10-16

我有一个自定义的非常复杂的文件格式。这种格式可以通过 bash 脚本解析为人类可读的(最终调用 c++ 程序来实际解析它(。

我该怎么办,以便在 vim 尝试读取此文件时,而不是简单地打开它,它应该首先调用此脚本,然后显示其结果而不是文件的内容

将此添加到您的 vimrc 文件中:

autocmd BufRead *.yourext call ParseMyComplexFile()     " When reading your complex files
function ParseMyComplexFile()
    let l:fname = expand('%:t')                     " Copy current file name to l:fname
    enew                                            " Open new empty buffer in place of current one
    setlocal buftype=nofile                         " (scratch buffer) New buffer is not related to a file and will not be written
    setlocal bufhidden=hide                         " (scratch buffer) Don't unload the buffer when it is hidden
    setlocal noswapfile                             " (scratch buffer) No swap file for this buffer
    " Execute your script on the file and change the name of the buffer to 'parsed <filename>'
    silent execute "read !yourscript.sh " . l:fname         
    silent execute "file parsed " . l:fname         
    1d                                              " Delete the first unwanted empty row
    1                                               " Position to first row
endfunction

yourextyourscript.sh替换为正确的值。

您将有两个新缓冲区:一个包含原始文件,另一个包含解析的输出(这将是当前缓冲区(。