问题
There are some words in vim like this:
你好hello
你好hello你好
hello你好
你好hi-hello你好
hi_hello你好
I want to automatically separate English words from Chinese words when saving the file with one space. The final format like this:
你好 hello
你好 hello 你好
hello 你好
你好 hi-hello 你好
hi_hello 你好
I tried to use this command without success::%s/\(\w\+[_-]*\w*\)/ \1 /g
With this command, there are two things that can't be done:
Lines beginning with English words should not have spaces at the beginning
Repeat commands should not repeat spaces
Is there any way to do it?
回答1:
this works for me:
:%s/[^\x00-\xff]\zs\ze\w\|\w\zs\ze[^\x00-\xff]/ /g
Explanation
[^\x00-\xff]matches any chinese character\zsmarks the beggining of the actual match\zemarks the end of the actual match\wis any word character
You will note that the \zs and \ze are next to each-other, which effectively means that the actual match is nothing, more precisely, the nothing between a latin character, and a Chinese character.
Then, It's just a matter of replacing this "nothing" by a space
来源:https://stackoverflow.com/questions/61232791/in-vim-how-to-wrap-all-english-words-with-space-when-save-file