Emacs Function Key Binding in GNU Screen with input-decode-map
Posted by hide1713 on July 4, 2011
Problem:
When you run Emacs in text mode or in a GNU screen, some of the function key bindings are not working. For example. <S-f4> will just print “;2S” on the screen instead of running command that binds to Shift f4. Some of the key binding such as Ctrl-arrow/Meta-arrow keys can be mapped correctly by setting the term info to xterm256, etc. However, a general solution is available to map any key code to where it should be.
Solution:
According to the manual, you can define keys in input-decode-map
http://www.gnu.org/software/emacs/manual/html_node/viper/Key-Bindings.html
Don’t copy the code. You need to type it in your .emacs file.
(define-key input-decode-map "^[O1;2S" (kbd "<S-f4>")) ; bind shift-f4 (define-key input-decode-map "^[O1;5S" (kbd "<C-f4>")) ; bind ctrl-f4 (global-set-key (kbd "<C-f4>") 'previous-line) (global-set-key (kbd "<S-f4>") 'next-line)
To get the key code for S-f4, press C-q and S-f4. You will see something like ^[O1;2S pops out in your emacs buffer. Bind the key code to the key name and you can use it anywhere now.