1

If I have non-printable characters assigned to a variable, how can I echo them?

For example, if I have a variable xyz set to be two spaces and a tab, then if I do this:

:echo xyz

it just shows a blank line. I can't actually see the characters to which the variable has been set. Is there any way to get echo to visualize non-printable characters?

Tyler Durden
  • 2,091
  • 2
  • 23
  • 42

2 Answers2

4

you can use the strtrans() function, which translates unprintable characters.

strtrans()

The result is a String, which is {expr} with all unprintable characters translated into printable characters 'isprint'. Like they are shown in a window. Example: echo strtrans(@a) This displays a newline in register a as "^@" instead of starting a new line.

Christian Brabandt
  • 25,820
  • 1
  • 52
  • 77
3

I often add characters around like in :echo '###'.xyz.'###'. It's simple enough, and more readable, IMO, than :echo split(xyz, '\zs')

Luc Hermitte
  • 17,351
  • 1
  • 33
  • 49
  • The problem is that the variable can have other things in it besides spaces. There are a lot of different unprintable characters. Also, if there are a lot of spaces, like say 15 of them, it is not easy to count them when you just have a swath of whitespace in the printout. – Tyler Durden Aug 12 '17 at 16:34
  • You have strtrans() for unprintable characters, or your can map the split list of characters to char2nr() to know their code => :echo map(split(xyz,\zs), 'char2nr(v:val)') – Luc Hermitte Aug 13 '17 at 03:47