Harry Seldon's blog

Fractals, Chaos, and Control Systems on Rails


How to have home and end keys working in xterm and in the Matlab command line.

Posted by Harry Seldon on August 08, 2009

My home, end and delete keys were not working in my unix terminal (xterm) or in my Matlab console. For instance in the Matlab console (Matlab run in –nodesktop mode), all I was getting after pressing the home or end key was 4~ or 5~, same was going on for the terminal (xterm and csh). As most shells recognize the ctrl+a, ctrl+e, ctr+d shortcuts for home (beginning of line), end (end of line) and delete and as Matlab too recognizes these shortcuts, the simplest solution is to have your keys sending these shortcuts when in a terminal. You probably do not want to completely reconfigure your keys as they might be working fine in text editors.

To configure your keys simply add this to your /home/user/.Xdefaults file:

*VT100*Translations:       #override  \
  <Key>Home: string(0x01)\n\
  <Key>End:  string(0x05)\n\
  <Key>BackSpace: string(0x08)\n\
  <Key>Delete:   string(0x04)\n\
  ~@Num_Lock<Key>KP_Home:      string(0x01)\n\
  ~@Num_Lock<Key>KP_End:       string(0x05)

“string(0x01)” means to send the code 0x01 which is the ASCII code for ctrl+a. “string(0x02)” would mean ctrl+b, “string(0x05)” indeed means ctrl+e etc.
A list of ASCII control code can be found here : http://www.phanderson.com/C/ascii.html
(Btw, If someone can explain me why the fist ASCII code is ctrl+a and not simply a I would be impressed).

How to test the configuration
To load the .Xdefaults file enter:

xrdb -merge ~/.Xdefaults

To check the state of your X configuration:

xrdb -query

To test your configuration, it is also often useful to check which code a key sends. To do this, in a terminal, just press ctrl+v then press the key. The code sent by the key should appear, for instance ^a (=ctrl+a) if you pressed the home key or directly ctrl+a.
[Edit] Also it is important that you know which terminal and shell you are using. To know this:

echo $SHELL
echo $TERM

As a bonus here are a few more configurations:
To be able to use page up, page down to scroll in xterm:

*VT100*Translations:       #override  \
  <Key>Prior: scroll-back(1,halfpage) \n\
  <Key>Next: scroll-forw(1,halfpage)

To be able to use the wheel mouse to scroll in xterm:

*VT100*Translations:       #override  \
  <Btn4Down>,<Btn4Up>: scroll-back(1,halfpage) \n\
  <Btn5Down>,<Btn5Up>: scroll-forw(1,halfpage)

To configure your function keys for often used commands:

*VT100*Translations:       #override  \
  <Key>F1: string("ls") string(0x0d) \n\
  <Key>F2: string("ll") string(0x0d) \n\
  <Key>F3: string("ps") string(0x0d) \n\
  Shift<Key>F1: string("ls -lg") string(0x0d) \n\
  Shift<Key>F2: string("pwd") string(0x0d) \n\
  Shift<Key>F3: string("ps -ax") string(0x0d) 

Notice that 0x0d is nothing else than the ASCII code of carriage return.

To get autocompletion thanks to your history in the command line. That is you press a few characters then you press up and it will complete the line with the last command with the same beginning in your history. With this, it is useful to have a long history and the same history file for all your opened terminals.
If you use tcsh, put this in your ~/.cshrc file:

# 5000 last commands are saved
set   history = 5000
# All terminals merge their history in one file 
set   savehist=(5000 merge)
if ( $?tcsh ) then
       bindkey -k up history-search-backward
       bindkey -k down history-search-forward
       # inser to backward-delete-word       
       bindkey ^[[2~ backward-delete-word 
       # Prompt with the current path
       set prompt="%~>"

“if ( $?tcsh )” checks that the current shell is tcsh.

If you use bash add this in ~/.bashrc (or /etc/inputrc):

# up & down arrow keys

\e means escape key. Escape key also sometimes appear as ^[. So if you press ctrl+v then up arrow you will get ^[[A.

Notice that once you have correctly set up the home, end keys and the up, down keys for history you will have the same configuration between xterm and Matlab.

All this should pretty much work under UNIX or LINUX. However, with all the configurations around (Linux,Unix,HP-UX,Unix through exceed/csh tcsh/gnome,KDE,CDE,etc.) a few things might not work in your configuration. So good luck!

Posted in | 6 comments | Tags , , , , , , , , , , | atom



Recent Comments

Recent Posts


actuators aircraft atc blog chaos chaos_theory charts control controllers controls crisis economy finance flight fractals git gnc gs guidance linux mandelbrot marketing navigation ns ofc on pilot rails ruby sas scs sensors statistics systems techcrunch thinkosphere tutorial typo ubuntu wifi