0

I want to use a hexadecimal Unicode escape in the replacement string of a substitution command, i.e., with the example character U+20AC:

:s#something#\x{20AC}#g

The above example doesn't work, because it uses Perl syntax in the replacement string, but I want this to work in Vim, and I want it to work for many different other possible Unicode characters too, specified by their hexadecimal value (2 or 4 digits or possibly even more). Since this is a series of substitution statements that will be generated by a script, I would like the substitution statements itself to be ASCII-only, as that is easier to work with. I think there must be a proper syntax for this, but I just cannot find it. \%u20AC works in the search pattern, but not in the replacement string. As this is not done interactively, the CTRL-V + 4 hex digits method won't work.

3 Answers 3

2

:s#something#\=nr2char(0x20AC)#g

See :help sub-replace-expression and :help nr2char() in Vim.

Or, if 20AC is really a constant, you can type : s # something # <Ctrl-V> u 20AC # g <Enter>, giving

:s#something#€#g

See :help i_ctrl-v in Vim.

6
  • Okay, this is slightly more readable than what I found myself, although strictly speaking a bit more complicated since it uses a function call. The ctrl-V method was not applicable, I already mentioned it in the question. But it might be useful for other readers.
    – gpvos
    Commented Nov 26, 2018 at 11:10
  • 1
    In a script you can write execute "s#something#\u20AC#g". The \u is interpreted because it appears in a doublequoted string, and the result is then interpreted by execute.
    – AlexP
    Commented Nov 26, 2018 at 11:17
  • That would be in a Vim script. I was thinking of a Perl script generating Vim commands (from some input file specifying mappings) to be executed by @x in Vim. But it's also a good tip, thanks. (And yeah, I could do the whole substitution in Perl, but I don't want to do it on every line, so I do that bit interactively. I just was surprised that there was no easy direct way to do it.)
    – gpvos
    Commented Nov 26, 2018 at 11:30
  • @x executes a Vim script. Have your Perl program generate the execute command.
    – AlexP
    Commented Nov 26, 2018 at 11:32
  • 1
    In normal mode, @ executes the contents of a register as a sequence of keys. In command mode, :@ executes the contents of a register as a script.
    – AlexP
    Commented Nov 26, 2018 at 12:41
1

The other answers are fine; string interpolation via :execute is a method that is particularly suited to scripting, so I'm mentioning that as well.

You can use the :help <Char> notation within double-quoted strings:

let char = "\<Char-0x20AC>"
execute 's#something#' . char . '#g'

Or convert a number via nr2char():

let value = 0x20AC
execute 's#something#' . nr2char(value) . '#g'
1

After some more doc reading, I found at least one, very elaborate way to do this, using a substution expression:

:s#something#\="\<Char-0x20AC>"#g

I prefer it because it is built right into the parser, instead of using a Vimscript function call.

One would hope there is an easier (more direct) way to do this though.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .