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?


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


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.


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

  • 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. 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)') Aug 13 '17 at 3:47

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.