0

I know how to output bindkeys using bindkey.

I can grep a word.

$ bindkey | grep line
"^A" beginning-of-line
"^E" end-of-line
"^J" accept-line
"^K" kill-line
...

But I can't grep ^[G or ^[[A, etc.

$ bindkey | grep ^[G
zsh: bad pattern: ^[G

How can I grep by keywords like ^[G or ^[[A, etc?

2

Two problems: characters that are special to the shell, and characters that are special in regexes.

The open bracket [ has a special meaning in the shell's filename expansion patterns, which is what zsh refers to with that error. You can tell it to take the characters literally with quotes. Either single or double quotes should do, but single quotes are better, they have less exceptions.

In regexes, an initial ^ tells to anchor the pattern to start of line, and [ has a special meaning similar to the one it has in the shell. So you need to tell grep to treat both of them as literals, this is done by escaping them with backslashes.

So:

bindkey | grep '\^\[G'

Note that the ^A in the output of bindkey is the two characters ^ (caret) and A (uppercase letter A), while what that combination stands for in the key bindings is Ctrl-A, the control character with numerical value 1. Grepping for that character would be different.

1
  • ^ is also an extendedglob operator in zsh. See also bindkey '^[F' or Alt+X, then descr<Tab> which expands to describe-key-briefly, and then type the key or key sequence you want described. – Stéphane Chazelas Feb 17 at 14:48
2

Using -F option and using quotes worked.

~ % bindkey | grep -F '^[G'
"^[G" get-line

-F, --fixed-strings Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is specified by POSIX .)

0

If you really want to get a control character, use hexcodes. E.g. a TAB:

grep -P '\x09'

Alternatively

grep -P '\t'

Find a list here.

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.