${(m)…}: Apply MULTIBYTE support

If the MULTIBYTE option is enabled, (m) activates multibyte character width calculation.

Without option MULTIBYTE, each byte is counted as a single character.

Examples

Display width of UNICODE characters
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#!/usr/bin/env zsh
[[ ! -o MULTIBYTE ]] && { echo "Missing MULTIBYTE support"; exit 1; }
  
# 10140 GREEK ACROPHONIC ATTIC ONE QUARTER
# don't use a double-quoted string, it does not apply the \U escape
unicode=$'\U10140'
  
print "Unicode character: $unicode"
print "Multibyte character width: ${(m)#unicode}"
  
# turn of option MULTIBYTE to get width in bytes
set +o MULTIBYTE
print "Character byte count: ${(m)#unicode}"      
Output:
1
2
3
Unicode character: 𐅀
Multibyte character width: 1
Character byte count: 4
BashSupport Pro ist eine Zsh IDE mit Unterstützung für ${(m)…} - probieren Sie es jetzt aus!
© 2020–2024 Joachim Ansorg
Impressum
Datenschutz
Rechtliches