Search code examples
regexvimunicode

how to search for Unicode character using vim character class and reoder their position


Given

数据     Data
子树      subtree

Desired output

Data  数据
subtree 子树

For non unicode, this can be achieved using command s/\(\w\+\) \+\(\w\+\)/\2 \1/g


Solution

  • The following collection includes all "word characters" as matched by \w as well as all characters in the CJK Unified Ideographs Unicode block:

    [0-9A-Za-z\u4E00-\u9FFF]
    

    search

    See :help collection.

    It can be used as-is in place of \w in your pattern:

    :%s/\([0-9A-Za-z\u4E00-\u9FFF]\+\) \+\([0-9A-Za-z\u4E00-\u9FFF]\+\)/\2 \1
    

    substitute

    That specific range (largely) covers the characters you provided in your sample. You may have to adjust it if your real world requirements are different.