I want to replace a character with its hex literal. For example before regex:


After regex:


I'm trying this:

s/\([a-z]\)/\=printf("%#02x", submatch(0))/

But this replaces it with


What am I doing wrong? Is this even possible?

1 Answer 1


In that way, printf expects a decimal number to be converted to a hexadecimal one. All you need to do is convert the character to a number as follows:

s/\([a-z]\)/\=printf("%#02x", char2nr(submatch(0)))/

See :help char2nr() for details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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