October 13, 2018

Grouping and sorting regular and symlinked folders before files with ls in zsh

By default ls sorts all folders, symlinked folders and files by name and not by type and then by name, which is very annoying and sometimes even confusing.

Thankfully you can change this behavior pretty easy, you just need to add one small function to your zsh configuration:

dir1st() { [[ -d $REPLY ]] && REPLY=1-$REPLY || REPLY=2-$REPLY;}

Then call it like this:

ls -ldU -- *(o+dir1st)

And here’s an example image:

If you’re a Bash user and knows how to make this work there, let me know! I’m pretty sure there’s a lot of Bash users out there who would want this as well.

Comments

I'm looking into a good way of implementing comments on my blog that works without JavaScript, respects the users privacy and is reasonably user friendly. If you have any ideas or just want to give me some feedback on this blog post or if you just want to say hello, feel free to do so either via e-mail, XMPP or Mastodon.