vim - 'gq' 命令将注释行与非注释行合并

标签 vim comments latex format tex

我现在正在使用 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的错误,也不知道如何修复它......任何帮助

更新:

今天我为 '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

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

最佳答案

:help format-comments 有提示:

Vim recognizes a comment by a specific string at the start of the line (ignoring white space).

尽管在使用 gq 进行格式化时似乎对三段注释进行了一些特殊处理,但不处理不在行首开始的注释出色地。您必须将 gq 格式的范围限制为注释周围的文本。

关于vim - 'gq' 命令将注释行与非注释行合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15849443/

相关文章:

vim - vim 与其他程序的接口(interface)

VIM - 同一行上多个命令的第一个命令被忽略

objective-c - Xcode 5 中提供了哪些新的文档命令?

在 Beamer 中包含/排除定义的帧

python - 如何从 python 中检查计算机上是否安装了 LaTeX 和 TeX Live?

latex - 通过其 [名称] 引用类似定理的环境

vim - 我怎样才能保留 Vim 撤消历史但不允许隐藏修改过的缓冲区?

vim - VimScript 中的 "Unclosed expression sequence"错误

php - html5 php 视频上传和显示 - 如何为特定上传添加评论

comments - 如何从 facebook 中的 url 获取所有评论?