How to prevent wrapping string in quotes while enable auto-wrap in vim?

馋奶兔 提交于 2019-12-10 11:57:51

问题


I tend to enable auto-wrap :

:set textwidth=80
:set formatoptions+=wt

But I don't want to wrap when I input a long string in quotes when I coding with C or javascript, because it will be error; Can I configure my vim auto-wrap exclude quotes? or auto typing '\' before wrapping this line?


回答1:


You can start from this little script which I coded and add some improvements in order to fit your needs:

"""""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

Note: to get the ^M in the code please type ctrl+v Enter

Name and save the file ex:script.vim and call it after that by the command ":source script.vim"

Here is the example: ( 30 characters - Limit -):




回答2:


Your settings are wrapping the lines by adding carriage return and which cause problem while compiling.

Instead you can use the wrap option which is a virtual wrap and doesn't affect the lines:

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


来源:https://stackoverflow.com/questions/41211133/how-to-prevent-wrapping-string-in-quotes-while-enable-auto-wrap-in-vim

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!