Search code examples
tcltclsh

How to use the left and right arrow keys in the tclsh interactive shell (Ubuntu 14.04)?


Why can't I use the left and right arrow keys (actually, the same goes for the up and down keys as well) to move about the line I'm currently on in the tclsh interactive shell? If I try to press either one, I get a bunch of abracadabra instead of moving back and forth. This is not all that convenient when, for example, you make a typo, but you can't move the cursor back to change it. You have to use the backspace key to erase all the stuff that you've typed after the place where the typo is located thereby destroying all your work. Is it possible to fix this, quite frankly, buggy behaviour?

enter image description here


Solution

  • The behaviour isn't buggy. It is inconvenient yes.

    To get editing in a shell, usually the GNU readline library is used. If a program doesn't use that library, you don't have that feature.

    For tclsh there are licensing reasons (GPL vs. BSD style Tcl license), which make it inconvenient to add readline support directly to tclsh for all those platforms where readline is not part of the operating system (nearly everything but Linux).

    You can use the Ubuntu rlwrap package to still get the editing you want. Install rlwrap:

    sudo apt-get install rlwrap
    

    And use it to run tclsh with command line editing:

    rlwrap -c tclsh
    

    Another option would be to use the Tk based shell tkcon, which provides a bit more options as a Tcl shell, its available as a ubuntu package too.

    Expanding my own answer: You can also build and use tclreadline, as a package in your .tclshrc.