GNU Readline

When you have a great and difficult task, something perhaps almost impossible, if you only work a little at a time, every day a little, suddenly the work will finish itself. - Karen Blixen
Photo by Russ Ward on Unsplash

As a stroke survivor I have limited use of my right hand. Since my stroke, I have been on the hunt for tools to make typing as easy as possible. I recently discovered the GNU Readline Library. I stumbled across this somewhat ancient (33 years old is ancient in tech years) library while doing MAVProxy development. Truth be told, I had already been using about 50% of the functionality of the readline library without being aware of it.

According to Wikipedia:

GNU Readline is a software library that provides line-editing and history capabilities for interactive programs with a command-line interface, such as Bash

Despite being based on emacs (I’m a vim fan), readline is very good. If you insist on vim then I suggest looking here, but I am satisfied with the emacs style commands as I don’t use terribly many of them.

I’m doing this on native Ubuntu 20.04. Much of what follows was inspired by these articles:

https://docs.python.org/3/library/readline.html

https://tiswww.cwru.edu/php/chet/readline/rluserman.html

Pro Tip:

I tried to run a sample file named “readline.py” and was hit by this error:

AttributeError: partially initialized module 'readline' has no attribute 'set_completer_delims' (most likely due to a circular import)

The reason is because I can’t import the readline module into a Python file named readline.py – see here: https://stackoverflow.com/questions/59762996/how-to-fix-attributeerror-partially-initialized-module

My solution was to rename readline.py to rline.py

My Readline Cheatsheet

These are sorted by usage frequency.

<TAB> readline will attempt to guess what is wanted
<CTRL> + r begin reverse search through command history

<CTRL> + p move backwards in the command history
<CTRL> + n move forward in the command history
<CTRL> + _ undo one step
<CTRL> + l clear screen
<CTRL> + a move to the start of the line
<CTRL> + e move to the end of the line
<ATL> + f move forward one word
<ATL> + b move backward one word
<CTRL> + k kill (cut) a line from the cursor to tbe end
<CTRL> + y yank (paste) the previously killed line
<CTRL> + g abort the current editing command
<ATL> + u uppercase the current word
<ATL> + l lowercase the current word
<ATL> + c capitalize

Tab Completion

As a disabled typist, this is my Holy Grail. With readline, simply start typing a command and then hit the <TAB> key. Readline will either guess what is wanted (if only one option is possible given the command history), or present a list of possible completions to select from.

<CTRL> + r

It is surprising how often I use <CTRL> + r !

Pro Tip: hit <CTRL> + r again and again if you need to search for a command — you’ll often stumble upon one close to what is needed.

Readline Init File

There is no standard place for this file – look in $HOME/.inputrc first. On my system it’s in the /etc/inputrc file. The documentation says there should be an environment variable called INPUTRC but on my system there is no such variable. The readline init file follows standard bash syntax. I have not had to modfiy this file at all.

Readline Murder

Although it sounds brutal, “killing” text means to save it away for later use. The anolog to killing is yanking. These are essentially just “Cut” and “Paste” but I assume programmers were more violent back in the 80’s. Too much pizza and coffee I guess.

Credits

https://docs.python.org/3/library/readline.html

https://tiswww.cwru.edu/php/chet/readline/rluserman.html

Previous
Previous

Advanced GNU Make

Next
Next

GNU Octave Introduction