I am running a session of emacs in an xterm+256color terminal, i.e., with the command emacs -nw. At the start of my session, the Home and End keys work just fine. However, after a while, the Home key eventually starts producing the key code ^[[H instead of ^[OF. Why does this happen? What can I do to fix this problem?
xterm has multiple keyboard modes (e.g. application mmode), and accepts escape sequences to switch between them. For example you can make the keypad send "1", "2" etc or you can get it to send sequences.
The simplest way to get it back to normal is to run 'reset' or 'tput reset', which will consult the termcap or terminfo terminal database and should output the correct escape sequences to put all the keys into a standard state.
To avoid the problem in the first place avoid using 'cat' on binary files and other sources of random escape sequences being output to your terminal which can change the modes.