Skip to content

Commit

Permalink
Fix error when unbinding keybindings with escaped characters
Browse files Browse the repository at this point in the history
Some characters are escaped when listing with `tmux list-keys`, e.g.
`M-"`, `#` and `$` are outputted as `"M-\""`, `\#` and `\$`,
respectively. Backslashes needs to be removed before running `tmux
unbind-key`, otherwise the following error may appear

```
unknown key: M-\\
```

Semicolons are escaped and they need to be that for `tmux unbind-key`.
Therefore we treat them specially (and keep the backslash). This fixes
GitHub issue #1.
  • Loading branch information
whame committed Feb 16, 2022
1 parent 9fd7acf commit ef3c67b
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions tmux-modal.tmux
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,30 @@ unbind() {
local kt=${lineArr[2]}
local kbd=${lineArr[3]}

# Check if keybinding contains any quote characters ('"').
# Check if keybinding contains any "special" characters.
case "$kbd" in
*'"'*)
'"'*'"')
# Keybindings that contain characters that need to be escaped,
# are quoted in the output of `tmux list-keys`. For example,
# `M-#` and `M-$` are listed as `"M-#"` and `"M-$"`. Moreover,
# quotes in the keybindings are escaped, e.g. `M-"` and `"` are
# listed as `"M-\""` and `\"`. Before giving it to `tmux
# unbind-key`, we therefore need to strip potential quotes...
# `M-#` and `M-$` are listed as `"M-#"` and `"M-$"`. Before
# giving it to `tmux unbind-key`, we therefore need to strip the
# surrounding quotes.
kbd=$(sed -e 's/^"\(.\+\)"$/\1/' <<< "$kbd")
# ... and backslashes.
kbd=$(sed -e 's/\\"/"/' <<< "$kbd")
;;&
*"\\"*)
# Some characters are escaped as well, e.g. `M-"`, `#` and `$`
# are listed as `"M-\""`, `\#` and `\$`, respectively.
# Backslashes needs to be removed before running `tmux
# unbind-key`.
kbd=$(sed -e "s/\\\\\(.\)/\1/g" <<< "$kbd")
;;&
*";"*)
# Semicolons are escaped and they need to be that for `tmux
# unbind-key`. Above we removed all backslashes, therefore we
# add it here for this special character.
# TODO: Are there any other special characters that need to be
# escaped?
kbd=$(sed -e "s/;/\\\\;/g" <<< "$kbd")
;;
esac

Expand Down

0 comments on commit ef3c67b

Please sign in to comment.