History and auto-completion with rlwrap

Fernando
Fernando

If you ever used Oracle's sqlplus you'd agree that it provides an arcane command-line interface.

Being a textmode command-line tool is not what makes it arcane though. It's that it doesn't offer auto-completion or a command history. If you made a small mistake when typing a long statement, you would have to re-enter it all over again.

Most modern command-line tools (including MySQL and PostgreSQL's equivalent to Oracle's sqlplus) provide much more powerful interfaces, just like the bash shell does. They include auto-completion of the text you are typing (by pressing TAB), access to a history of commands (up/down arrows, or C-p/C-n), incremental search on the history (C-r), they remember the history in between invocations and more. Virtually all these tools use the GNU readline library to provide these capabilities.

Unfortunately, not all command-line tools use GNU readline (splplus being one). Fortunately, there's rlwrap. I just came to know this nice little tool.

rlwrap "wraps" any other command-line tool and gives you a readline interface to it. So, you can invoke rlwrap sqlplus and you get sqlplus with the history capabilities of the readline library.

You may also pass to rlwrap a list of potential words to use for completion. For example, I also use rlwrap with groovysh (the Groovy language shell), so I created a file "~/.groovysh_completions" containing the list of commands groovysh accepts. Now, when I launch groovysh I get command history and specialized auto-completion.

Now, rlwrap cannot do magic. Being so generic, it cannot do intelligent context-dependent auto-completion. For instance, PostgreSQL's command-line interface automatically pulls the list of potential table names after doing SELECT * FROM <TAB>. rlwrap cannot give this intelligence to sqlplus, but it's still much better than nothing.