Search code examples
vim

In vim, how do I replace a hex character with another hex character


how can I replace a hex with another hex value? for example, I want to replace \x09 with \x10, I tried

:s/\\%x09/\x10/g
:s/\\%x09/\\%x10/g

but neither worked.

The first parameter \\%x09 is correct, and the issue is in the second parameter.


Solution

  • The first parameter \\%x09 is correct, and the issue is in the second parameter.

    No, \\%x09 is not correct, it should be \%x09.

    If you take a look at :help :s you will notice that the first part ("search") of a substitution is a pattern, but the second part ("replace") is a string, which means that you can't use a regular expression, there, and thus that something like:

    :s/\%x09/\x10/g
    

    can only be expected to replace the matching character with three: a x, a 1, and a 0, which is not what you want.

    One possible solution could be to use :help printf() in a :help sub-replace-expression. Something like this:

    :s/\%x09/\=printf("\x10")/g