7

I am trying to align these columns

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

I have tried to use awk but no luck there. The preferred format is like

super+Shift+d     mode $mode_launcher     
super+alt+c       ~/bin/editinvim
super+alt+b       ranger
8

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
6

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
...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.