Harry Seldon's blog

Fractals, Chaos, and Control Systems on Rails

Read

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

Bonus
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="%~>"
endi

“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[A":history-search-backward
"\e[B":history-search-forward

\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 | 5 comments | Tags , , , , , , , , , , | atom

Delicious

Trackbacks

Use the following link to trackback from your own site:
http://harryseldon.thinkosphere.com/trackbacks?article_id=74

Comments

Leave a response

  1. Misha Koshelev on August 09, 2009 at 10:09 pm

    Thanks. Any way to have this work on gnome-terminal btw? It doesn’t seem to use same translations and a Google search did not show up anything…

    Thanks Misha

  2. Harry Seldon on August 09, 2009 at 10:15 pm

    Well, I am using most Unix/CDE and Linux/KDE. So unfortunately I do not know much about Gnome.

    As a try, let me know your shell (echo $SHELL) and terminal (echo$TERM). I’ll see if I can figure out something.

    Also use and abuse of what I say in the “Test your configuration section”. That is verify your xrdb -query, and verify which codes your keys send and post this here.

  3. Misha Koshelev on August 09, 2009 at 10:24 pm

    Thank you. Here is my relevant output:
    misha@misha-d630:~/Montague Lab/code$ echo $SHELL
    /bin/bash
    misha@misha-d630:~/Montague Lab/code$ echo $TERM
    xterm
    misha@misha-d630:~/Montague Lab/code$ echo $COLORTERM
    gnome-terminal

    It seems clearly a gnome-terminal problem though, as I try in xterm using the Ctrl-V Home and Ctrl-V end, and I get:
    misha@misha-d630:~$ ^A^E

    If I try in gnome-terminal, I get:
    misha@misha-d630:~/Montague Lab/code$ ^[OH^[OF

    I did a little googling on this question, and the only reply I saw that seemed relevant was people’s advice to look at gnome-terminal source…

    Thank you Misha

  4. Harry Seldon on August 09, 2009 at 10:42 pm

    First, you need to put 2 spaces at the end of your lines to go to the next lines (I know it is not obvious ;-) ). It is Markdown syntax. I have done it for clarity.

    Then, what about using xterm? If it works in xterm, use xterm. Nope ?

    To have it working in gnome-terminal, you need to find where the code for your home key is set up. If it cannot be changed then I do not have any other solution than using xterm.

  5. Ljubomir Josifovski on October 22, 2009 at 09:56 pm

    Thanks for that, worked for me too. Only had to say

    XTerm*VT100.translations:

    instead of

    *VT100*Translations:

    in .Xdefaults? No idea what the difference really is.

Leave a comment

Markdown enabled




Search

Navigate


Recent Comments

Recent Posts

Tags

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

Categories

Archives

Syndicate


Sponsor