【问题标题】:The 'gq' command merge comment lines with non-comment lines'gq' 命令将注释行与非注释行合并
【发布时间】:2013-04-06 09:53:58
【问题描述】:

我现在正在使用 vim 编写 LaTeX 文档,但在使用 'gq' 命令格式化段落时遇到了问题。例如,如果我有这样的段落:

some
text% this is a comment
some
text

'gqap'的结果是:

some text% this is a comment some text

我希望是这样的:

some text% this is a comment
some text

但是,如果评论是独立的,“gq”可以正常工作:

some
text
% this is a comment
some
text

得到:

some text
% this is a comment
some text

我就是不知道是不是vim的bug,也不知道怎么解决……求救

更新:

今天我为'formatexpr'写了一个vim函数来防止以“%%”结尾的vim断行:

function FormatTeX()
    let lnum = v:lnum                             " I found that v:lnum and v:count may change before exiting this function, so I made a copy here
    let lcount = v:count
    let lb = lnum + lcount - 1
    let le = lb
    while lb >= lnum                              " process the file in inverse order, or we have to deal with line number changes
        if match(getline(lb), '%%$') >= 0
            if lb < le
                exec "normal! ".(lb + 1)."GzR"    " the zR here opens all fold, or the result may be wrong 
                exec "normal! gw".le."G"
            endif
            let le = lb - 1
        elseif lb == lnum
            if lcount > 1
                exec "normal! ".lb."GzR"
                exec "normal! gw".le."G"
            else
                return 1                          " when 'formatoptions' has an 'a' flag, this branch is necessary or the cursor will jump unpredictable...
                                                  " according to the source code of vim, if the return value of 'formatexpr' is non-zero, the build-in formatter is used.
            endif
        endif
        let lb = lb - 1
    endwhile
    return 0
endfunction

我希望这个糟糕的例子可以帮助其他面临类似问题的人。

【问题讨论】:

  • 顺便说一句,我想知道如何防止某些特定行的自动格式化:例如,我有一个代表数学表达式的长行,我不希望它被拆分。

标签: vim comments latex format tex


【解决方案1】:

:help format-comments有提示:

Vim 通过行首的特定字符串识别注释 (忽略空白)。

虽然在使用gq 格式化时似乎对三件套cmets 进行了一些特殊处理,但未在行首开始的cmets 处理得并不好。您必须将 gq 格式的范围限制为 cmets 周围的文本。

【讨论】:

  • 非常感谢,我想也许我应该将'formatprg'设置为外部脚本以满足这样的特定需求。
  • +1 这将是正确答案! @SaltyEgg 如果对您有帮助,请不要忘记接受答案。
【解决方案2】:

今天我为'formatexpr'写了一个vim函数来防止以“%%”结尾的vim断行:

function FormatTeX()
    let lnum = v:lnum                             " I found that v:lnum and v:count may change before exiting this function, so I made a copy here
    let lcount = v:count
    let lb = lnum + lcount - 1
    let le = lb
    while lb >= lnum                              " process the file in inverse order, or we have to deal with line number changes
        if match(getline(lb), '%%$') >= 0
            if lb < le
                exec "normal! ".(lb + 1)."GzR"    " the zR here opens all fold, or the result may be wrong 
                exec "normal! gw".le."G"
            endif
            let le = lb - 1
        elseif lb == lnum
            if lcount > 1
                exec "normal! ".lb."GzR"
                exec "normal! gw".le."G"
            else
                return 1                          " when 'formatoptions' has an 'a' flag, this branch is necessary or the cursor will jump unpredictable...
                                                  " according to the source code of vim, if the return value of 'formatexpr' is non-zero, the build-in formatter is used.
            endif
        endif
        let lb = lb - 1
    endwhile
    return 0
endfunction

我希望这个可怜的例子可以帮助其他面临类似问题的人。

【讨论】:

    猜你喜欢
    • 2021-12-21
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 2018-05-31
    • 2018-09-10
    • 2010-12-03
    相关资源
    最近更新 更多