Aligning columns using awk

You could re-print the first column, left-justified in a suitably wide field:

$ awk '{$1 = sprintf("%-30s", $1)} 1' file
super+t                        sticky toggle
super+Shift+space              floating toggle
super+Shift+r                  restart
super+Shift+d                  mode $mode_launcher
super+Shift+c                  reload
super+r                        mode resize
super+Return                   i3-sensible-terminal
super+q                        kill
super+n                        Nautilus scratchpad show
super+m                        neomutt scratchpad show
super+minus                    scratchpad show
super+f                        fullscreen toggle
super+c                        bar mode toggle
super+button2                  kill
super+alt+x                    systemctl -i suspend
super+alt+v                    cmus
super+alt+m                    neomutt
super+alt+c                    ~/bin/editinvim
super+alt+b                    ranger

If you want to choose a suitable width automatically based on the length of column 1, then:

awk '
  NR==FNR {w = length($1) > w ? length($1) : w; next} 
  {$1 = sprintf("%-*s", w+4, $1)} 
  1
' file file

Alternatively, the command column -t can be used to format text in columns.

column -t file

The default column separator is " ". In the example lines have multiple spaces but just the first separates columns: we may replace the first by ":" and use it as separator.

$ sed 's/ /:/1' file | column -s ':' -t
super+t            sticky toggle
super+Shift+space  floating toggle
super+Shift+r      restart
super+Shift+d      mode $mode_launcher
...

Tags:

Awk

Columns