Skip to content

Instantly share code, notes, and snippets.

@robin-a-meade
Last active February 25, 2022 08:13
Show Gist options
  • Save robin-a-meade/fcf76e74033ec84d4d0bb6eddea209b2 to your computer and use it in GitHub Desktop.
Save robin-a-meade/fcf76e74033ec84d4d0bb6eddea209b2 to your computer and use it in GitHub Desktop.

Change column order for exa long listing

Related issue:

Temporary workaround

exa -lg --time-style=long-iso --color=always \
  | sed -E 's/^ *([^ ]*) *([^ ]*) *([^ ]*) *([^ ]*) *([^ ]* *[^ ]*) *(.*)$/\1\t\3\t\4\t\2\t\5\t\6/' \
  | column -t -s $'\t' -o ' '

I had to use --time-style=long-iso because the default timestamp format caused a problem when day of month was a single digit.

Explanation of sed invocation:

-E      Use modern regex syntax (ERE as opposed to BRE)
s/      begin substitution
^       beginning of line
 *      skip zero or more spaces (for good measure)
([^ ]*) capture zero or more non-SPACE characters into group 1: permissions bits
 *      skip zero or more spaces
([^ ]*) capture zero or more non-SPACE characters into group 2: size
 *      skip zero or more spaces
([^ ]*) capture zero or more non-SPACE characters into group 3: user
 *      skip zero or more spaces
([^ ]*) capture zero or more non-SPACE characters into group 4: group
 *      skip zero or more spaces
([^ ]* *[^ ]*) capture timestamp into group 5: timestamp
 *      skip zero or more spaces
(.*)$   capture remainder of line into group 6
/       begin replacement
\1\t\3\t\4\t\2\t\5\t\6  order the captured groups as you wish, separated by TABs
/       end

The column invocation creates a table from the TAB delimited input it is given. The column delimiter in the output table is set to one space.

.bashrc

To use this approach, I suggest the following additions to your ~/.bashrc:

exa_long () {
  exa -lg --time-style=long-iso --color=always --group-directories-first "$@" \
  | sed -E 's/^ *([^ ]*) *([^ ]*) *([^ ]*) *([^ ]*) *([^ ]* *[^ ]*) *(.*)$/\1\t\3\t\4\t\2\t\5\t\6/' \
  | column -t -s $'\t' -o ' '
}
exa_huge () {
  # exa -lgHi  # No -S though; we don't want blocks.
  #            # Add -h if you want want a header row.
  # 1 - inode number
  # 2 - permission bits
  # 3 - number of hard links
  # 4 - size
  # 5 - user
  # 6 - group
  # 7 - timestamp
  # 8 - Remainder
  exa -lgHi --time-style=long-iso --color=always --group-directories-first "$@" \
  | sed -E 's/^ *([^ ]*) *([^ ]*) *([^ ]*) *([^ ]*) *([^ ]*) *([^ ]*) *([^ ]* *[^ ]*) *(.*)$/\1\t\3\t\2\t\5\t\6\t\4\t\7\t\8/' \
  | column -t -s $'\t' -o ' '
}

alias ll='exa_long'        # Long listing
alias ldir='exa_long -D'   # Just directories
alias l.='exa_long -d .*'  # Just dot files and dot directories
alias lh='exa_huge'        # Huge mode
alias ltr='exa_long --sort newest' # Like `ls -ltr`

The huge mode adds inode number and the number of hard links to the beginning of the line.

Screenshots

image

image

EXA_COLORS

Incidently, I use the following EXA_COLORS environment variable:

export EXA_COLORS="in=0;38:lc=0;38:ur=0;33:uw=0;33:ux=0;33:ue=0;33:gr=0;37:gw=0;37:gx=0;37:tr=0;31:tw=0;31:tx=0;31:da=0;36:uu=0;33:gu=0;37"

The default coloring of the permission bits was too colorful.

The above EXA_COLORS makes the color of the owner rwx permission bits match the color of the user owner, and the color of the group rwx permission bits match the group owner. The rwx permission bits of other is set to magenta.

@CouldBeThis
Copy link

Thanks for making this! I found it after spending the evening trying out every other ls-type tool available to see if anyone had made a more configurable alternative with similar functionality. Nope! So did a deeper dive into the github Issues and found this.

Would like to note for anyone else who has the issue I did that on Ubuntu 18.04 the command column turned out to be installed with an old early 2000s bsdmaintools package with way lesser functionality so it didn't work (threw an error about not knowing what to do with -o). The column (manpage) which you are presumably using is in util-linux. While util-linux is in the Ubuntu repos, that version does not include column.

The install docs are worth a read as they have information about how to install just one package rather than the whole kitnkaboodle. I am a bit too dumb to have made it work but I'm sure someone else can find it useful. Running the whole thing looks to have installed several dozen programs...

However, now it works. :) I tried to figure out something like this myself to start but couldn't make the colors persist through the pipe. Wish I had found this first before wasting so much time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment