在vim中启用自动换行时如何防止将字符串包装在引号中? [英] How to prevent wrapping string in quotes while enable auto-wrap in vim?

查看:19
本文介绍了在vim中启用自动换行时如何防止将字符串包装在引号中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我倾向于启用自动换行:

I tend to enable auto-wrap :

:set textwidth=80
:set formatoptions+=wt

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

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?

推荐答案

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

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

注意: 要获得代码中的^M,请输入ctrl+v 回车

命名并保存文件ex:script.vim,然后通过命令:source script.vim"

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

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

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

这篇关于在vim中启用自动换行时如何防止将字符串包装在引号中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆