Unwrapping rlwrap

作者

Michaela Woods

Developer Advocate

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.

Unwrapping rlwrap

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…

Unwrapping rlwrap

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(kdb+データベース)
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:

Q(kdb+データベース)
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

AIによるイノベーションを加速する、KXのデモをお客様に合わせてご提供します。

当社のチームが以下の実現をサポートします:

  • ストリーミング、リアルタイム、および過去データに最適化された設計
  • エンタープライズ向けのスケーラビリティ、耐障害性、統合性、そして高度な分析機能
  • 幅広い開発言語との統合に対応する充実したツール群

専門担当者によるデモをリクエスト

*」は必須フィールドを示します

本フォームを送信いただくと、KXの製品・サービス、お知らせ、イベントに関する営業・マーケティング情報をお受け取りいただけます。プライバシーポリシーからお手続きいただくことで購読解除も可能です。当社の個人情報の収集・使用に関する詳しい情報については、プライバシーポリシーをご覧ください。

このフィールドは入力チェック用です。変更しないでください。