How to prevent the string wrapping in quotation marks when turning on automatic wrapping in vim?

I want to enable automatic wrapping:

:set textwidth=80 :set formatoptions+=wt 

But I do not want to wrap when entering a long string in quotation marks, when I encode with C or javascript, because it will be an error; Can I set quotes for vim auto-traversal? or automatic input "\" before wrapping this line?

0
source share
2 answers

You can start with this little script that I encoded and add some improvements to suit 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 ^M in code, type ctrl + v Enter

Name and save the ex : script.vim and call it after that with the command :source script.vim

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

enter image description here

0
source

Your settings wrap strings, adding carriage returns and which cause compilation problems.

Instead, you can use the wrap option, which is a virtual shell and does not affect lines:

 :set wrap :set textwidth=0 :set wrapmargin=0 
-1
source

Source: https://habr.com/ru/post/1263556/


All Articles