【发布时间】:2016-12-20 13:50:56
【问题描述】:
我倾向于启用自动换行:
:set textwidth=80
:set formatoptions+=wt
但是当我用 C 或 javascript 编码时,当我在引号中输入一个长字符串时,我不想换行,因为它会出错; 我可以配置我的 vim 自动换行排除引号吗? 还是在换行之前自动输入“\”?
【问题讨论】:
我倾向于启用自动换行:
:set textwidth=80
:set formatoptions+=wt
但是当我用 C 或 javascript 编码时,当我在引号中输入一个长字符串时,我不想换行,因为它会出错; 我可以配置我的 vim 自动换行排除引号吗? 还是在换行之前自动输入“\”?
【问题讨论】:
您可以从我编写的这个小脚本开始,并添加一些改进以满足您的需求:
"""""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 个字符 - 限制 -):
【讨论】:
您的设置通过添加回车来换行,这会导致编译时出现问题。
相反,您可以使用wrap 选项,它是一个虚拟换行,不会影响行:
:set wrap
:set textwidth=0
:set wrapmargin=0
【讨论】:
:global和substitution和事件autocmd,一旦你完全理解它们,你就可以解决这个问题。