5

Given this file:

printf 'hello\x19world\n' > hello.txt

I can replace a hex character:

$ ex -sc '%s/\%x19/ /|x' hello.txt
$ od -c hello.txt
0000000   h   e   l   l   o       w   o   r   l   d  \n

However I cannot replace with a hex character:

$ ex -sc '%s/ /\%x19/|x' hello.txt
$ od -c hello.txt
0000000   h   e   l   l   o   %   x   1   9   w   o   r   l   d  \n
7

From :help todo:

Substitute with hex/unicode number "\%xff" and "\%uabcd". Just like "\%uabcd" in search pattern.

In other words, this isn't implemented yet.

You can do it interactively, in Vim. See :help 24.8.

Type :%s/ /, then type Ctrl-V, then x19, then press Enter.

1

Yes you can do this easily. You just have to use an expression in the replacement part (see the help at :h sub-replace-expression link).

So you would do it like this:

ex -sc '%s/ /\="\x19"/g|x' hello.txt
 od -c hello.txt
 0000000   h   e   l   l   o 031   w   o   r   l   d  \n

See the help at :h expr-quote link

0

You can pass it from the shell:

ex -sc $'%s/\031/\027/|x' hello.txt

This will pass the literal ^Y and ^[ characters to ex, which seems to work fine. You can also enter a literal character as ^V^Y (control+v control+y) in most shells.

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.