1

Create a sample text for testing.

cat > sample.txt << EOF
i and 你
EOF

Open sample.txt in vim,the cjk character in unicode is 4f60,

%s/\%u4f60/you/g   
%s/你/you/g   # you can use it also

It can replace with you,it turn out to be i and you,i change i and 你 into i and you,now i want to change reversely,change i and you into i and 你,it can be done with %s/you/你/g,try another way:

%s/you/\%u4f60/g  
%s/you/你/g   # it can work

It change it into i and %u4f60, instead of i and 你,why?

  • "If everything else fails, you can type any character as four hex bytes: CTRL-V u 4f60" – Alex Kroll Aug 5 at 13:45
  • 4
    That is because \%u4f60 is a regex atom, that is only valid in the search part of the :s. – Christian Brabandt Aug 5 at 14:21
0

The \%u expression is only a valid syntax for the pattern of a search or of a :s, and not for the replacement part. See :help /\%u.

Similarly to other search pattern syntax such as \s to match whitespace, * to match repeats of the previous item, etc.

In order to produce this character on the replacement side, you have a few options. One of them is to just type the character, assuming you have a keyboard driver that is able to produce it.

From Vim, you can produce that character from its Unicode by using the Ctrl+V combination followed by a sequence of digits, which you can precede with u followed by four hexadecimal characters to enter an Unicode code point.

In your case, Ctrl+V, u, 4, f, 6, 0 will produce the 你 character in either insert or command mode (you need it in command mode to enter it as part of a replacement in a :s command.)

Note that you would type:

:%s/you/Ctrl+Vu4f60/g

But what you would see is:

:%s/you/你/g

The Ctrl+V sequence is simply being used to enter a Unicode character literally.

The other option you have is to have your replacement start with \= in order to introduce an expression and then use a \u escape sequence inside a double quoted string. See :help sub-replace-expression and :help expr-quote. Putting that together:

:%s/you/\="\u4f60"/g

The downside of this approach is that you need to turn your whole replacement into an expression, so if you have more characters as part of that replacement, you need to incorporate them into the string. Also, using special items in the replacement, such as & to repeat the matched pattern, or \1 etc. to insert the contents of matched groups, now requires you to explicitly use the submatch() function in the expression.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.