When using one of SQLite’s tabular output modes, you can enable the --wrap
option in order to limit the width of each column. This can be handy when the data contains long lines of text.
When we do this, we have the option of specifying “word wrap”, so that words don’t get cut off halfway through.
There are two ways to specify word wrap: use --wordwrap on
or its shortcut -ww
.
Using --wordwrap on
Here’s an example of enabling word wrap with the --wordwrap on
option:
.mode -wrap 20 --wordwrap on
Now let’s run a query:
SELECT * FROM Album LIMIT 1;
Result:
+---------+---------------------+----------+ | AlbumId | Title | ArtistId | +---------+---------------------+----------+ | 1 | For Those About To | 1 | | | Rock We Salute You | | +---------+---------------------+----------+
We can see that the word “Rock” is wrapped to the next line.
If we hadn’t used word wrap, that word would have been cut off. To demonstrate what I mean, here it is again, except this time I’ll disable word wrap:
.mode -wrap 20 --wordwrap off
Now run the query again:
SELECT * FROM Album LIMIT 1;
Result:
+---------+----------------------+----------+ | AlbumId | Title | ArtistId | +---------+----------------------+----------+ | 1 | For Those About To R | 1 | | | ock We Salute You | | +---------+----------------------+----------+
We can see that without word wrap enabled, the word “Rock” is torn apart and uses up two lines.
Using -ww
(Shortcut)
As mentioned, we can use -ww
as a shortcut for --wordwrap on
:
.mode -wrap 10 -ww
Now let’s run a query:
SELECT * FROM Album LIMIT 1;
Result:
+---------+------------+----------+ | AlbumId | Title | ArtistId | +---------+------------+----------+ | 1 | For Those | 1 | | | About To | | | | Rock We | | | | Salute | | | | You | | +---------+------------+----------+
This time I set the column width to 10 instead of 20, and so more words are wrapped. None are interrupted with a line break.
Let’s disable word wrap to see how it affects the output:
.mode -wrap 10 -wordwrap off
Now run the query again:
SELECT * FROM Album LIMIT 1;
Result:
+---------+------------+----------+ | AlbumId | Title | ArtistId | +---------+------------+----------+ | 1 | For Those | 1 | | | About To R | | | | ock We Sal | | | | ute You | | +---------+------------+----------+
Now that the column width is narrower, two words are broken when we disable word wrap.