SQLite | SQLite Commands | Set Column Widths in Column Mode (.width)
The .width command sets the display width of each column when .mode column is active. This article explains how to use it.
Using the .width Command
The .width command sets the width of each displayed column. It applies when the output mode is set to column with .mode. The syntax is as follows.
.width NUM NUM ...
Each argument specifies a column width in characters. Separate multiple widths with spaces.
A single argument changes only the first column, not every column. SQLite also leaves two spaces between adjacent columns regardless of their widths.
Try an example. Switch to column mode and query the data using the default settings.
.mode column
qlite> .mode column
sqlite>
sqlite> select * from user;
1 devkuma Seoul
2 kimkc Busan
3 araikuma Seoul
4 happykuma Seoul
5 mykuma Daejeon
sqlite>
By default, each column is 10 characters wide, with two additional spaces between columns.
Set only the first column width to 4 characters, then run the SELECT statement.
.width 4
sqlite> select * from user;
1 devkuma Seoul
2 kimkc Busan
3 araikuma Seoul
4 happykuma Seoul
5 mykuma Daejeon
sqlite>
The first column is now 4 characters wide; the other columns retain the default width of 10.
Next, set the first column to 4 characters and the second to 6.
.width 4 6
sqlite> .width 4 6
sqlite>
sqlite> select * from user;
1 devkum Seoul
2 kimkc Busan
3 araiku Seoul
4 happyk Seoul
5 mykuma Daejeon
sqlite>
The first and second columns are 4 and 6 characters wide, respectively. Remaining columns retain the default width of 10.
When Data Exceeds the Specified Width
If a value exceeds its column width, characters beyond that width are truncated in the display.
The following example sets both the first and second columns to 4 characters.
.width 4 4
sqlite> .width 4 4
sqlite>
sqlite> select * from user;
1 devk Seoul
2 kimk Busan
3 arai Seoul
4 happ Seoul
5 myku Daejeon
sqlite>
Although the second column contains longer values, only the first four characters are displayed. This affects display only; stored values remain unchanged.
Right-Aligning Data
Specify a negative width with .width to right-align the displayed data.
Run the following .width command to set the widths and enable right alignment.
.width -4 -10
sqlite> select * from user;
1 devkuma Seoul
2 kimkc Busan
3 araikuma Seoul
4 happykuma Seoul
5 mykuma Daejeon
sqlite>
Each column is displayed right-aligned within its specified width.