Unwrapping rlwrap

2 Jun 2020 | ,
Share on:

by Eoin Cunning.

Sometimes developers have a choice; spend five minutes completing a menial task or spend a little longer streamlining it for future convenience. To that end, what I outline below will hopefully enable you to get the most out of rlwrap with q.

The out-of-the-box kdb+ console does not support using arrow keys to edit the line of code you are writing, or go back through the history of commands you have entered. Rlwrap is a small utility which enables these features.

If you aren’t already using rlwrap here’s how to install it.

macOS: code.kx.com/q/learn/install/macos/#install-rlwrap
Linux: code.kx.com/q/learn/install/linux/#install-rlwrap

Note rlwrap is available for Linux and macOS only, not for Windows.

Developers usually install rlwrap and update their q alias and are happy that they no longer have to type entire lines of code left-to-right, in a language read right-to-left, without being able to use the arrow keys. But there’s actually some useful features of rlwrap that most developers don’t seem to be aware of, and that’s what this post hopes to share.

CTRL-r


Problem

You have a command that you executed previously that you want to re-execute or edit slightly and execute without having to retype the whole thing. Or endlessly hitting the up arrow until you find it.

Solution

Similarly to how you can search through your history in a bash shell. You can search through all your q history by first hitting CTRL-r then typing what you’re searching for.

The latest match will then appear, You can use the up arrow to look at older lines that also match. Once you’ve found the one you were looking for hit the right or left arrow to edit before executing. Or hit enter to execute the same line again.

CTRL-l


Problem

You’re showing somebody something in q and you have lots of code on your screen, or maybe while sharing your screen you can’t really see the bottom of the screen properly or just want to start from blank to make things clear.

Solution

CTRL-l works as a shortcut for ‘clear’ command. No one may ever need to know about that embarrassing ‘type error…

rlwrap -f


Problem

Maybe you’ve just started learning kdb+ and you can’t remember this big list of all the q functions. You have https://code.kx.com/q/ref/ bookmarked but are just fed up having to keep checking.

… or maybe you’re an experienced developer that’s just started a new role and really can’t be bothered trying to remember all the new function names in the framework you are now going to be using

… or like me, you just really struggle spelling recipricol reciprical … “(%:)”. Seriously, 10 letters for a q function?

Solution

The -f, –file option, from “man rlwrap

This will allow you to start typing a function and then hit ‘tab’ to auto-complete/show you all the options you have. Much like when you press tab in Unix when writing out file paths.

Now you just need a way to populate this file. The tools in order to do this are in this script

Start your q session load in all the functions you want to be included in your auto complete list. Then load in provided script and run the following:

q)\l rlwrapAutoComp.q
q).rac.writeRlwrapFile ` sv (hsym `$getenv `HOME),`qRlwrapAutoComplete.txt

Now start a new q session with rlwrap using the -f option and reference the file you just created:

rlwrap -f ~/qRlwapAutoComplete.txt q

Now when you hit ‘tab’ twice you will see all the options in ~/qRlwapAutoComplete.txt

You’ll never have to spell reciprocal again!

The code for this utility and the others mentioned above are in this script

SUGGESTED ARTICLES