Skip to content

Instantly share code, notes, and snippets.

@OleMchls
Created April 14, 2012 13:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save OleMchls/2384354 to your computer and use it in GitHub Desktop.
Save OleMchls/2384354 to your computer and use it in GitHub Desktop.
Vagrant bash autocomplete
# Autocompletion for Vagrant just put this line in your ~/.profile
complete -W "$(echo `vagrant --help | awk '/box/,/up/ {print $1}'`;)" vagrant
@Velaa98
Copy link

Velaa98 commented Oct 25, 2018

good contribution, thanks. I tried replacing "--help" with "list-commands" but the "rsync" option does not appear. You know why?

@antbrown
Copy link

@Velaa98 There's a possibility you have a plugin installed which provides a vagrant command with the string "up" in its name, for example something like vagrant-hostsupdater. If this is the case then you might be getting a shorter command list from vagrant list-commands | awk '/box/,/up/ {print $1}' than you were expecting.

I saw a comment on stackoverflow that made me think this might work:

# Vagrant bash completion.
complete -W "$(echo $(vagrant list-commands | sed '1,3d' | awk '{print $1}'))" vagrant

As far as I understand it...

  1. We use the complete bash builtin function, pass it a word list -W "..." and tell it to do its magic on the vagrant command.
  2. The word list is constructed by using two nested command substitions - $() - "it literally plugs the command output into another context".
  3. The outermost command substitution echo will print the results of the innermost one vagrant list-commands | sed '1,3d' | awk '{print $1}.
  4. vagrant list-commands will give us a list of commands vagrant knows about, but with 3 lines of descriptive text at the start.
  5. The sed command 1,3d will delete the first 3 lines of the output piped to it, this is how the header text is chopped off.
  6. The awk program {print $1}prints the first field for each line of output. A field to awk is anything up to the first white-space character for each line, this is how the descriptions are removed, leaving just the vagrant command name.

I think that's how it works.

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