Commit abe228b14d97d8d47e8ff4406de387fac45cfe68

Parents: 30851656591293ed2e19340ab78c937855a11143

From: Robin Jarry <robin@jarry.cc>
Date: Sun Oct 22 23:23:18 2023 +0700

commands: use completion from go-opt
Implement command completion with complete struct field tags from the
get-opt library introduced earlier.

Changelog-changed: Improved command completion.
Signed-off-by: Robin Jarry <robin@jarry.cc>
Reviewed-by: Koni Marti <koni.marti@gmail.com>
Tested-by: Moritz Poldrack <moritz@poldrack.dev>
Tested-by: Inwit <inwit@sindominio.net>

Stats

commands/account/cf.go +3/-3
commands/account/check-mail.go +0/-4
commands/account/clear.go +0/-4
commands/account/compose.go +6/-5
commands/account/connection.go +0/-4
commands/account/expand-folder.go +0/-4
commands/account/export-mbox.go +3/-3
commands/account/import-mbox.go +3/-4
commands/account/mkdir.go +9/-20
commands/account/next-folder.go +0/-4
commands/account/next-result.go +0/-4
commands/account/next.go +0/-4
commands/account/recover.go +3/-4
commands/account/rmdir.go +0/-4
commands/account/search.go +14/-27
commands/account/select.go +0/-4
commands/account/sort.go +16/-40
commands/account/split.go +0/-4
commands/account/view.go +0/-4
commands/cd.go +3/-4
commands/choose.go +0/-4
commands/commands.go +42/-122
commands/compose/abort.go +0/-4
commands/compose/attach-key.go +0/-4
commands/compose/attach.go +8/-5
commands/compose/cc-bcc.go +0/-4
commands/compose/detach.go +4/-3
commands/compose/edit.go +0/-4
commands/compose/encrypt.go +0/-4
commands/compose/header.go +3/-3
commands/compose/multipart.go +3/-4
commands/compose/next-field.go +0/-4
commands/compose/postpone.go +3/-15
commands/compose/send.go +6/-13
commands/compose/sign.go +0/-4
commands/compose/switch.go +4/-3
commands/ct.go +3/-7
commands/eml.go +3/-4
commands/exec.go +0/-4
commands/help.go +3/-3
commands/move-tab.go +0/-4
commands/msg/archive.go +3/-4
commands/msg/copy.go +3/-3
commands/msg/delete.go +0/-4
commands/msg/envelope.go +0/-4
commands/msg/fold.go +0/-4
commands/msg/forward.go +4/-3
commands/msg/invite.go +0/-4
commands/msg/mark.go +0/-4
commands/msg/modify-labels.go +3/-3
commands/msg/move.go +3/-3
commands/msg/pipe.go +0/-4
commands/msg/read.go +8/-5
commands/msg/recall.go +0/-4
commands/msg/reply.go +4/-3
commands/msg/toggle-thread-context.go +0/-4
commands/msg/toggle-threads.go +0/-4
commands/msg/unsubscribe.go +0/-5
commands/msgview/close.go +0/-4
commands/msgview/next-part.go +0/-4
commands/msgview/next.go +0/-4
commands/msgview/open-link.go +3/-3
commands/msgview/open.go +0/-4
commands/msgview/save.go +5/-7
commands/msgview/toggle-headers.go +0/-4
commands/msgview/toggle-key-passthrough.go +0/-4
commands/new-account.go +0/-4
commands/next-tab.go +0/-4
commands/parser.go +0/-134
commands/parser_test.go +0/-258
commands/pin-tab.go +0/-4
commands/prompt.go +3/-53
commands/pwd.go +0/-4
commands/quit.go +0/-4
commands/send-keys.go +0/-4
commands/suspend.go +0/-4
commands/term.go +0/-4
commands/terminal/close.go +0/-4
commands/util.go +11/-8
commands/z.go +4/-3
main.go +1/-1

Changeset

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
diff --git a/commands/account/cf.go b/commands/account/cf.go
index 59203a89fc33db86640a4adba99ae980b289cf50..d73d49780d9fd54b5ad562fa3ab4b650aeefd5d0 100644
--- a/commands/account/cf.go
+++ b/commands/account/cf.go
@@ -11,7 +11,7 @@
 var history map[string]string
 
 type ChangeFolder struct {
-	Folder string `opt:"..." metavar:"<folder>"`
+	Folder string `opt:"folder" complete:"CompleteFolder"`
 }
 
 func init() {
@@ -23,8 +23,8 @@ func (ChangeFolder) Aliases() []string {
 	return []string{"cf"}
 }
 
-func (ChangeFolder) Complete(args []string) []string {
-	return commands.GetFolders(args)
+func (*ChangeFolder) CompleteFolder(arg string) []string {
+	return commands.GetFolders(arg)
 }
 
 func (c ChangeFolder) Execute(args []string) error {
diff --git a/commands/account/check-mail.go b/commands/account/check-mail.go
index a8d80a66f7c5bcbb2215d95a6a126f84fb1a16bf..d31b2648e8283ab116fd9aa5068d3833bd2cee10 100644
--- a/commands/account/check-mail.go
+++ b/commands/account/check-mail.go
@@ -16,10 +16,6 @@ func (CheckMail) Aliases() []string {
 	return []string{"check-mail"}
 }
 
-func (CheckMail) Complete(args []string) []string {
-	return nil
-}
-
 func (CheckMail) Execute(args []string) error {
 	acct := app.SelectedAccount()
 	if acct == nil {
diff --git a/commands/account/clear.go b/commands/account/clear.go
index fd209d074a85afb290e1954685f2608eddd967a2..dec6bcd2c40db1da31ca3fe0c5878d3104e4fe65 100644
--- a/commands/account/clear.go
+++ b/commands/account/clear.go
@@ -19,10 +19,6 @@ func (Clear) Aliases() []string {
 	return []string{"clear"}
 }
 
-func (Clear) Complete(args []string) []string {
-	return nil
-}
-
 func (c Clear) Execute(args []string) error {
 	acct := app.SelectedAccount()
 	if acct == nil {
diff --git a/commands/account/compose.go b/commands/account/compose.go
index 81eb3de0deb684744d047e093fde66abeabda4df..fe86a1794557984f27721b69dd1ad321b6d90d3a 100644
--- a/commands/account/compose.go
+++ b/commands/account/compose.go
@@ -11,12 +11,13 @@
 	"github.com/emersion/go-message/mail"
 
 	"git.sr.ht/~rjarry/aerc/app"
+	"git.sr.ht/~rjarry/aerc/commands"
 	"git.sr.ht/~rjarry/aerc/config"
 )
 
 type Compose struct {
 	Headers  string `opt:"-H" action:"ParseHeader"`
-	Template string `opt:"-T"`
+	Template string `opt:"-T" complete:"CompleteTemplate"`
 	Edit     bool   `opt:"-e"`
 	NoEdit   bool   `opt:"-E"`
 	Body     string `opt:"..." required:"false"`
@@ -37,12 +38,12 @@ 	}
 	return nil
 }
 
-func (Compose) Aliases() []string {
-	return []string{"compose"}
+func (*Compose) CompleteTemplate(arg string) []string {
+	return commands.GetTemplates(arg)
 }
 
-func (Compose) Complete(args []string) []string {
-	return nil
+func (Compose) Aliases() []string {
+	return []string{"compose"}
 }
 
 func (c Compose) Execute(args []string) error {
diff --git a/commands/account/connection.go b/commands/account/connection.go
index b9cd887be80646980c07daabe0a7fe260917fed8..d633c1cea92bc706573663e70a7c0f5d92e13a52 100644
--- a/commands/account/connection.go
+++ b/commands/account/connection.go
@@ -18,10 +18,6 @@ func (Connection) Aliases() []string {
 	return []string{"connect", "disconnect"}
 }
 
-func (Connection) Complete(args []string) []string {
-	return nil
-}
-
 func (c Connection) Execute(args []string) error {
 	acct := app.SelectedAccount()
 	if acct == nil {
diff --git a/commands/account/expand-folder.go b/commands/account/expand-folder.go
index f26da70a1d71e80322dc5a66b30fc05fade006a2..c264872ad7248b20efc409963521429d6345bff0 100644
--- a/commands/account/expand-folder.go
+++ b/commands/account/expand-folder.go
@@ -16,10 +16,6 @@ func (ExpandCollapseFolder) Aliases() []string {
 	return []string{"expand-folder", "collapse-folder"}
 }
 
-func (ExpandCollapseFolder) Complete(args []string) []string {
-	return nil
-}
-
 func (ExpandCollapseFolder) Execute(args []string) error {
 	acct := app.SelectedAccount()
 	if acct == nil {
diff --git a/commands/account/export-mbox.go b/commands/account/export-mbox.go
index 00e03ca61d302974941e7ea58439e599cc2df50d..14d9290e6ae01eab6421c96a0a175659321aa2b4 100644
--- a/commands/account/export-mbox.go
+++ b/commands/account/export-mbox.go
@@ -17,7 +17,7 @@ 	"git.sr.ht/~rjarry/aerc/worker/types"
 )
 
 type ExportMbox struct {
-	Filename string `opt:"filename"`
+	Filename string `opt:"filename" complete:"CompleteFilename"`
 }
 
 func init() {
@@ -28,8 +28,8 @@ func (ExportMbox) Aliases() []string {
 	return []string{"export-mbox"}
 }
 
-func (ExportMbox) Complete(args []string) []string {
-	return commands.CompletePath(filepath.Join(args...))
+func (*ExportMbox) CompleteFilename(arg string) []string {
+	return commands.CompletePath(arg)
 }
 
 func (e ExportMbox) Execute(args []string) error {
diff --git a/commands/account/import-mbox.go b/commands/account/import-mbox.go
index b3ad2a089990f2c95d776fbf7a682786bf757443..774dfa8f2c72026cb9f0bbe43130c9f7b15e277b 100644
--- a/commands/account/import-mbox.go
+++ b/commands/account/import-mbox.go
@@ -6,7 +6,6 @@ 	"errors"
 	"fmt"
 	"io"
 	"os"
-	"path/filepath"
 	"sync/atomic"
 	"time"
 
@@ -19,7 +18,7 @@ 	"git.sr.ht/~rjarry/aerc/worker/types"
 )
 
 type ImportMbox struct {
-	Filename string `opt:"filename"`
+	Filename string `opt:"filename" complete:"CompleteFilename"`
 }
 
 func init() {
@@ -30,8 +29,8 @@ func (ImportMbox) Aliases() []string {
 	return []string{"import-mbox"}
 }
 
-func (ImportMbox) Complete(args []string) []string {
-	return commands.CompletePath(filepath.Join(args...))
+func (*ImportMbox) CompleteFilename(arg string) []string {
+	return commands.CompletePath(arg)
 }
 
 func (i ImportMbox) Execute(args []string) error {
diff --git a/commands/account/mkdir.go b/commands/account/mkdir.go
index 9beeb01a5e337958ea91472dbe6eb5a2bb03bf13..3e546acea4be670f340c7fb33cb130117fe78b1e 100644
--- a/commands/account/mkdir.go
+++ b/commands/account/mkdir.go
@@ -2,15 +2,15 @@ package account
 
 import (
 	"errors"
-	"strings"
 	"time"
 
 	"git.sr.ht/~rjarry/aerc/app"
+	"git.sr.ht/~rjarry/aerc/commands"
 	"git.sr.ht/~rjarry/aerc/worker/types"
 )
 
 type MakeDir struct {
-	Folder string `opt:"..." metavar:"<folder>"`
+	Folder string `opt:"folder" complete:"CompleteFolder"`
 }
 
 func init() {
@@ -21,26 +21,15 @@ func (MakeDir) Aliases() []string {
 	return []string{"mkdir"}
 }
 
-func (MakeDir) Complete(args []string) []string {
-	if len(args) == 0 {
+func (*MakeDir) CompleteFolder(arg string) []string {
+	acct := app.SelectedAccount()
+	if acct == nil {
 		return nil
 	}
-	name := strings.Join(args, " ")
-
-	list := app.SelectedAccount().Directories().List()
-	inboxes := make([]string, len(list))
-	copy(inboxes, list)
-
-	// remove inboxes that don't match and append the path separator to all
-	// others
-	for i := len(inboxes) - 1; i >= 0; i-- {
-		if !strings.HasPrefix(inboxes[i], name) && name != "" {
-			inboxes = append(inboxes[:i], inboxes[i+1:]...)
-			continue
-		}
-		inboxes[i] += app.SelectedAccount().Worker().PathSeparator()
-	}
-	return inboxes
+	return commands.FilterList(
+		acct.Directories().List(), arg, "",
+		app.SelectedAccount().Worker().PathSeparator(),
+		app.SelectedAccountUiConfig().FuzzyComplete)
 }
 
 func (m MakeDir) Execute(args []string) error {
diff --git a/commands/account/next-folder.go b/commands/account/next-folder.go
index 1b5651c8315c74510d6f1d7a4b1b5ebe752e3a1f..f44abdc108e892d128b453b95c8d044b3bc95032 100644
--- a/commands/account/next-folder.go
+++ b/commands/account/next-folder.go
@@ -18,10 +18,6 @@ func (NextPrevFolder) Aliases() []string {
 	return []string{"next-folder", "prev-folder"}
 }
 
-func (NextPrevFolder) Complete(args []string) []string {
-	return nil
-}
-
 func (np NextPrevFolder) Execute(args []string) error {
 	acct := app.SelectedAccount()
 	if acct == nil {
diff --git a/commands/account/next-result.go b/commands/account/next-result.go
index e841899fa41d6756e204d9721e0ce232f66af523..d624e5599de4de4fccc3aa034902f28df347b3e6 100644
--- a/commands/account/next-result.go
+++ b/commands/account/next-result.go
@@ -17,10 +17,6 @@ func (NextPrevResult) Aliases() []string {
 	return []string{"next-result", "prev-result"}
 }
 
-func (NextPrevResult) Complete(args []string) []string {
-	return nil
-}
-
 func (NextPrevResult) Execute(args []string) error {
 	acct := app.SelectedAccount()
 	if acct == nil {
diff --git a/commands/account/next.go b/commands/account/next.go
index 14679b1b0a1bdb14021c49ba931fa403d8f8fd44..142f615161ab3198353d14e19eb3ae33b9fb45f3 100644
--- a/commands/account/next.go
+++ b/commands/account/next.go
@@ -35,10 +35,6 @@ func (NextPrevMsg) Aliases() []string {
 	return []string{"next", "next-message", "prev", "prev-message"}
 }
 
-func (NextPrevMsg) Complete(args []string) []string {
-	return nil
-}
-
 func (np NextPrevMsg) Execute(args []string) error {
 	acct := app.SelectedAccount()
 	if acct == nil {
diff --git a/commands/account/recover.go b/commands/account/recover.go
index dae3d807246125f719c602a884e251deec9e73d5..cba6e0cb68cb5b4143ad2307c3bf903ae2720662 100644
--- a/commands/account/recover.go
+++ b/commands/account/recover.go
@@ -16,7 +16,7 @@ type Recover struct {
 	Force  bool   `opt:"-f"`
 	Edit   bool   `opt:"-e"`
 	NoEdit bool   `opt:"-E"`
-	File   string `opt:"file"`
+	File   string `opt:"file" complete:"CompleteFile"`
 }
 
 func init() {
@@ -31,7 +31,7 @@ func (Recover) Options() string {
 	return "feE"
 }
 
-func (r Recover) Complete(args []string) []string {
+func (*Recover) CompleteFile(arg string) []string {
 	// file name of temp file is hard-coded in the NewComposer() function
 	files, err := filepath.Glob(
 		filepath.Join(os.TempDir(), "aerc-compose-*.eml"),
@@ -39,8 +39,7 @@ 	)
 	if err != nil {
 		return nil
 	}
-	return commands.CompletionFromList(files,
-		commands.Operands(args, r.Options()))
+	return commands.CompletionFromList(files, arg)
 }
 
 func (r Recover) Execute(args []string) error {
diff --git a/commands/account/rmdir.go b/commands/account/rmdir.go
index 36bfc69940b2bb849c2ce940a7397d0c621256e6..398f375f41270424caea94d858ce0c755e96a748 100644
--- a/commands/account/rmdir.go
+++ b/commands/account/rmdir.go
@@ -20,10 +20,6 @@ func (RemoveDir) Aliases() []string {
 	return []string{"rmdir"}
 }
 
-func (RemoveDir) Complete(args []string) []string {
-	return nil
-}
-
 func (r RemoveDir) Execute(args []string) error {
 	acct := app.SelectedAccount()
 	if acct == nil {
diff --git a/commands/account/search.go b/commands/account/search.go
index bb5617c062cabecad17b6b9f0a11c919fa6e5357..ca1b9684a5de2c11ae8a4f40af9f9e7954791510 100644
--- a/commands/account/search.go
+++ b/commands/account/search.go
@@ -23,47 +23,34 @@ 	Unread       bool                 `opt:"-u" action:"ParseUnread"`
 	Body         bool                 `opt:"-b"`
 	All          bool                 `opt:"-a"`
 	Headers      textproto.MIMEHeader `opt:"-H" action:"ParseHeader" metavar:"<header>:<value>"`
-	WithFlags    models.Flags         `opt:"-x" action:"ParseFlag"`
-	WithoutFlags models.Flags         `opt:"-X" action:"ParseNotFlag"`
-	To           []string             `opt:"-t" action:"ParseTo"`
-	From         []string             `opt:"-f" action:"ParseFrom"`
-	Cc           []string             `opt:"-c" action:"ParseCc"`
-	StartDate    time.Time            `opt:"-d" action:"ParseDate"`
+	WithFlags    models.Flags         `opt:"-x" action:"ParseFlag" complete:"CompleteFlag"`
+	WithoutFlags models.Flags         `opt:"-X" action:"ParseNotFlag" complete:"CompleteFlag"`
+	To           []string             `opt:"-t" action:"ParseTo" complete:"CompleteAddress"`
+	From         []string             `opt:"-f" action:"ParseFrom" complete:"CompleteAddress"`
+	Cc           []string             `opt:"-c" action:"ParseCc" complete:"CompleteAddress"`
+	StartDate    time.Time            `opt:"-d" action:"ParseDate" complete:"CompleteDate"`
 	EndDate      time.Time
 	Terms        string `opt:"..." required:"false"`
 }
 
 func init() {
 	register(SearchFilter{})
-}
-
-func (SearchFilter) Options() string {
-	return "rubax:X:t:H:f:c:d:"
 }
 
 func (SearchFilter) Aliases() []string {
 	return []string{"search", "filter"}
 }
 
-func (s SearchFilter) CompleteOption(
-	r rune,
-	search string,
-) []string {
-	var valid []string
-	switch r {
-	case 'x', 'X':
-		valid = commands.GetFlagList()
-	case 't', 'f', 'c':
-		valid = commands.GetAddress(search)
-	case 'd':
-		valid = commands.GetDateList()
-	default:
-	}
-	return commands.CompletionFromList(valid, []string{search})
+func (*SearchFilter) CompleteFlag(arg string) []string {
+	return commands.CompletionFromList(commands.GetFlagList(), arg)
+}
+
+func (*SearchFilter) CompleteAddress(arg string) []string {
+	return commands.CompletionFromList(commands.GetAddress(arg), arg)
 }
 
-func (SearchFilter) Complete(args []string) []string {
-	return nil
+func (*SearchFilter) CompleteDate(arg string) []string {
+	return commands.CompletionFromList(commands.GetDateList(), arg)
 }
 
 func (s *SearchFilter) ParseRead(arg string) error {
diff --git a/commands/account/select.go b/commands/account/select.go
index efd8f53b591ffed221ab29b68b9149bfb2f561a0..884b4bcef355895a372fa0c81268ebe9c970b58f 100644
--- a/commands/account/select.go
+++ b/commands/account/select.go
@@ -18,10 +18,6 @@ func (SelectMessage) Aliases() []string {
 	return []string{"select", "select-message"}
 }
 
-func (SelectMessage) Complete(args []string) []string {
-	return nil
-}
-
 func (s SelectMessage) Execute(args []string) error {
 	acct := app.SelectedAccount()
 	if acct == nil {
diff --git a/commands/account/sort.go b/commands/account/sort.go
index 2adfbf19f0b767671ec3ed4b5b1ff78bd68fddd0..ccccab25137434b84f3681a3b5ceb272eb813160 100644
--- a/commands/account/sort.go
+++ b/commands/account/sort.go
@@ -2,7 +2,6 @@ package account
 
 import (
 	"errors"
-	"strings"
 
 	"git.sr.ht/~rjarry/aerc/app"
 	"git.sr.ht/~rjarry/aerc/commands"
@@ -13,6 +12,9 @@ )
 
 type Sort struct {
 	Unused struct{} `opt:"-"`
+	// these fields are only used for completion
+	Reverse  bool     `opt:"-r"`
+	Criteria []string `opt:"criteria" complete:"CompleteCriteria"`
 }
 
 func init() {
@@ -23,46 +25,20 @@ func (Sort) Aliases() []string {
 	return []string{"sort"}
 }
 
-func (Sort) Complete(args []string) []string {
-	supportedCriteria := []string{
-		"arrival",
-		"cc",
-		"date",
-		"from",
-		"read",
-		"size",
-		"subject",
-		"to",
-		"flagged",
-	}
-	if len(args) == 0 {
-		return supportedCriteria
-	}
-	last := args[len(args)-1]
-	var completions []string
-	currentPrefix := strings.Join(args, " ") + " "
-	// if there is a completed criteria or option then suggest all again
-	for _, criteria := range append(supportedCriteria, "-r") {
-		if criteria == last {
-			for _, criteria := range supportedCriteria {
-				completions = append(completions, currentPrefix+criteria)
-			}
-			return completions
-		}
-	}
+var supportedCriteria = []string{
+	"arrival",
+	"cc",
+	"date",
+	"from",
+	"read",
+	"size",
+	"subject",
+	"to",
+	"flagged",
+}
 
-	currentPrefix = strings.Join(args[:len(args)-1], " ")
-	if len(args) > 1 {
-		currentPrefix += " "
-	}
-	// last was beginning an option
-	if last == "-" {
-		return []string{currentPrefix + "-r"}
-	}
-	// the last item is not complete
-	completions = commands.FilterList(supportedCriteria, last, currentPrefix,
-		app.SelectedAccountUiConfig().FuzzyComplete)
-	return completions
+func (*Sort) CompleteCriteria(arg string) []string {
+	return commands.CompletionFromList(supportedCriteria, arg)
 }
 
 func (Sort) Execute(args []string) error {
diff --git a/commands/account/split.go b/commands/account/split.go
index 4774297fd0c9bf33cb49236272b46dcc1871e951..8690d99a055d97cc7aef2b35fa8928b71f3017f6 100644
--- a/commands/account/split.go
+++ b/commands/account/split.go
@@ -33,10 +33,6 @@ func (Split) Aliases() []string {
 	return []string{"split", "vsplit", "hsplit"}
 }
 
-func (Split) Complete(args []string) []string {
-	return nil
-}
-
 func (s Split) Execute(args []string) error {
 	acct := app.SelectedAccount()
 	if acct == nil {
diff --git a/commands/account/view.go b/commands/account/view.go
index 701a773826c7fac1ee5141e72012d8e0fc7894cf..0cdc175df1cd6b37ab05ed9a73fde8b0a2a916b7 100644
--- a/commands/account/view.go
+++ b/commands/account/view.go
@@ -19,10 +19,6 @@ func (ViewMessage) Aliases() []string {
 	return []string{"view-message", "view"}
 }
 
-func (ViewMessage) Complete(args []string) []string {
-	return nil
-}
-
 func (v ViewMessage) Execute(args []string) error {
 	acct := app.SelectedAccount()
 	if acct == nil {
diff --git a/commands/cd.go b/commands/cd.go
index 6620e732b327524f1f44a216adc102a3c3cd0104..d2dc531063689908c596db648129999723de73ee 100644
--- a/commands/cd.go
+++ b/commands/cd.go
@@ -12,7 +12,7 @@
 var previousDir string
 
 type ChangeDirectory struct {
-	Target string `opt:"directory" default:"~"`
+	Target string `opt:"directory" default:"~" complete:"CompleteTarget"`
 }
 
 func init() {
@@ -23,9 +23,8 @@ func (ChangeDirectory) Aliases() []string {
 	return []string{"cd"}
 }
 
-func (ChangeDirectory) Complete(args []string) []string {
-	path := strings.Join(args, " ")
-	completions := CompletePath(path)
+func (*ChangeDirectory) CompleteTarget(arg string) []string {
+	completions := CompletePath(arg)
 
 	var dirs []string
 	for _, c := range completions {
diff --git a/commands/choose.go b/commands/choose.go
index 4e2007fd2460e6de9ab0d88a8b6e7446c10c2f65..19958dce8fcde4c92af66fccaa7626dbe5d9394b 100644
--- a/commands/choose.go
+++ b/commands/choose.go
@@ -18,10 +18,6 @@ func (Choose) Aliases() []string {
 	return []string{"choose"}
 }
 
-func (Choose) Complete(args []string) []string {
-	return nil
-}
-
 func (Choose) Execute(args []string) error {
 	if len(args) < 5 || len(args)%4 != 1 {
 		return chooseUsage(args[0])
diff --git a/commands/commands.go b/commands/commands.go
index d76194af90df9603823a99734e2d69faa8e1b221..9c193018c0a07143c4e0dd546d9e937536f72406 100644
--- a/commands/commands.go
+++ b/commands/commands.go
@@ -3,12 +3,13 @@
 import (
 	"bytes"
 	"errors"
+	"path"
 	"reflect"
+	"sort"
 	"strings"
 	"unicode"
 
 	"git.sr.ht/~rjarry/go-opt"
-	"github.com/google/shlex"
 
 	"git.sr.ht/~rjarry/aerc/app"
 	"git.sr.ht/~rjarry/aerc/config"
@@ -21,17 +22,6 @@
 type Command interface {
 	Aliases() []string
 	Execute([]string) error
-	Complete([]string) []string
-}
-
-type OptionsProvider interface {
-	Command
-	Options() string
-}
-
-type OptionCompleter interface {
-	OptionsProvider
-	CompleteOption(rune, string) []string
 }
 
 type Commands map[string]Command
@@ -174,6 +164,7 @@ 		options := FilterList(
 			templates.Terms(),
 			strings.TrimSpace(search),
 			"",
+			"",
 			app.SelectedAccountUiConfig().FuzzyComplete,
 		)
 		return options, prefix + padding, true
@@ -194,117 +185,62 @@ // GetCompletions returns the completion options and the command prefix
 func GetCompletions(
 	cmd Command, args *opt.Args,
 ) (options []string, prefix string) {
-	// complete options
-	var spec string
-	if provider, ok := cmd.(OptionsProvider); ok {
-		spec = provider.Options()
-	}
-
-	parser, err := newParser(args.String(), spec, strings.HasSuffix(args.String(), " "))
-	if err != nil {
-		log.Debugf("completion parser failed: %v", err)
-		return
-	}
-
-	switch parser.kind {
-	case SHORT_OPTION:
-		for _, r := range strings.ReplaceAll(spec, ":", "") {
-			if strings.ContainsRune(parser.flag, r) {
-				continue
-			}
-			option := string(r)
-			if strings.Contains(spec, option+":") {
-				option += " "
-			}
-			options = append(options, option)
-		}
-		prefix = args.String()
-	case OPTION_ARGUMENT:
-		cmpl, ok := cmd.(OptionCompleter)
-		if !ok {
-			return
-		}
-		stem := args.String()
-		if parser.arg != "" {
-			stem = strings.TrimSuffix(stem, parser.arg)
-		}
-		pad := ""
-		if !strings.HasSuffix(stem, " ") {
-			pad += " "
-		}
-		s := parser.flag
-		r := rune(s[len(s)-1])
-		for _, option := range cmpl.CompleteOption(r, parser.arg) {
-			options = append(options, pad+escape(option)+" ")
-		}
-		prefix = stem
-	case OPERAND:
-		clone := args.Clone()
-		clone.Cut(clone.Count() - parser.optind)
-		args.Shift(1)
-		for _, option := range cmd.Complete(args.Args()) {
-			if strings.Contains(option, "  ") {
-				option = escape(option)
-			}
-			options = append(options, " "+option)
-		}
-		prefix = clone.String()
-	}
-
-	return
+	// copy zeroed struct
+	tmp := reflect.New(reflect.TypeOf(cmd)).Interface().(Command)
+	spec := opt.NewCmdSpec(args.Arg(0), tmp)
+	return spec.GetCompletions(args)
 }
 
-func GetFolders(args []string) []string {
+func GetFolders(arg string) []string {
 	acct := app.SelectedAccount()
 	if acct == nil {
 		return make([]string, 0)
 	}
-	if len(args) == 0 {
-		return acct.Directories().List()
-	}
-	return FilterList(acct.Directories().List(), args[0], "", acct.UiConfig().FuzzyComplete)
+	return CompletionFromList(acct.Directories().List(), arg)
 }
 
-// CompletionFromList provides a convenience wrapper for commands to use in the
-// Complete function. It simply matches the items provided in valid
-func CompletionFromList(valid []string, args []string) []string {
-	if len(args) == 0 {
-		return valid
+func GetTemplates(arg string) []string {
+	templates := make(map[string]bool)
+	for _, dir := range config.Templates.TemplateDirs {
+		for _, f := range listDir(dir, false) {
+			if !isDir(path.Join(dir, f)) {
+				templates[f] = true
+			}
+		}
+	}
+	names := make([]string, len(templates))
+	for n := range templates {
+		names = append(names, n)
 	}
-	return FilterList(valid, args[0], "", app.SelectedAccountUiConfig().FuzzyComplete)
+	sort.Strings(names)
+	return CompletionFromList(names, arg)
 }
 
-func GetLabels(args []string) []string {
+// CompletionFromList provides a convenience wrapper for commands to use in a
+// complete callback. It simply matches the items provided in valid
+func CompletionFromList(valid []string, arg string) []string {
+	return FilterList(valid, arg, "", "", app.SelectedAccountUiConfig().FuzzyComplete)
+}
+
+func GetLabels(arg string) []string {
 	acct := app.SelectedAccount()
 	if acct == nil {
 		return make([]string, 0)
 	}
-	if len(args) == 0 {
-		return acct.Labels()
-	}
-
-	// + and - are used to denote tag addition / removal and need to be striped
-	// only the last tag should be completed, so that multiple labels can be
-	// selected
-	last := args[len(args)-1]
-	others := strings.Join(args[:len(args)-1], " ")
 	var prefix string
-	switch last[0] {
-	case '+':
-		prefix = "+"
-	case '-':
-		prefix = "-"
-	default:
-		prefix = ""
+	if arg != "" {
+		// + and - are used to denote tag addition / removal and need to
+		// be striped only the last tag should be completed, so that
+		// multiple labels can be selected
+		switch arg[0] {
+		case '+':
+			prefix = "+"
+		case '-':
+			prefix = "-"
+		}
+		arg = strings.TrimLeft(arg, "+-")
 	}
-	trimmed := strings.TrimLeft(last, "+-")
-
-	var prev string
-	if len(others) > 0 {
-		prev = others + " "
-	}
-	out := FilterList(acct.Labels(), trimmed, prev+prefix, acct.UiConfig().FuzzyComplete)
-	return out
+	return FilterList(acct.Labels(), arg, prefix, " ", acct.UiConfig().FuzzyComplete)
 }
 
 // hasCaseSmartPrefix checks whether s starts with prefix, using a case
@@ -324,19 +260,3 @@ 		}
 	}
 	return false
 }
-
-// splitCmd splits the command into arguments
-func splitCmd(cmd string) ([]string, error) {
-	args, err := shlex.Split(cmd)
-	if err != nil {
-		return nil, err
-	}
-	return args, nil
-}
-
-func escape(s string) string {
-	if strings.Contains(s, " ") {
-		return strings.ReplaceAll(s, " ", "\\ ")
-	}
-	return s
-}
diff --git a/commands/compose/abort.go b/commands/compose/abort.go
index 4bacb9c3fb043319d07ec11a5c6ba935c7063932..cde43f46894e0752bc78182d7040cc6dd17a92af 100644
--- a/commands/compose/abort.go
+++ b/commands/compose/abort.go
@@ -14,10 +14,6 @@ func (Abort) Aliases() []string {
 	return []string{"abort"}
 }
 
-func (Abort) Complete(args []string) []string {
-	return nil
-}
-
 func (Abort) Execute(args []string) error {
 	composer, _ := app.SelectedTabContent().(*app.Composer)
 	app.RemoveTab(composer, true)
diff --git a/commands/compose/attach-key.go b/commands/compose/attach-key.go
index e81a6a5abfb96701934ca757b08d9324833b9ac4..6ba5eae0d7356d6c6156a0033a6f2972a39cbb1e 100644
--- a/commands/compose/attach-key.go
+++ b/commands/compose/attach-key.go
@@ -14,10 +14,6 @@ func (AttachKey) Aliases() []string {
 	return []string{"attach-key"}
 }
 
-func (AttachKey) Complete(args []string) []string {
-	return nil
-}
-
 func (AttachKey) Execute(args []string) error {
 	composer, _ := app.SelectedTabContent().(*app.Composer)
 	return composer.SetAttachKey(!composer.AttachKey())
diff --git a/commands/compose/attach.go b/commands/compose/attach.go
index 3acf28c2b60551609fe896db6283a937eb138a29..49d63bb8056a37119e99f76e0955b20dd443cbf2 100644
--- a/commands/compose/attach.go
+++ b/commands/compose/attach.go
@@ -23,7 +23,8 @@
 type Attach struct {
 	Menu bool   `opt:"-m"`
 	Name string `opt:"-r"`
-	Path string `opt:"..." metavar:"<path>" required:"false"`
+	Path string `opt:"path" required:"false" complete:"CompletePath"`
+	Args string `opt:"..." required:"false"`
 }
 
 func init() {
@@ -34,9 +35,8 @@ func (Attach) Aliases() []string {
 	return []string{"attach"}
 }
 
-func (Attach) Complete(args []string) []string {
-	path := strings.Join(args, " ")
-	return commands.CompletePath(path)
+func (*Attach) CompletePath(arg string) []string {
+	return commands.CompletePath(arg)
 }
 
 func (a Attach) Execute(args []string) error {
@@ -52,6 +52,9 @@ 			return errors.New("command is required")
 		}
 		return a.readCommand()
 	default:
+		if a.Args != "" {
+			return errors.New("only a single path is supported")
+		}
 		return a.addPath(a.Path)
 	}
 }
@@ -186,7 +189,7 @@ 	return nil
 }
 
 func (a Attach) readCommand() error {
-	cmd := exec.Command("sh", "-c", a.Path)
+	cmd := exec.Command("sh", "-c", a.Path+" "+a.Args)
 
 	data, err := cmd.Output()
 	if err != nil {
diff --git a/commands/compose/cc-bcc.go b/commands/compose/cc-bcc.go
index aeb6af976ad6ab8f8d0f4c6146349d09670668bf..dd5e96141b9c76b37b157a34be14e9341c66220a 100644
--- a/commands/compose/cc-bcc.go
+++ b/commands/compose/cc-bcc.go
@@ -16,10 +16,6 @@ func (CC) Aliases() []string {
 	return []string{"cc", "bcc"}
 }
 
-func (CC) Complete(args []string) []string {
-	return nil
-}
-
 func (c CC) Execute(args []string) error {
 	composer, _ := app.SelectedTabContent().(*app.Composer)
 
diff --git a/commands/compose/detach.go b/commands/compose/detach.go
index a2996516ec0bad4e875b1ff2c6256b96df78f5d0..91cf2a58c8495f9cb6cb46bdaa019d3d160aa26f 100644
--- a/commands/compose/detach.go
+++ b/commands/compose/detach.go
@@ -4,10 +4,11 @@ import (
 	"fmt"
 
 	"git.sr.ht/~rjarry/aerc/app"
+	"git.sr.ht/~rjarry/aerc/commands"
 )
 
 type Detach struct {
-	Path string `opt:"path" required:"false"`
+	Path string `opt:"path" required:"false" complete:"CompletePath"`
 }
 
 func init() {
@@ -18,9 +19,9 @@ func (Detach) Aliases() []string {
 	return []string{"detach"}
 }
 
-func (Detach) Complete(args []string) []string {
+func (*Detach) CompletePath(arg string) []string {
 	composer, _ := app.SelectedTabContent().(*app.Composer)
-	return composer.GetAttachments()
+	return commands.CompletionFromList(composer.GetAttachments(), arg)
 }
 
 func (d Detach) Execute(args []string) error {
diff --git a/commands/compose/edit.go b/commands/compose/edit.go
index 80f4e6f440cd6ac8848a6b5e26b4cf9d34c27cda..1929f45a5d7e0ed5beccb3843712d076e925ee14 100644
--- a/commands/compose/edit.go
+++ b/commands/compose/edit.go
@@ -20,10 +20,6 @@ func (Edit) Aliases() []string {
 	return []string{"edit"}
 }
 
-func (Edit) Complete(args []string) []string {
-	return nil
-}
-
 func (e Edit) Execute(args []string) error {
 	composer, ok := app.SelectedTabContent().(*app.Composer)
 	if !ok {
diff --git a/commands/compose/encrypt.go b/commands/compose/encrypt.go
index 3121dff0c33329f411b362127b2aaf81197feaa3..ee902f6e361237f3c1925fb659bcfe9d05e5febf 100644
--- a/commands/compose/encrypt.go
+++ b/commands/compose/encrypt.go
@@ -14,10 +14,6 @@ func (Encrypt) Aliases() []string {
 	return []string{"encrypt"}
 }
 
-func (Encrypt) Complete(args []string) []string {
-	return nil
-}
-
 func (Encrypt) Execute(args []string) error {
 	composer, _ := app.SelectedTabContent().(*app.Composer)
 	composer.SetEncrypt(!composer.Encrypt())
diff --git a/commands/compose/header.go b/commands/compose/header.go
index aaa14e43663e0763fff5fa04e662701102fbd7dd..afc27e9257778481989b2eeca3f166d1efdedc03 100644
--- a/commands/compose/header.go
+++ b/commands/compose/header.go
@@ -11,7 +11,7 @@
 type Header struct {
 	Force  bool   `opt:"-f"`
 	Remove bool   `opt:"-d"`
-	Name   string `opt:"name"`
+	Name   string `opt:"name" complete:"CompleteHeaders"`
 	Value  string `opt:"..." required:"false"`
 }
 
@@ -37,8 +37,8 @@ func (Header) Options() string {
 	return "fd"
 }
 
-func (Header) Complete(args []string) []string {
-	return commands.CompletionFromList(headers, args)
+func (*Header) CompleteHeaders(arg string) []string {
+	return commands.CompletionFromList(headers, arg)
 }
 
 func (h Header) Execute(args []string) error {
diff --git a/commands/compose/multipart.go b/commands/compose/multipart.go
index 969410627f589b48feb514c5aff3683cf62f2f89..5b70134264064f3a615c553d13d64d98a7c73636 100644
--- a/commands/compose/multipart.go
+++ b/commands/compose/multipart.go
@@ -11,7 +11,7 @@ )
 
 type Multipart struct {
 	Remove bool   `opt:"-d"`
-	Mime   string `opt:"mime" metavar:"<mime/type>"`
+	Mime   string `opt:"mime" metavar:"<mime/type>" complete:"CompleteMime"`
 }
 
 func init() {
@@ -22,13 +22,12 @@ func (Multipart) Aliases() []string {
 	return []string{"multipart"}
 }
 
-func (Multipart) Complete(args []string) []string {
+func (*Multipart) CompleteMime(arg string) []string {
 	var completions []string
-	completions = append(completions, "-d")
 	for mime := range config.Converters {
 		completions = append(completions, mime)
 	}
-	return commands.CompletionFromList(completions, args)
+	return commands.CompletionFromList(completions, arg)
 }
 
 func (m Multipart) Execute(args []string) error {
diff --git a/commands/compose/next-field.go b/commands/compose/next-field.go
index 88fbb03dabe89e4a513f0f254b01788467fe1e9e..396c1ce10bea902e7b50b4870dc5a0758ee9773d 100644
--- a/commands/compose/next-field.go
+++ b/commands/compose/next-field.go
@@ -14,10 +14,6 @@ func (NextPrevField) Aliases() []string {
 	return []string{"next-field", "prev-field"}
 }
 
-func (NextPrevField) Complete(args []string) []string {
-	return nil
-}
-
 func (NextPrevField) Execute(args []string) error {
 	composer, _ := app.SelectedTabContent().(*app.Composer)
 	if args[0] == "prev-field" {
diff --git a/commands/compose/postpone.go b/commands/compose/postpone.go
index 767e759c6f1ccafe332246c2bbd715487260f971..3ce9cc849ede1c8cb050044bede0f35b45c29887 100644
--- a/commands/compose/postpone.go
+++ b/commands/compose/postpone.go
@@ -14,7 +14,7 @@ 	"git.sr.ht/~rjarry/aerc/worker/types"
 )
 
 type Postpone struct {
-	Folder string `opt:"-t"`
+	Folder string `opt:"-t" complete:"CompleteFolder"`
 }
 
 func init() {
@@ -25,20 +25,8 @@ func (Postpone) Aliases() []string {
 	return []string{"postpone"}
 }
 
-func (Postpone) Options() string {
-	return "t:"
-}
-
-func (Postpone) CompleteOption(r rune, arg string) []string {
-	var valid []string
-	if r == 't' {
-		valid = commands.GetFolders([]string{arg})
-	}
-	return commands.CompletionFromList(valid, []string{arg})
-}
-
-func (Postpone) Complete(args []string) []string {
-	return nil
+func (*Postpone) CompleteFolder(arg string) []string {
+	return commands.GetFolders(arg)
 }
 
 func (p Postpone) Execute(args []string) error {
diff --git a/commands/compose/send.go b/commands/compose/send.go
index e36724714c32363132a6b81c1ec8c6c370a0a2d1..6f90562841cbdd6e39cc0c66601809fb0df05612 100644
--- a/commands/compose/send.go
+++ b/commands/compose/send.go
@@ -28,8 +28,8 @@ 	"golang.org/x/oauth2"
 )
 
 type Send struct {
-	Archive string `opt:"-a" action:"ParseArchive" metavar:"flat|year|month"`
-	CopyTo  string `opt:"-t"`
+	Archive string `opt:"-a" action:"ParseArchive" metavar:"flat|year|month" complete:"CompleteArchive"`
+	CopyTo  string `opt:"-t" complete:"CompleteFolders"`
 }
 
 func init() {
@@ -40,19 +40,12 @@ func (Send) Aliases() []string {
 	return []string{"send"}
 }
 
-func (Send) Options() string {
-	return "a:t:"
+func (*Send) CompleteArchive(arg string) []string {
+	return commands.CompletionFromList(msg.ARCHIVE_TYPES, arg)
 }
 
-func (s Send) CompleteOption(r rune, term string) []string {
-	if r == 't' {
-		return commands.GetFolders([]string{term})
-	}
-	return nil
-}
-
-func (Send) Complete(args []string) []string {
-	return nil
+func (*Send) CompleteFolders(arg string) []string {
+	return commands.GetFolders(arg)
 }
 
 func (s *Send) ParseArchive(arg string) error {
diff --git a/commands/compose/sign.go b/commands/compose/sign.go
index e30fca050bcf2ca0b8abe2a52be0976f7de58912..faeceed411d9aecf4475628b41f1aa38fe7fe7f2 100644
--- a/commands/compose/sign.go
+++ b/commands/compose/sign.go
@@ -16,10 +16,6 @@ func (Sign) Aliases() []string {
 	return []string{"sign"}
 }
 
-func (Sign) Complete(args []string) []string {
-	return nil
-}
-
 func (Sign) Execute(args []string) error {
 	composer, _ := app.SelectedTabContent().(*app.Composer)
 
diff --git a/commands/compose/switch.go b/commands/compose/switch.go
index 0c027a41edc3ca81fffecd0efa491d7c7cce9492..637099b593ad928bdf20ad9d9aaae347ea10d466 100644
--- a/commands/compose/switch.go
+++ b/commands/compose/switch.go
@@ -4,6 +4,7 @@ import (
 	"errors"
 
 	"git.sr.ht/~rjarry/aerc/app"
+	"git.sr.ht/~rjarry/aerc/commands"
 )
 
 type AccountSwitcher interface {
@@ -13,7 +14,7 @@
 type SwitchAccount struct {
 	Next    bool   `opt:"-n"`
 	Prev    bool   `opt:"-p"`
-	Account string `opt:"..." metavar:"<account>" required:"false"`
+	Account string `opt:"account" required:"false" complete:"CompleteAccount"`
 }
 
 func init() {
@@ -24,8 +25,8 @@ func (SwitchAccount) Aliases() []string {
 	return []string{"switch-account"}
 }
 
-func (SwitchAccount) Complete(args []string) []string {
-	return app.AccountNames()
+func (*SwitchAccount) CompleteAccount(arg string) []string {
+	return commands.CompletionFromList(app.AccountNames(), arg)
 }
 
 func (s SwitchAccount) Execute(args []string) error {
diff --git a/commands/ct.go b/commands/ct.go
index 8a6bb063e19c99cf30f080a241ee7e09720ead1e..2d057b4fc89f02f08cb105ccedb6fc0cce0c1bbc 100644
--- a/commands/ct.go
+++ b/commands/ct.go
@@ -9,7 +9,7 @@ 	"git.sr.ht/~rjarry/aerc/app"
 )
 
 type ChangeTab struct {
-	Tab string `opt:"tab"`
+	Tab string `opt:"tab" complete:"CompleteTab"`
 }
 
 func init() {
@@ -20,12 +20,8 @@ func (ChangeTab) Aliases() []string {
 	return []string{"ct", "change-tab"}
 }
 
-func (ChangeTab) Complete(args []string) []string {
-	if len(args) == 0 {
-		return app.TabNames()
-	}
-	joinedArgs := strings.Join(args, " ")
-	return FilterList(app.TabNames(), joinedArgs, "", app.SelectedAccountUiConfig().FuzzyComplete)
+func (*ChangeTab) CompleteTab(arg string) []string {
+	return CompletionFromList(app.TabNames(), arg)
 }
 
 func (c ChangeTab) Execute(args []string) error {
diff --git a/commands/eml.go b/commands/eml.go
index adacd05ba29c3cf5977c8d90132a39f85335485a..fe7352430c8dfa2e9b040b990b16178036ed303c 100644
--- a/commands/eml.go
+++ b/commands/eml.go
@@ -5,14 +5,13 @@ 	"bytes"
 	"fmt"
 	"io"
 	"os"
-	"strings"
 
 	"git.sr.ht/~rjarry/aerc/app"
 	"git.sr.ht/~rjarry/aerc/lib"
 )
 
 type Eml struct {
-	Path string `opt:"path" required:"false"`
+	Path string `opt:"path" required:"false" complete:"CompletePath"`
 }
 
 func init() {
@@ -23,8 +22,8 @@ func (Eml) Aliases() []string {
 	return []string{"eml", "preview"}
 }
 
-func (Eml) Complete(args []string) []string {
-	return CompletePath(strings.Join(args, " "))
+func (*Eml) CompletePath(arg string) []string {
+	return CompletePath(arg)
 }
 
 func (e Eml) Execute(args []string) error {
diff --git a/commands/exec.go b/commands/exec.go
index 4c2f3d1b313db2b4a6295f3000b92d0988b6e1aa..2a3ed5f56f45b4a49e43128aae65f5d89000bd85 100644
--- a/commands/exec.go
+++ b/commands/exec.go
@@ -22,10 +22,6 @@ func (ExecCmd) Aliases() []string {
 	return []string{"exec"}
 }
 
-func (ExecCmd) Complete(args []string) []string {
-	return nil
-}
-
 func (e ExecCmd) Execute(args []string) error {
 	cmd := exec.Command(e.Args[0], e.Args[1:]...)
 	env := os.Environ()
diff --git a/commands/help.go b/commands/help.go
index b2bcdf7c65338385247f857cbc337be95f88ab66..0733230358d58bf8f9bd87f777143f2b5ec6977c 100644
--- a/commands/help.go
+++ b/commands/help.go
@@ -7,7 +7,7 @@ 	"git.sr.ht/~rjarry/aerc/app"
 )
 
 type Help struct {
-	Topic string `opt:"topic" action:"ParseTopic" default:"aerc"`
+	Topic string `opt:"topic" action:"ParseTopic" default:"aerc" complete:"CompleteTopic"`
 }
 
 var pages = []string{
@@ -35,8 +35,8 @@ func (Help) Aliases() []string {
 	return []string{"help"}
 }
 
-func (Help) Complete(args []string) []string {
-	return CompletionFromList(pages, args)
+func (*Help) CompleteTopic(arg string) []string {
+	return CompletionFromList(pages, arg)
 }
 
 func (h *Help) ParseTopic(arg string) error {
diff --git a/commands/move-tab.go b/commands/move-tab.go
index 23580f2d2e961ef22eeea569641529d2a9265481..7ebe269ccd8697e51e5ed4bb79f15586e5bc5b24 100644
--- a/commands/move-tab.go
+++ b/commands/move-tab.go
@@ -32,10 +32,6 @@ func (MoveTab) Aliases() []string {
 	return []string{"move-tab"}
 }
 
-func (MoveTab) Complete(args []string) []string {
-	return nil
-}
-
 func (m MoveTab) Execute(args []string) error {
 	app.MoveTab(m.Index, m.Relative)
 	return nil
diff --git a/commands/msg/archive.go b/commands/msg/archive.go
index f4d6e3be286afcd4155b890aaed8ac381ce2df45..34cba8b8fbdc50a6f634e178890eca9d7432c7af 100644
--- a/commands/msg/archive.go
+++ b/commands/msg/archive.go
@@ -21,7 +21,7 @@
 var ARCHIVE_TYPES = []string{ARCHIVE_FLAT, ARCHIVE_YEAR, ARCHIVE_MONTH}
 
 type Archive struct {
-	Type string `opt:"type" action:"ParseArchiveType" metavar:"flat|year|month"`
+	Type string `opt:"type" action:"ParseArchiveType" metavar:"flat|year|month" complete:"CompleteType"`
 }
 
 func (a *Archive) ParseArchiveType(arg string) error {
@@ -42,9 +42,8 @@ func (Archive) Aliases() []string {
 	return []string{"archive"}
 }
 
-func (Archive) Complete(args []string) []string {
-	valid := []string{"flat", "year", "month"}
-	return commands.CompletionFromList(valid, args)
+func (*Archive) CompleteType(arg string) []string {
+	return commands.CompletionFromList(ARCHIVE_TYPES, arg)
 }
 
 func (a Archive) Execute(args []string) error {
diff --git a/commands/msg/copy.go b/commands/msg/copy.go
index 4109ef99fc90229ae838dcdd35afa1462e311603..77c9ade1f8fccdde14ea2dc8d2c81932101f3997 100644
--- a/commands/msg/copy.go
+++ b/commands/msg/copy.go
@@ -10,7 +10,7 @@ )
 
 type Copy struct {
 	CreateFolders bool   `opt:"-p"`
-	Folder        string `opt:"..." metavar:"<folder>"`
+	Folder        string `opt:"folder" complete:"CompleteFolder"`
 }
 
 func init() {
@@ -21,8 +21,8 @@ func (Copy) Aliases() []string {
 	return []string{"cp", "copy"}
 }
 
-func (Copy) Complete(args []string) []string {
-	return commands.GetFolders(args)
+func (*Copy) CompleteFolder(arg string) []string {
+	return commands.GetFolders(arg)
 }
 
 func (c Copy) Execute(args []string) error {
diff --git a/commands/msg/delete.go b/commands/msg/delete.go
index 49463abc76a39eb6080cc2880449a4018ff64297..1a1a130ca8f317ed0e84559c94693e3407bf6622 100644
--- a/commands/msg/delete.go
+++ b/commands/msg/delete.go
@@ -21,10 +21,6 @@ func (Delete) Aliases() []string {
 	return []string{"delete", "delete-message"}
 }
 
-func (Delete) Complete(args []string) []string {
-	return nil
-}
-
 func (Delete) Execute(args []string) error {
 	h := newHelper()
 	store, err := h.store()
diff --git a/commands/msg/envelope.go b/commands/msg/envelope.go
index 6da82a1e8feae44f3d76dd50d829ba76127a6381..e104648dfeba9526f4fee629adb023099f7ceaad 100644
--- a/commands/msg/envelope.go
+++ b/commands/msg/envelope.go
@@ -25,10 +25,6 @@ func (Envelope) Aliases() []string {
 	return []string{"envelope"}
 }
 
-func (Envelope) Complete(args []string) []string {
-	return nil
-}
-
 func (e Envelope) Execute(args []string) error {
 	acct := app.SelectedAccount()
 	if acct == nil {
diff --git a/commands/msg/fold.go b/commands/msg/fold.go
index 0621c8c39dffa83c74a750642e5021a8999228ee..06e933d7bde26d76236f5be1c746807f22b14cf9 100644
--- a/commands/msg/fold.go
+++ b/commands/msg/fold.go
@@ -17,10 +17,6 @@ func (Fold) Aliases() []string {
 	return []string{"fold", "unfold"}
 }
 
-func (Fold) Complete(args []string) []string {
-	return nil
-}
-
 func (Fold) Execute(args []string) error {
 	h := newHelper()
 	store, err := h.store()
diff --git a/commands/msg/forward.go b/commands/msg/forward.go
index e6e386a9138e188d0473eaf72ca9e57de9738383..4147e8c77a348a2644eca690b7388683ed507dcb 100644
--- a/commands/msg/forward.go
+++ b/commands/msg/forward.go
@@ -13,6 +13,7 @@ 	"strings"
 	"sync"
 
 	"git.sr.ht/~rjarry/aerc/app"
+	"git.sr.ht/~rjarry/aerc/commands"
 	"git.sr.ht/~rjarry/aerc/config"
 	"git.sr.ht/~rjarry/aerc/lib"
 	"git.sr.ht/~rjarry/aerc/lib/format"
@@ -27,7 +28,7 @@ 	AttachAll  bool     `opt:"-A"`
 	AttachFull bool     `opt:"-F"`
 	Edit       bool     `opt:"-e"`
 	NoEdit     bool     `opt:"-E"`
-	Template   string   `opt:"-T"`
+	Template   string   `opt:"-T" complete:"CompleteTemplate"`
 	To         []string `opt:"..." required:"false"`
 }
 
@@ -39,8 +40,8 @@ func (forward) Aliases() []string {
 	return []string{"forward"}
 }
 
-func (forward) Complete(args []string) []string {
-	return nil
+func (*forward) CompleteTemplate(arg string) []string {
+	return commands.GetTemplates(arg)
 }
 
 func (f forward) Execute(args []string) error {
diff --git a/commands/msg/invite.go b/commands/msg/invite.go
index 5b8558b0364d9360959771559b8804d658f7985a..12bea93ee40d54d0a1aa4f6d72800429238b3f78 100644
--- a/commands/msg/invite.go
+++ b/commands/msg/invite.go
@@ -28,10 +28,6 @@ func (invite) Aliases() []string {
 	return []string{"accept", "accept-tentative", "decline"}
 }
 
-func (invite) Complete(args []string) []string {
-	return nil
-}
-
 func (i invite) Execute(args []string) error {
 	acct := app.SelectedAccount()
 	if acct == nil {
diff --git a/commands/msg/mark.go b/commands/msg/mark.go
index c2c21cf2ed09e96311f1ec5e6bfb9547b481be53..9547548f3a9758d45cfe8597724f7f5071013d3b 100644
--- a/commands/msg/mark.go
+++ b/commands/msg/mark.go
@@ -20,10 +20,6 @@ func (Mark) Aliases() []string {
 	return []string{"mark", "unmark", "remark"}
 }
 
-func (Mark) Complete(args []string) []string {
-	return nil
-}
-
 func (m Mark) Execute(args []string) error {
 	h := newHelper()
 	OnSelectedMessage := func(fn func(uint32)) error {
diff --git a/commands/msg/modify-labels.go b/commands/msg/modify-labels.go
index 6fdbeac495a459e39f4625c2fbfdc423f8118ed6..8cc72f2aa68b1a15d8442f014ee7850d6019edf8 100644
--- a/commands/msg/modify-labels.go
+++ b/commands/msg/modify-labels.go
@@ -9,7 +9,7 @@ 	"git.sr.ht/~rjarry/aerc/worker/types"
 )
 
 type ModifyLabels struct {
-	Labels []string `opt:"..." metavar:"[+-]<label>"`
+	Labels []string `opt:"..." metavar:"[+-]<label>" complete:"CompleteLabels"`
 }
 
 func init() {
@@ -20,8 +20,8 @@ func (ModifyLabels) Aliases() []string {
 	return []string{"modify-labels", "tag"}
 }
 
-func (ModifyLabels) Complete(args []string) []string {
-	return commands.GetLabels(args)
+func (*ModifyLabels) CompleteLabels(arg string) []string {
+	return commands.GetLabels(arg)
 }
 
 func (m ModifyLabels) Execute(args []string) error {
diff --git a/commands/msg/move.go b/commands/msg/move.go
index 1dd68d351ffc4e6efb933ba098c8397d9ea92643..06c7fc15b5cead4c70292a2f6e7f5c226431c569 100644
--- a/commands/msg/move.go
+++ b/commands/msg/move.go
@@ -14,7 +14,7 @@ )
 
 type Move struct {
 	CreateFolders bool   `opt:"-p"`
-	Folder        string `opt:"..." metavar:"<folder>"`
+	Folder        string `opt:"folder" complete:"CompleteFolder"`
 }
 
 func init() {
@@ -25,8 +25,8 @@ func (Move) Aliases() []string {
 	return []string{"mv", "move"}
 }
 
-func (Move) Complete(args []string) []string {
-	return commands.GetFolders(args)
+func (*Move) CompleteFolder(arg string) []string {
+	return commands.GetFolders(arg)
 }
 
 func (m Move) Execute(args []string) error {
diff --git a/commands/msg/pipe.go b/commands/msg/pipe.go
index b984752bafa2122012e9f121e92e198428b38055..b32d715945ea718d75c5182f46e24401f1dfc029 100644
--- a/commands/msg/pipe.go
+++ b/commands/msg/pipe.go
@@ -32,10 +32,6 @@ func (Pipe) Aliases() []string {
 	return []string{"pipe"}
 }
 
-func (Pipe) Complete(args []string) []string {
-	return nil
-}
-
 func (p Pipe) Execute(args []string) error {
 	if p.Full && p.Part {
 		return errors.New("-m and -p are mutually exclusive")
diff --git a/commands/msg/read.go b/commands/msg/read.go
index e55ed00e64ac52a069ea356c7c5a9e9eb7b4b11f..72159a53fb929fbb8640609795852f9c3eb145f1 100644
--- a/commands/msg/read.go
+++ b/commands/msg/read.go
@@ -6,6 +6,7 @@ 	"strings"
 	"time"
 
 	"git.sr.ht/~rjarry/aerc/app"
+	"git.sr.ht/~rjarry/aerc/commands"
 	"git.sr.ht/~rjarry/aerc/models"
 	"git.sr.ht/~rjarry/aerc/worker/types"
 )
@@ -13,7 +14,7 @@
 type FlagMsg struct {
 	Toggle   bool         `opt:"-t"`
 	Answered bool         `opt:"-a" aliases:"flag,unflag"`
-	Flag     models.Flags `opt:"-x" aliases:"flag,unflag" action:"ParseFlag"`
+	Flag     models.Flags `opt:"-x" aliases:"flag,unflag" action:"ParseFlag" complete:"CompleteFlag"`
 	FlagName string
 }
 
@@ -23,10 +24,6 @@ }
 
 func (FlagMsg) Aliases() []string {
 	return []string{"flag", "unflag", "read", "unread"}
-}
-
-func (FlagMsg) Complete(args []string) []string {
-	return nil
 }
 
 func (f *FlagMsg) ParseFlag(arg string) error {
@@ -44,6 +41,12 @@ 	default:
 		return fmt.Errorf("Unknown flag %q", arg)
 	}
 	return nil
+}
+
+var validFlags = []string{"seen", "answered", "flagged"}
+
+func (*FlagMsg) CompleteFlag(arg string) []string {
+	return commands.CompletionFromList(validFlags, arg)
 }
 
 // If this was called as 'flag' or 'unflag', without the toggle (-t)
diff --git a/commands/msg/recall.go b/commands/msg/recall.go
index 3b78a763eb06725f12aedad383f7cedae7c75bb2..cea02ddb4df375afed225e3d768296252c5b09de 100644
--- a/commands/msg/recall.go
+++ b/commands/msg/recall.go
@@ -31,10 +31,6 @@ func (Recall) Aliases() []string {
 	return []string{"recall"}
 }
 
-func (Recall) Complete(args []string) []string {
-	return nil
-}
-
 func (r Recall) Execute(args []string) error {
 	editHeaders := (config.Compose.EditHeaders || r.Edit) && !r.NoEdit
 
diff --git a/commands/msg/reply.go b/commands/msg/reply.go
index 2ab9e9f8cf329dbd1446aada215eedcc26d89324..ebf2031728b2012b654829b5cb6b2f0544765895 100644
--- a/commands/msg/reply.go
+++ b/commands/msg/reply.go
@@ -10,6 +10,7 @@ 	"strings"
 	"time"
 
 	"git.sr.ht/~rjarry/aerc/app"
+	"git.sr.ht/~rjarry/aerc/commands"
 	"git.sr.ht/~rjarry/aerc/commands/account"
 	"git.sr.ht/~rjarry/aerc/config"
 	"git.sr.ht/~rjarry/aerc/lib"
@@ -26,7 +27,7 @@ type reply struct {
 	All      bool   `opt:"-a"`
 	Close    bool   `opt:"-c"`
 	Quote    bool   `opt:"-q"`
-	Template string `opt:"-T"`
+	Template string `opt:"-T" complete:"CompleteTemplate"`
 	Edit     bool   `opt:"-e"`
 	NoEdit   bool   `opt:"-E"`
 }
@@ -39,8 +40,8 @@ func (reply) Aliases() []string {
 	return []string{"reply"}
 }
 
-func (reply) Complete(args []string) []string {
-	return nil
+func (*reply) CompleteTemplate(arg string) []string {
+	return commands.GetTemplates(arg)
 }
 
 func (r reply) Execute(args []string) error {
diff --git a/commands/msg/toggle-thread-context.go b/commands/msg/toggle-thread-context.go
index 0ef6778f9d2ebc55814acdfc2ad6f95b1d7d7e09..4b87eaa8c8c4e399cb267a0470878ddf9db5ee11 100644
--- a/commands/msg/toggle-thread-context.go
+++ b/commands/msg/toggle-thread-context.go
@@ -14,10 +14,6 @@ func (ToggleThreadContext) Aliases() []string {
 	return []string{"toggle-thread-context"}
 }
 
-func (ToggleThreadContext) Complete(args []string) []string {
-	return nil
-}
-
 func (ToggleThreadContext) Execute(args []string) error {
 	h := newHelper()
 	store, err := h.store()
diff --git a/commands/msg/toggle-threads.go b/commands/msg/toggle-threads.go
index 88cc763fb266966761663dad4654345f8b9eda47..d2933bba83ba8dc0e2c93d1c297dbe41f4927c07 100644
--- a/commands/msg/toggle-threads.go
+++ b/commands/msg/toggle-threads.go
@@ -15,10 +15,6 @@ func (ToggleThreads) Aliases() []string {
 	return []string{"toggle-threads"}
 }
 
-func (ToggleThreads) Complete(args []string) []string {
-	return nil
-}
-
 func (ToggleThreads) Execute(args []string) error {
 	h := newHelper()
 	acct, err := h.account()
diff --git a/commands/msg/unsubscribe.go b/commands/msg/unsubscribe.go
index 6fef16f5b142b23c8cabace01bd259b5b237237b..c6a0b23f89ed5e6e436d3f2203071c8e6287feb7 100644
--- a/commands/msg/unsubscribe.go
+++ b/commands/msg/unsubscribe.go
@@ -31,11 +31,6 @@ func (Unsubscribe) Aliases() []string {
 	return []string{"unsubscribe"}
 }
 
-// Complete returns a list of completions
-func (Unsubscribe) Complete(args []string) []string {
-	return nil
-}
-
 // Execute runs the Unsubscribe command
 func (u Unsubscribe) Execute(args []string) error {
 	editHeaders := (config.Compose.EditHeaders || u.Edit) && !u.NoEdit
diff --git a/commands/msgview/close.go b/commands/msgview/close.go
index 32702da9f8b22482583fe37ef71037eaf6ad41ad..95960ef3ce4657291aa18f33e4c1c2ecef917597 100644
--- a/commands/msgview/close.go
+++ b/commands/msgview/close.go
@@ -14,10 +14,6 @@ func (Close) Aliases() []string {
 	return []string{"close"}
 }
 
-func (Close) Complete(args []string) []string {
-	return nil
-}
-
 func (Close) Execute(args []string) error {
 	mv, _ := app.SelectedTabContent().(*app.MessageViewer)
 	app.RemoveTab(mv, true)
diff --git a/commands/msgview/next-part.go b/commands/msgview/next-part.go
index 9b1b1fcc50b9f3908bb29c65f7fcce8565c43d0d..77eb008d33449fa1137807412b6360dad391f8be 100644
--- a/commands/msgview/next-part.go
+++ b/commands/msgview/next-part.go
@@ -16,10 +16,6 @@ func (NextPrevPart) Aliases() []string {
 	return []string{"next-part", "prev-part"}
 }
 
-func (NextPrevPart) Complete(args []string) []string {
-	return nil
-}
-
 func (np NextPrevPart) Execute(args []string) error {
 	mv, _ := app.SelectedTabContent().(*app.MessageViewer)
 	for n := 0; n < np.Offset; n++ {
diff --git a/commands/msgview/next.go b/commands/msgview/next.go
index d8f046f82e67aae346ac3e93b0edc73da8a17ac9..c953cd5d51329f4b2f3c6a3bf51f1971f8c27ad2 100644
--- a/commands/msgview/next.go
+++ b/commands/msgview/next.go
@@ -39,10 +39,6 @@ func (NextPrevMsg) Aliases() []string {
 	return []string{"next", "next-message", "prev", "prev-message"}
 }
 
-func (NextPrevMsg) Complete(args []string) []string {
-	return nil
-}
-
 func (np NextPrevMsg) Execute(args []string) error {
 	cmd := account.NextPrevMsg{Amount: np.Amount, Percent: np.Percent}
 	err := cmd.Execute(args)
diff --git a/commands/msgview/open-link.go b/commands/msgview/open-link.go
index 55aee08c1a3ccae67fa2873264d79708b086fb6f..eceb42321bf17ad238cfc124e597d8c8caebc93b 100644
--- a/commands/msgview/open-link.go
+++ b/commands/msgview/open-link.go
@@ -11,7 +11,7 @@ 	"git.sr.ht/~rjarry/aerc/log"
 )
 
 type OpenLink struct {
-	Url *url.URL `opt:"url" action:"ParseUrl"`
+	Url *url.URL `opt:"url" action:"ParseUrl" complete:"CompleteUrl"`
 	Cmd string   `opt:"..." required:"false"`
 }
 
@@ -23,11 +23,11 @@ func (OpenLink) Aliases() []string {
 	return []string{"open-link"}
 }
 
-func (OpenLink) Complete(args []string) []string {
+func (*OpenLink) CompleteUrl(arg string) []string {
 	mv := app.SelectedTabContent().(*app.MessageViewer)
 	if mv != nil {
 		if p := mv.SelectedMessagePart(); p != nil {
-			return commands.CompletionFromList(p.Links, args)
+			return commands.CompletionFromList(p.Links, arg)
 		}
 	}
 	return nil
diff --git a/commands/msgview/open.go b/commands/msgview/open.go
index 0d6db5b0142a3b751f3f796fe213873efee2a70d..a84434049cc53a6e562737052d04f987a6dd7a46 100644
--- a/commands/msgview/open.go
+++ b/commands/msgview/open.go
@@ -29,10 +29,6 @@ func (Open) Aliases() []string {
 	return []string{"open"}
 }
 
-func (Open) Complete(args []string) []string {
-	return nil
-}
-
 func (o Open) Execute(args []string) error {
 	mv := app.SelectedTabContent().(*app.MessageViewer)
 	if mv == nil {
diff --git a/commands/msgview/save.go b/commands/msgview/save.go
index ea7599d31d7fe8ac5f18de8b7a24169197aefe40..9f2b71fc19f254fd7219a93843990f9a5b77359c 100644
--- a/commands/msgview/save.go
+++ b/commands/msgview/save.go
@@ -22,7 +22,7 @@ 	Force          bool   `opt:"-f"`
 	CreateDirs     bool   `opt:"-p"`
 	Attachments    bool   `opt:"-a"`
 	AllAttachments bool   `opt:"-A"`
-	Path           string `opt:"..." required:"false" metavar:"<path>"`
+	Path           string `opt:"path" required:"false" complete:"CompletePath"`
 }
 
 func init() {
@@ -37,14 +37,12 @@ func (Save) Aliases() []string {
 	return []string{"save"}
 }
 
-func (s Save) Complete(args []string) []string {
-	trimmed := commands.Operands(args, s.Options())
-	path := strings.Join(trimmed, " ")
+func (*Save) CompletePath(arg string) []string {
 	defaultPath := config.General.DefaultSavePath
-	if defaultPath != "" && !isAbsPath(path) {
-		path = filepath.Join(defaultPath, path)
+	if defaultPath != "" && !isAbsPath(arg) {
+		arg = filepath.Join(defaultPath, arg)
 	}
-	return commands.CompletePath(xdg.ExpandHome(path))
+	return commands.CompletePath(xdg.ExpandHome(arg))
 }
 
 func (s Save) Execute(args []string) error {
diff --git a/commands/msgview/toggle-headers.go b/commands/msgview/toggle-headers.go
index c27307d3944e9884fd79bffa3a2a5f3584fd9c36..37f5b22ff91568c6940304465c9faca9e7eebeaa 100644
--- a/commands/msgview/toggle-headers.go
+++ b/commands/msgview/toggle-headers.go
@@ -14,10 +14,6 @@ func (ToggleHeaders) Aliases() []string {
 	return []string{"toggle-headers"}
 }
 
-func (ToggleHeaders) Complete(args []string) []string {
-	return nil
-}
-
 func (ToggleHeaders) Execute(args []string) error {
 	mv, _ := app.SelectedTabContent().(*app.MessageViewer)
 	mv.ToggleHeaders()
diff --git a/commands/msgview/toggle-key-passthrough.go b/commands/msgview/toggle-key-passthrough.go
index 327358701dfa978782a7ef403ed9aa80db2cbf57..b4c5f2c7242613b416e2e05909ce043090b366c7 100644
--- a/commands/msgview/toggle-key-passthrough.go
+++ b/commands/msgview/toggle-key-passthrough.go
@@ -15,10 +15,6 @@ func (ToggleKeyPassthrough) Aliases() []string {
 	return []string{"toggle-key-passthrough"}
 }
 
-func (ToggleKeyPassthrough) Complete(args []string) []string {
-	return nil
-}
-
 func (ToggleKeyPassthrough) Execute(args []string) error {
 	mv, _ := app.SelectedTabContent().(*app.MessageViewer)
 	keyPassthroughEnabled := mv.ToggleKeyPassthrough()
diff --git a/commands/new-account.go b/commands/new-account.go
index b30e0e34480e91985ae4eb3a3834da6fb774eeab..1a14a821098a224b0f18d188020dbb38df3ff95b 100644
--- a/commands/new-account.go
+++ b/commands/new-account.go
@@ -16,10 +16,6 @@ func (NewAccount) Aliases() []string {
 	return []string{"new-account"}
 }
 
-func (NewAccount) Complete(args []string) []string {
-	return nil
-}
-
 func (n NewAccount) Execute(args []string) error {
 	wizard := app.NewAccountWizard()
 	wizard.ConfigureTemporaryAccount(n.Temp)
diff --git a/commands/next-tab.go b/commands/next-tab.go
index d837419185c460f1b4710b1d85b30a9c69a775ed..f1f34d223f9f5e82b246f3d9235e9892eb05efcc 100644
--- a/commands/next-tab.go
+++ b/commands/next-tab.go
@@ -16,10 +16,6 @@ func (NextPrevTab) Aliases() []string {
 	return []string{"next-tab", "prev-tab"}
 }
 
-func (NextPrevTab) Complete(args []string) []string {
-	return nil
-}
-
 func (np NextPrevTab) Execute(args []string) error {
 	for n := 0; n < np.Offset; n++ {
 		if args[0] == "prev-tab" {
diff --git a/commands/parser.go b/commands/parser.go
deleted file mode 100644
index e8146506f02f51c79fe00c5c2b8fc121a53c79d8..0000000000000000000000000000000000000000
--- a/commands/parser.go
+++ /dev/null
@@ -1,134 +0,0 @@
-package commands
-
-import (
-	"strings"
-)
-
-type completionType int
-
-const (
-	NONE completionType = iota
-	COMMAND
-	OPERAND
-	SHORT_OPTION
-	OPTION_ARGUMENT
-)
-
-type parser struct {
-	tokens []string
-	optind int
-	spec   string
-	space  bool
-	kind   completionType
-	flag   string
-	arg    string
-	err    error
-}
-
-func newParser(cmd, spec string, spaceTerminated bool) (*parser, error) {
-	args, err := splitCmd(cmd)
-	if err != nil {
-		return nil, err
-	}
-
-	p := &parser{
-		tokens: args,
-		optind: 0,
-		spec:   spec,
-		space:  spaceTerminated,
-		kind:   NONE,
-		flag:   "",
-		arg:    "",
-		err:    nil,
-	}
-
-	state := command
-	for state != nil {
-		state = state(p)
-	}
-
-	return p, p.err
-}
-
-func (p *parser) empty() bool {
-	return len(p.tokens) == 0
-}
-
-func (p *parser) peek() string {
-	return p.tokens[0]
-}
-
-func (p *parser) advance() string {
-	if p.empty() {
-		return ""
-	}
-	tok := p.tokens[0]
-	p.tokens = p.tokens[1:]
-	p.optind++
-	return tok
-}
-
-func (p *parser) set(t completionType) {
-	p.kind = t
-}
-
-func (p *parser) hasArgument() bool {
-	n := len(p.flag)
-	if n > 0 {
-		s := string(p.flag[n-1]) + ":"
-		return strings.Contains(p.spec, s)
-	}
-	return false
-}
-
-type stateFn func(*parser) stateFn
-
-func command(p *parser) stateFn {
-	p.set(COMMAND)
-	p.advance()
-	return peek(p)
-}
-
-func peek(p *parser) stateFn {
-	if p.empty() {
-		if p.space {
-			return operand
-		}
-		return nil
-	}
-	if p.spec == "" {
-		return operand
-	}
-	s := p.peek()
-	switch {
-	case s == "--":
-		p.advance()
-	case strings.HasPrefix(s, "-"):
-		return short_option
-	}
-	return operand
-}
-
-func short_option(p *parser) stateFn {
-	p.set(SHORT_OPTION)
-	tok := p.advance()
-	p.flag = tok[1:]
-	if p.hasArgument() {
-		return option_argument
-	}
-	return peek(p)
-}
-
-func option_argument(p *parser) stateFn {
-	p.set(OPTION_ARGUMENT)
-	p.arg = p.advance()
-	if p.empty() && len(p.arg) == 0 {
-		return nil
-	}
-	return peek(p)
-}
-
-func operand(p *parser) stateFn {
-	p.set(OPERAND)
-	return nil
-}
diff --git a/commands/parser_test.go b/commands/parser_test.go
deleted file mode 100644
index d6ccd38560a844d4c96322d1f04009e15dd26f4a..0000000000000000000000000000000000000000
--- a/commands/parser_test.go
+++ /dev/null
@@ -1,258 +0,0 @@
-package commands
-
-import (
-	"testing"
-)
-
-var parserTests = []struct {
-	name       string
-	cmd        string
-	wantType   completionType
-	wantFlag   string
-	wantArg    string
-	wantOptind int
-}{
-	{
-		name:       "empty command",
-		cmd:        "",
-		wantType:   COMMAND,
-		wantFlag:   "",
-		wantArg:    "",
-		wantOptind: 0,
-	},
-	{
-		name:       "command only",
-		cmd:        "cmd",
-		wantType:   COMMAND,
-		wantFlag:   "",
-		wantArg:    "",
-		wantOptind: 1,
-	},
-	{
-		name:       "with space",
-		cmd:        "cmd ",
-		wantType:   OPERAND,
-		wantFlag:   "",
-		wantArg:    "",
-		wantOptind: 1,
-	},
-	{
-		name:       "with two spaces",
-		cmd:        "cmd  ",
-		wantType:   OPERAND,
-		wantFlag:   "",
-		wantArg:    "",
-		wantOptind: 1,
-	},
-	{
-		name:       "with single option flag",
-		cmd:        "cmd -",
-		wantType:   SHORT_OPTION,
-		wantFlag:   "",
-		wantArg:    "",
-		wantOptind: 2,
-	},
-	{
-		name:       "with single option flag two spaces",
-		cmd:        "cmd  -",
-		wantType:   SHORT_OPTION,
-		wantFlag:   "",
-		wantArg:    "",
-		wantOptind: 2,
-	},
-	{
-		name:       "with single option flag completed",
-		cmd:        "cmd -a",
-		wantType:   SHORT_OPTION,
-		wantFlag:   "a",
-		wantArg:    "",
-		wantOptind: 2,
-	},
-	{
-		name:       "with single option flag completed and space",
-		cmd:        "cmd -a ",
-		wantType:   OPERAND,
-		wantFlag:   "a",
-		wantArg:    "",
-		wantOptind: 2,
-	},
-	{
-		name:       "with single option flag completed and two spaces",
-		cmd:        "cmd -a ",
-		wantType:   OPERAND,
-		wantFlag:   "a",
-		wantArg:    "",
-		wantOptind: 2,
-	},
-	{
-		name:       "with two single option flag completed",
-		cmd:        "cmd -b -a",
-		wantType:   SHORT_OPTION,
-		wantFlag:   "a",
-		wantArg:    "",
-		wantOptind: 3,
-	},
-	{
-		name:       "with two single option flag combined",
-		cmd:        "cmd -ab",
-		wantType:   SHORT_OPTION,
-		wantFlag:   "ab",
-		wantArg:    "",
-		wantOptind: 2,
-	},
-	{
-		name:       "with two single option flag and space",
-		cmd:        "cmd -ab ",
-		wantType:   OPERAND,
-		wantFlag:   "ab",
-		wantArg:    "",
-		wantOptind: 2,
-	},
-	{
-		name:       "with mandatory option flag",
-		cmd:        "cmd -f",
-		wantType:   OPTION_ARGUMENT,
-		wantFlag:   "f",
-		wantArg:    "",
-		wantOptind: 2,
-	},
-	{
-		name:       "with mandatory option flag and space",
-		cmd:        "cmd -f ",
-		wantType:   OPTION_ARGUMENT,
-		wantFlag:   "f",
-		wantArg:    "",
-		wantOptind: 2,
-	},
-	{
-		name:       "with mandatory option flag and two spaces",
-		cmd:        "cmd -f  ",
-		wantType:   OPTION_ARGUMENT,
-		wantFlag:   "f",
-		wantArg:    "",
-		wantOptind: 2,
-	},
-	{
-		name:       "with mandatory option flag and completed",
-		cmd:        "cmd -f a",
-		wantType:   OPTION_ARGUMENT,
-		wantFlag:   "f",
-		wantArg:    "a",
-		wantOptind: 3,
-	},
-	{
-		name:       "with mandatory option flag and completed quote",
-		cmd:        "cmd -f 'a b'",
-		wantType:   OPTION_ARGUMENT,
-		wantFlag:   "f",
-		wantArg:    "a b",
-		wantOptind: 3,
-	},
-	{
-		name:       "with mandatory option flag and operand",
-		cmd:        "cmd -f 'a b' hello",
-		wantType:   OPERAND,
-		wantFlag:   "f",
-		wantArg:    "a b",
-		wantOptind: 3,
-	},
-	{
-		name:       "with mandatory option flag and two spaces between",
-		cmd:        "cmd -f  a",
-		wantType:   OPTION_ARGUMENT,
-		wantFlag:   "f",
-		wantArg:    "a",
-		wantOptind: 3,
-	},
-	{
-		name:       "with mandatory option flag and more spaces",
-		cmd:        "cmd -f  a ",
-		wantType:   OPERAND,
-		wantFlag:   "f",
-		wantArg:    "a",
-		wantOptind: 3,
-	},
-	{
-		name:       "with template data",
-		cmd:        "cmd -a {{if .Size}}  hello {{else}} {{end}}",
-		wantType:   OPERAND,
-		wantFlag:   "a",
-		wantArg:    "",
-		wantOptind: 2,
-	},
-	{
-		name:       "with operand",
-		cmd:        "cmd -ab /tmp/aerc-",
-		wantType:   OPERAND,
-		wantFlag:   "ab",
-		wantArg:    "",
-		wantOptind: 2,
-	},
-	{
-		name:       "with operand indicator",
-		cmd:        "cmd -ab -- /tmp/aerc-",
-		wantType:   OPERAND,
-		wantFlag:   "ab",
-		wantArg:    "",
-		wantOptind: 3,
-	},
-	{
-		name:       "hyphen connected command",
-		cmd:        "cmd-dmc",
-		wantType:   COMMAND,
-		wantFlag:   "",
-		wantArg:    "",
-		wantOptind: 1,
-	},
-	{
-		name:       "incomplete hyphen connected command",
-		cmd:        "cmd-",
-		wantType:   COMMAND,
-		wantFlag:   "",
-		wantArg:    "",
-		wantOptind: 1,
-	},
-	{
-		name:       "hyphen connected command with option",
-		cmd:        "cmd-dmc -a",
-		wantType:   SHORT_OPTION,
-		wantFlag:   "a",
-		wantArg:    "",
-		wantOptind: 2,
-	},
-}
-
-func TestCommands_Parser(t *testing.T) {
-	for i, test := range parserTests {
-		n := len(test.cmd)
-		spaceTerminated := n > 0 && test.cmd[n-1] == ' '
-		parser, err := newParser(test.cmd, "abf:", spaceTerminated)
-		if err != nil {
-			t.Errorf("parser error: %v", err)
-		}
-
-		if test.wantType != parser.kind {
-			t.Errorf("test %d '%s': completion type does not match: "+
-				"want %d, but got %d", i, test.cmd, test.wantType,
-				parser.kind)
-		}
-
-		if test.wantFlag != parser.flag {
-			t.Errorf("test %d '%s': flag does not match: "+
-				"want %s, but got %s", i, test.cmd, test.wantFlag,
-				parser.flag)
-		}
-
-		if test.wantArg != parser.arg {
-			t.Errorf("test %d '%s': arg does not match: "+
-				"want %s, but got %s", i, test.cmd, test.wantArg,
-				parser.arg)
-		}
-
-		if test.wantOptind != parser.optind {
-			t.Errorf("test %d '%s': optind does not match: "+
-				"want %d, but got %d", i, test.cmd, test.wantOptind,
-				parser.optind)
-		}
-	}
-}
diff --git a/commands/pin-tab.go b/commands/pin-tab.go
index 276442cee26dfdeb3c4c36371a9c024b19fc6851..d5013bf67ed41174753490f51b64e691ed9346ef 100644
--- a/commands/pin-tab.go
+++ b/commands/pin-tab.go
@@ -14,10 +14,6 @@ func (PinTab) Aliases() []string {
 	return []string{"pin-tab", "unpin-tab"}
 }
 
-func (PinTab) Complete(args []string) []string {
-	return nil
-}
-
 func (PinTab) Execute(args []string) error {
 	switch args[0] {
 	case "pin-tab":
diff --git a/commands/prompt.go b/commands/prompt.go
index dd259c303c2dc8a44297ff2f78f20f0c91a8723c..d791f7a9f1c86045ce1eb64fbc94b1f8b57d23cf 100644
--- a/commands/prompt.go
+++ b/commands/prompt.go
@@ -1,8 +1,6 @@
 package commands
 
 import (
-	"strings"
-
 	"git.sr.ht/~rjarry/go-opt"
 
 	"git.sr.ht/~rjarry/aerc/app"
@@ -10,7 +8,7 @@ )
 
 type Prompt struct {
 	Text string   `opt:"text"`
-	Cmd  []string `opt:"..."`
+	Cmd  []string `opt:"..." complete:"CompleteCommand"`
 }
 
 func init() {
@@ -21,56 +19,8 @@ func (Prompt) Aliases() []string {
 	return []string{"prompt"}
 }
 
-func (Prompt) Complete(args []string) []string {
-	argc := len(args)
-	if argc == 0 {
-		return nil
-	}
-	hascommand := argc > 2
-	if argc == 1 {
-		args = append(args, "")
-	}
-
-	cmd := GlobalCommands.ByName(args[1])
-	var cs []string
-	if cmd != nil {
-		cs = cmd.Complete(args[2:])
-		hascommand = true
-	} else {
-		if hascommand {
-			return nil
-		}
-		cs = GlobalCommands.Names()
-	}
-	if cs == nil {
-		return nil
-	}
-
-	var b strings.Builder
-	// it seems '' quoting is enough
-	// to keep quoted arguments in one piece
-	b.WriteRune('\'')
-	b.WriteString(args[0])
-	b.WriteRune('\'')
-	b.WriteRune(' ')
-	if hascommand {
-		b.WriteString(args[1])
-		b.WriteRune(' ')
-	}
-
-	src := b.String()
-	b.Reset()
-
-	rs := make([]string, 0, len(cs))
-	for _, c := range cs {
-		b.WriteString(src)
-		b.WriteString(c)
-
-		rs = append(rs, b.String())
-		b.Reset()
-	}
-
-	return rs
+func (*Prompt) CompleteCommand(arg string) []string {
+	return CompletionFromList(GlobalCommands.Names(), arg)
 }
 
 func (p Prompt) Execute(args []string) error {
diff --git a/commands/pwd.go b/commands/pwd.go
index 426be78b030d0b5a7fb9f6ea9707b2816b9b62f2..2b1961a1c72a9e490c8203d0f3f43dd3a622dd7f 100644
--- a/commands/pwd.go
+++ b/commands/pwd.go
@@ -17,10 +17,6 @@ func (PrintWorkDir) Aliases() []string {
 	return []string{"pwd"}
 }
 
-func (PrintWorkDir) Complete(args []string) []string {
-	return nil
-}
-
 func (PrintWorkDir) Execute(args []string) error {
 	pwd, err := os.Getwd()
 	if err != nil {
diff --git a/commands/quit.go b/commands/quit.go
index 3e0f2968ce46914d71c2fbb952ef6d3889c847aa..e96c92536f60402536901a566f0462fbe825e9ff 100644
--- a/commands/quit.go
+++ b/commands/quit.go
@@ -18,10 +18,6 @@ func (Quit) Aliases() []string {
 	return []string{"quit", "exit"}
 }
 
-func (Quit) Complete(args []string) []string {
-	return nil
-}
-
 type ErrorExit int
 
 func (err ErrorExit) Error() string {
diff --git a/commands/send-keys.go b/commands/send-keys.go
index 9541b88defc630d4f0b0ab2e8e73fa7ea25b8ac6..bb872a3342ad1af5ce754f7b41951f7c1d6eca9a 100644
--- a/commands/send-keys.go
+++ b/commands/send-keys.go
@@ -19,10 +19,6 @@ func (SendKeys) Aliases() []string {
 	return []string{"send-keys"}
 }
 
-func (SendKeys) Complete(args []string) []string {
-	return nil
-}
-
 func (s SendKeys) Execute(args []string) error {
 	tab, ok := app.SelectedTabContent().(app.HasTerminal)
 	if !ok {
diff --git a/commands/suspend.go b/commands/suspend.go
index 80d59028b2d0436290761265cc587f84cc775dd1..a8996441d2374af008fa6e662f41d79820e897f7 100644
--- a/commands/suspend.go
+++ b/commands/suspend.go
@@ -12,10 +12,6 @@ func (Suspend) Aliases() []string {
 	return []string{"suspend"}
 }
 
-func (Suspend) Complete(args []string) []string {
-	return nil
-}
-
 func (Suspend) Execute(args []string) error {
 	ui.QueueSuspend()
 	return nil
diff --git a/commands/term.go b/commands/term.go
index 225fee5788de6382db5c9f7a01f0e7022160dcb6..3f8f987500a9c61da3544c5114d8b0f16d431d86 100644
--- a/commands/term.go
+++ b/commands/term.go
@@ -21,10 +21,6 @@ func (Term) Aliases() []string {
 	return []string{"terminal", "term"}
 }
 
-func (Term) Complete(args []string) []string {
-	return nil
-}
-
 func (t Term) Execute(args []string) error {
 	if len(t.Cmd) == 0 {
 		shell, err := loginshell.Shell()
diff --git a/commands/terminal/close.go b/commands/terminal/close.go
index 913a33875ae045f831dd8372b0a46b9bcc901646..610a846381024d559a0f9bd8188cc3087555e44a 100644
--- a/commands/terminal/close.go
+++ b/commands/terminal/close.go
@@ -14,10 +14,6 @@ func (Close) Aliases() []string {
 	return []string{"close"}
 }
 
-func (Close) Complete(args []string) []string {
-	return nil
-}
-
 func (Close) Execute(args []string) error {
 	term, _ := app.SelectedTabContent().(*app.Terminal)
 	term.Close()
diff --git a/commands/util.go b/commands/util.go
index e9a377a67abcea7bfe564776d0898be2fd35e288..a9aa81b3442a63af9270ead1e4409e1930199046 100644
--- a/commands/util.go
+++ b/commands/util.go
@@ -18,6 +18,7 @@ 	"git.sr.ht/~rjarry/aerc/lib/xdg"
 	"git.sr.ht/~rjarry/aerc/log"
 	"git.sr.ht/~rjarry/aerc/models"
 	"git.sr.ht/~rjarry/aerc/worker/types"
+	"git.sr.ht/~rjarry/go-opt"
 	"github.com/gdamore/tcell/v2"
 )
 
@@ -93,7 +94,7 @@ 			return nil
 		}
 
 		if !strings.HasPrefix(path, ".") && !strings.Contains(path, "/.") {
-			log.Debugf("removing hidden files from glob results")
+			log.Tracef("removing hidden files from glob results")
 			for i := len(matches) - 1; i >= 0; i-- {
 				if strings.HasPrefix(filepath.Base(matches[i]), ".") {
 					if i == len(matches)-1 {
@@ -107,11 +108,13 @@ 		}
 
 		for i, m := range matches {
 			if isDir(m) {
-				matches[i] = m + "/"
+				m += "/"
 			}
+			matches[i] = opt.QuoteArg((xdg.TildeHome(m)))
 		}
 
 		sort.Strings(matches)
+
 		return matches
 	}
 
@@ -122,11 +125,11 @@ 		f = filepath.Join(path, f)
 		if isDir(f) {
 			f += "/"
 		}
-
-		files[i] = f
+		files[i] = opt.QuoteArg((xdg.TildeHome(f)))
 	}
 
 	sort.Strings(files)
+
 	return files
 }
 
@@ -227,16 +230,16 @@ }
 
 // FilterList takes a list of valid completions and filters it, either
 // by case smart prefix, or by fuzzy matching, prepending "prefix" to each completion
-func FilterList(valid []string, search, prefix string, isFuzzy bool) []string {
-	out := make([]string, 0)
+func FilterList(valid []string, search, prefix, suffix string, isFuzzy bool) []string {
+	out := make([]string, 0, len(valid))
 	if isFuzzy {
 		for _, v := range fuzzy.RankFindFold(search, valid) {
-			out = append(out, prefix+v.Target)
+			out = append(out, opt.QuoteArg(prefix+v.Target+suffix))
 		}
 	} else {
 		for _, v := range valid {
 			if hasCaseSmartPrefix(v, search) {
-				out = append(out, prefix+v)
+				out = append(out, opt.QuoteArg(prefix+v+suffix))
 			}
 		}
 	}
diff --git a/commands/z.go b/commands/z.go
index 5aee2a2ccba1a00b494132d22002bb47b164eda6..2ea65e0cdf80cdea7698b2b7192959ad59bc88d5 100644
--- a/commands/z.go
+++ b/commands/z.go
@@ -8,7 +8,8 @@ 	"strings"
 )
 
 type Zoxide struct {
-	Target string `opt:"..." default:"~" metavar:"<folder> | <query>..."`
+	Target string   `opt:"folder" default:"~" complete:"CompleteFolder"`
+	Args   []string `opt:"..." required:"false" metavar:"<query>..."`
 }
 
 func ZoxideAdd(arg string) error {
@@ -36,8 +37,8 @@ func (Zoxide) Aliases() []string {
 	return []string{"z"}
 }
 
-func (Zoxide) Complete(args []