In VIM, How to wrap all English words with space when save file?

陌路散爱 提交于 2020-04-17 22:06:08

问题


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:

  1. Lines beginning with English words should not have spaces at the beginning

  2. 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
  • \zs marks the beggining of the actual match
  • \ze marks the end of the actual match
  • \w is 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!