vim - 在 VIM 中折叠 C 预处理器

标签 vim folding

是否可以在 VIM 中折叠 C 预处理器。例如:

#if defined(DEBUG)
  //some block of code
  myfunction();
#endif

我想折叠它,使它成为:
 +--  4 lines: #if defined(DEBUG)---

最佳答案

由于 Vim 的高亮引擎的限制,这很重要:它不能很好地高亮重叠区域。在我看来,您有两个选择:

  • 使用语法高亮和 contains=选项,直到它适合您(可能取决于某些插件):
    syn region cMyFold start="#if" end="#end" transparent fold contains=ALL
    " OR
    syn region cMyFold start="#if" end="#end" transparent fold contains=ALLBUT,cCppSkip
    " OR something else along those lines
    " Use syntax folding
    set foldmethod=syntax
    

    这可能需要很多麻烦,而且您可能永远无法令人满意地工作。把这个放在 vimfiles/after/syntax/c.vim~/.vim/after/syntax/c.vim .
  • 使用折叠标记。这会起作用,但您将无法折叠牙套或其他任何您可能喜欢的东西。把这个放在 ~/.vim/after/ftplugin/c.vim (或 Windows 上的等效 vimfiles 路径):
    " This function customises what is displayed on the folded line:
    set foldtext=MyFoldText()
    function! MyFoldText()
        let line = getline(v:foldstart)
        let linecount = v:foldend + 1 - v:foldstart
        let plural = ""
        if linecount != 1
            let plural = "s"
        endif
        let foldtext = printf(" +%s %d line%s: %s", v:folddashes, linecount, plural, line)
        return foldtext
    endfunction
    " This is the line that works the magic
    set foldmarker=#if,#endif
    set foldmethod=marker
    
  • 关于vim - 在 VIM 中折叠 C 预处理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2407920/

    相关文章:

    vim - 使用 :browse old IF no file is specified as a cli argument 启动 vim

    c++ - 如何在 vim 中创建映射以自动执行 .h C++ 文件的 ifdef 命令

    vim - 如何激活vim折叠标记?

    vim - vim中的自定义折叠

    vim/NERDtree/folding - 它能记住折叠的状态吗?

    java - Vim java折叠无法识别折叠

    vim - 从管道命令的输出中使用 vim 打开文件

    Vim 多链命令,第一个使用范围

    vim - 将当前行发送到 vim 中的外部命令(不捕获)

    基于空白深度的 Notepad++ 折叠