Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: swapping keys



Francisco Borges wrote:
> One possible solution is to switch keys like "`~" and "1!" (I have to
> confess I have only swapped "`~").
> 
> Since I am at it... Is it possible to swap keys inside zsh?

Yes, it's fairly straightforward.  As written it doesn't work for
multibyte characters (e.g. you can't swap with a Euro or Yen or Pound
Sterling in multibyte mode).  That wouldn't be too hard to fix (but it's
not as simple as just removing the test: you need a loop over bytes in
the argument).


# start
# swapkeys X Y
#   swap keys X and Y so typing X gives Y and vice versa
# swapkeys -d X Y
#   completely remove a previous swapping

emulate -L zsh
setopt cbases

integer delete i
local key hexkey

if [[ $1 = -d ]]; then
  delete=1
  shift
fi

if [[ $# -ne 2 || ${#1} -ne 1 || ${#2} -ne 1 ]]; then
  print "Usage: $0 [-d] key1 key2" >&2
  return 1
fi

for (( i = 1; i <= 2; i++ )); do
  key=$argv[i]
  hexkey=$(( [#16] #key ))
  if (( delete )); then
    zle -D insert-key-$hexkey
    bindkey $key self-insert
  else
    eval "insert-key-$hexkey () { LBUFFER+=\$'\\x${hexkey##0x}'; }"
    zle -N insert-key-$hexkey
    bindkey ${argv[3-i]} insert-key-$hexkey
  fi
done
# end

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php



Messages sorted by: Reverse Date, Date, Thread, Author