【问题标题】:How to prevent wrapping string in quotes while enable auto-wrap in vim?在vim中启用自动换行时如何防止将字符串换行?
【发布时间】:2016-12-20 13:50:56
【问题描述】:

我倾向于启用自动换行:

:set textwidth=80
:set formatoptions+=wt

但是当我用 C 或 javascript 编码时,当我在引号中输入一个长字符串时,我不想换行,因为它会出错; 我可以配置我的 vim 自动换行排除引号吗? 还是在换行之前自动输入“\”?

【问题讨论】:

    标签: vim vi


    【解决方案1】:

    您可以从我编写的这个小脚本开始,并添加一些改进以满足您的需求:

    """""the event that will trigger the wrap (leaving insert mode)
    au InsertLeave * call WrapLines()
    """""highlight the column where the wrapping will be made
    set colorcolumn=30
    """""WrapLines will be executed on lines
    function! WrapLines()
    execute ":%g/^/ call WrapFunction()"
    endfunction
    
    """""check and wrap the line
    function! WrapFunction()
        let l:line=getline(".")
        let l:length=strlen(l:line)
        let l:occurence=0
        let l:i=0
        let l:nb=30
        for l:i in split(l:line,'\zs')
            if matchstr(l:i,'"') != ''
                let l:occurence+=1
        let l:occurence=l:occurence % 2
            endif
    
       let l:nb-=1
    if l:nb == 0
    break
    endif
        endfor
        if l:length >= 30
            if l:occurence == 0
    """""to get ^M you need to type <ctrl-v><enter> buttons
                normal! 30|i^M
            endif
        endif
    endfunction
    

    注意: 要在代码中获取^M,请键入ctrl+v 输入

    命名并保存文件ex:script.vim,然后通过命令“:source script.vim”调用它

    示例如下:(30 个字符 - 限制 -):

    【讨论】:

      【解决方案2】:

      您的设置通过添加回车来换行,这会导致编译时出现问题。

      相反,您可以使用wrap 选项,它是一个虚拟换行,不会影响行:

      :set wrap
      :set textwidth=0
      :set wrapmargin=0
      

      【讨论】:

      • 我想使用真正的包装来影响线条!但不要在引号内包裹太长的字符串。
      • 你的意思是换行会在另一行吗?
      • 阅读:globalsubstitution和事件autocmd,一旦你完全理解它们,你就可以解决这个问题。
      • 告诉我你的进展。 :)
      • 是的!当一行超过 80 个字符时,我上面的设置会自动换行!我只想在自动换行之前不将字符串包含在引号内或添加“\”。
      猜你喜欢
      • 2010-10-06
      • 1970-01-01
      • 2020-06-27
      • 2010-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多