1

I'm trying to setup zsh so that it autocompletes my rclone commands, for example rclone copy gdr <tab> and it becomes rclone copy gdrive-johnsmith while also doing the same thing to file names.


rclone did come with a command to generate a zsh autocomplete file, but the file did not autocomplete remote directory names (like gdrive-johnsmith: above), just the commands like copy, listremotes, and authorize. I opened up the generated file and could not for the life of me figure out how to make zsh list the remote names (which I can get a list of using rclone listremotes) and the files together "the correct way" (no parsing ls :P). Anyways, I ended up giving up and doing just that and now my _rclone file looks something like this:

#compdef rclone

_arguments \
    '1: :->level1' \
    '2: :->level2' \
    '3: :($(rclone listremotes) $(ls))'

_arguments '*: :($(rclone listremotes) $(ls))'

The entire file is like 38 lines but I don't think there's anything relevant in there (I can post the rest if needed). The "_arguments" line used to look like this:

_arguments '*: :_files'

before I changed it to list both the remotes and files in the cwd. Like I said above, I'd like a way to be able to use _files. My current approach is ugly, breaks whenever file or folder names have a space because zsh splits em up, and it groups everything together and it looks horrible:

 17:43:06  /usr/share/zsh/vendor-completions
 » rclone copy
_bootctl                   gdrive-fakeName:           _rclone
box-fakeName:              _hostnamectl               _sd_hosts_or_user_at_host
_busctl                    _journalctl                _sd_outputmodes
_curl                      _localectl                 _sd_unit_files
dropbox-google-fakeName:   _loginctl                  _systemctl

etc...

Anyways, is there a way to, in zsh:

  • A) Use the _files function or anything that doesn't break whenever I press spacebar to autocomplete rclone remote directories?
  • B) If A is not possible, is there atleast a way to (visually) seperate the normal directories and the rclone ones?

ZSH version is 5.1.1, OS is KDE neon 5.11, oh-my-zsh installed too.

1

From reddit:

You can use _alternative to complete two sorts of things such as directories and rclone remotes. For example, something like:

_arguments \
  '3: : _alternative
    "files:file:_files"
    "remotes:remote:($(rclone listremotes))"'

The space before _alternative is important here to avoid _arguments passing compadd options to it.

Whether they are visually separated depends on your style settings. I think oh-my-zsh does this for you but if not, try the following:

zstyle ':completion:*' group-name ''
zstyle ':completion:*:descriptions' format '%B%d%b'

Thanks to /u/okapi-a!

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.