How to prevent wrapping string in quotes while ena

2019-06-11 22:33发布

问题:

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


标签: vim vi