I have set the ls program dired uses via

(setq insert-directory-program "gls"))

(so that I can specify --group-directories-first in dired-listing-switches).

Trouble is when I browse a remote directory, TRAMP seems to use the same variable insert-directory-program, but on arbitrary remote machines the gls command might not be available.

How can I set the ls program dired uses to gls only for local directories?

  • What (local) system are you running on? Have you built gls in e.g. /usr/local/bin with the system ls in /usr/bin?
    – NickD
    Commented Sep 2, 2021 at 20:14
  • @NickD Thanks for chiming in! I am on macOS Mojave (Intel) and installed GNU coreutils via Homebrew. So, yes, gls is in /usr/local/bin and the standard ls is in /bin.
    – halloleo
    Commented Sep 3, 2021 at 1:00
  • So you could set your PATH (and emacs's exec-path) to have /usr/local/bin first and rename gls to ls. Then restore the default for insert-directory-program: that way, you (and emacs) would get the ls that knows about --group-directories-first and you'd be happy and since you don't change the default, tramp would use that and everybody else would be happy too. People on your machine who have not set their PATH that way (either not including it at all or including it after /bin) would get the system ls and presumably they would be happy too. Any reason this wouldn't work?
    – NickD
    Commented Sep 3, 2021 at 3:12
  • @NickD That's probably the way to go. I just had hoped that there were separate variables for local and remote dired...
    – halloleo
    Commented Sep 4, 2021 at 12:36

1 Answer 1


Elaborating on the comments form @NickD there seems to be no separate way to configure the used ls command used used for TRAMP directories vs local directories.

So the remaining option is to symlink the local gls (at least for Emacs) to ls and keep Emacs' default of insert-directory-program as "ls".

Update 28 Sep 2021:

Also, it is important to use no non-standard switches in dired-listing-switches, because they are used for the remote dired buffers as well! So stick with something like "-alhF" and don't use GNU's --group-directories-first, beacuse your TRAMP dired buffere will be empty when the remote system for example plain BSD.

  • Another option might be to use ls-lisp instead of a OS-level ls, but I don't know what ls-lisp does for remote directories. - Will test it!
    – halloleo
    Commented Sep 8, 2021 at 1:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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