Enable autocomplete in an sqlite3 interactive shell

22

5

I am using sqlite3 on a machine where I can use tab completion (ie .read abc will autocomplete to .read abcdefghij.db. I would like to know how to enable this on my personal machine.

Both machines are ubuntu linux and the shell is bash. I am referring to autocompletion in the sqlite interactive prompt.

Originally posted on dba.

CoatedMoose

Posted 2012-11-04T20:52:39.067

Reputation: 323

Answers

12

Compile the program with readline supoort. Readline is a common library that handles user input in interpreters such as bash and python. Fetch the source, the dependencies and configure with:

user@computer in: ~/src/sqlite-autoconf-3071602
$ ./configure --enable-readline=yes

Consult the INSTALL file for details. Also, it's worth mentioning that there are probably binaries of sqlite3 with readline support already packaged for your distro. Look around.

Ярослав Рахматуллин

Posted 2012-11-04T20:52:39.067

Reputation: 9 076

1you were probably missing the "-dev" package with the necessary headers. – Ярослав Рахматуллин – 2018-08-27T17:11:03.800

15

You can use rlwrap if you don't want to compile sqlite3. Just run sudo apt install rlwrap, and then set up an alias for sqlite3 in your .bashrc:

alias sqlite="rlwrap -a -N -c -i sqlite3"

The -c option gives you filename completions.

And you can create a file ~/.rlwrap/sqlite3_completions to have keyword completions:

ABORT ACTION ADD AFTER ALL ALTER ANALYZE AND AS ASC ATTACH AUTOINCREMENT BEFORE BEGIN BETWEEN BY CASCADE CASE CAST CHECK COLLATE COLUMN COMMIT CONFLICT CONSTRAINT CREATE CROSS CURRENT_DATE CURRENT_TIME CURRENT_TIMESTAMP DATABASE DEFAULT DEFERRABLE DEFERRED DELETE DESC DETACH DISTINCT DROP EACH ELSE END ESCAPE EXCEPT EXCLUSIVE EXISTS EXPLAIN FAIL FOR FOREIGN FROM FULL GLOB GROUP HAVING IF IGNORE IMMEDIATE IN INDEX INDEXED INITIALLY INNER INSERT INSTEAD INTERSECT INTO IS ISNULL JOIN KEY LEFT LIKE LIMIT MATCH NATURAL NO NOT NOTNULL NULL OF OFFSET ON OR ORDER OUTER PLAN PRAGMA PRIMARY QUERY RAISE RECURSIVE REFERENCES REGEXP REINDEX RELEASE RENAME REPLACE RESTRICT RIGHT ROLLBACK ROW SAVEPOINT SELECT SET TABLE TEMP TEMPORARY THEN TO TRANSACTION TRIGGER UNION UNIQUE UPDATE USING VACUUM VALUES VIEW VIRTUAL WHEN WHERE WITH WITHOUT

The -i option makes keyword completion case insensitive.

arekolek

Posted 2012-11-04T20:52:39.067

Reputation: 251

2This answer deserves a lot more up-votes. Also note that I had to remove the -N option for the completion to work with Ubuntu's sqlite3. – xhienne – 2017-01-20T17:36:04.740

There's also /usr/share/rlwrap/completions (as documented in man rlwrap) directory to put system-wide completions files for all users. One thing to note the completion file names in there shouldn't end with _completion suffix as is the case with the per-user ~/.rlwrap directory. – None – 2017-06-18T22:27:58.783

1rlwrap -a -N -c -i -f ~/.rlwrap/sqlite3_completions sqlite3 for Debian users. echo '.help' | sqlite3 | grep -o '^\.[a-z]* ' >> ~/.rlwrap/sqlite3_completions to autocomplete all dot commands. – zhazha – 2017-12-16T02:48:22.693