bash autocomplete

153 views
Skip to first unread message

haaber

unread,
Mar 1, 2018, 3:48:32 PM3/1/18
to qubes-users
Since Q4 much admin work is done on the command line. So it makes sense
to learn bash to autocomplete nicely. I tried this in dom0:

_qvm()
{ local cur VMS
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
VMS=`qvm-ls | cut -f1 -d" "| grep -v NAME`
COMPREPLY=( $(compgen -W "${APPVMS}" ${cur}) )
return 0
}
complete -F _qvm qvm-start


in order to type qvm-start [TAB] and get the list of available VMS. But
this does not work, even if, type in line by line a terminal the two
commands (qvm-ls and compgen) do work ! Is there some bash-guru that
sees the error and helps me, please? I guess its the damn pipes!
Bernhard

haaber

unread,
Mar 1, 2018, 3:59:17 PM3/1/18
to qubes...@googlegroups.com
> Since Q4 much admin work is done on the command line. So it makes sense
> to learn bash to autocomplete nicely. I tried this in dom0:
>
> _qvm()
> { local cur VMS
> COMPREPLY=()
> cur="${COMP_WORDS[COMP_CWORD]}"
> VMS=`qvm-ls | cut -f1 -d" "| grep -v NAME`
> COMPREPLY=( $(compgen -W "${VMS}" ${cur}) )
> return 0
> }
> complete -F _qvm qvm-start
>

there was a small type (APPVMS <-> VMS) that is not the real problem
here. I corrected it above inside the quote. Bernhard

Unman

unread,
Mar 2, 2018, 4:22:07 AM3/2/18
to haaber, qubes...@googlegroups.com
Try this:

_qvm()
{ local cur
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
VMS=`qvm-ls --raw-list`
COMPREPLY=( $(compgen -W "${VMS}" -- ${cur}) )
}


haaber

unread,
Mar 2, 2018, 2:06:15 PM3/2/18
to qubes...@googlegroups.com
On 03/02/2018 10:22 AM, Unman wrote:
>
> Try this:
>
> _qvm()
> { local cur
> COMPREPLY=()
> cur="${COMP_WORDS[COMP_CWORD]}"
> VMS=`qvm-ls --raw-list`
> COMPREPLY=( $(compgen -W "${VMS}" -- ${cur}) )
> }
works perfectly, thank you. Should be completed (haha) by
complete -F _qvm qvm-appmenus
complete -F _qvm qvm-clone
complete -F _qvm qvm-firewall
complete -F _qvm qvm-move-to-vm
complete -F _qvm qvm-remove
complete -F _qvm qvm-start-gui
complete -F _qvm qvm-unpause
complete -F _qvm qvm-backup
complete -F _qvm qvm-copy-to-vm
complete -F _qvm qvm-pause
complete -F _qvm qvm-run
complete -F _qvm qvm-usb
complete -F _qvm qvm-backup-restore
complete -F _qvm qvm-service
complete -F _qvm qvm-kill
complete -F _qvm qvm-shutdown
complete -F _qvm qvm-tags
complete -F _qvm qvm-check
complete -F _qvm qvm-features
complete -F _qvm qvm-prefs
complete -F _qvm qvm-start

to have the shell behave nicer. If I have some free time, I might
customize this stub to suggest available options to all qvm-* and
qubes-* commands. I am surprised that I might be the first one to
discuss this subject (?!) Bernhard

Holger Levsen

unread,
Mar 2, 2018, 2:10:29 PM3/2/18
to haaber, qubes...@googlegroups.com
On Tue, Feb 27, 2018 at 03:23:50PM +0100, haaber wrote:
> to have the shell behave nicer. If I have some free time, I might
> customize this stub to suggest available options to all qvm-* and
> qubes-* commands. I am surprised that I might be the first one to
> discuss this subject (?!) Bernhard

i'm definitly interested in this, this is super useful.


--
cheers,
Holger
signature.asc

awokd

unread,
Mar 2, 2018, 2:14:32 PM3/2/18
to haaber, qubes...@googlegroups.com
On Fri, March 2, 2018 7:10 pm, Holger Levsen wrote:
> On Tue, Feb 27, 2018 at 03:23:50PM +0100, haaber wrote:
>
>> to have the shell behave nicer. If I have some free time, I might
>> customize this stub to suggest available options to all qvm-* and
>> qubes-* commands. I am surprised that I might be the first one to
>> discuss this subject (?!) Bernhard

Don't think the first, but this is the first functioning example I've seen.

> i'm definitly interested in this, this is super useful.

Ditto!


Marek Marczykowski-Górecki

unread,
Mar 2, 2018, 2:31:37 PM3/2/18
to aw...@danwin1210.me, haaber, qubes...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

On Fri, Mar 02, 2018 at 07:13:56PM -0000, 'awokd' via qubes-users wrote:
> On Fri, March 2, 2018 7:10 pm, Holger Levsen wrote:
> > On Tue, Feb 27, 2018 at 03:23:50PM +0100, haaber wrote:
> >
> >> to have the shell behave nicer. If I have some free time, I might
> >> customize this stub to suggest available options to all qvm-* and
> >> qubes-* commands. I am surprised that I might be the first one to
> >> discuss this subject (?!) Bernhard
>
> Don't think the first, but this is the first functioning example I've seen.

There were multiple attempts for zsh, for Qubes 3.2:
https://gist.github.com/kalkin/133feb85ad63712dc859
https://github.com/woju/qubes-core-admin/commit/501fb736282741e90bb323f818026d9c8bc0863c

> > i'm definitly interested in this, this is super useful.
>
> Ditto!

!

- --
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
-----BEGIN PGP SIGNATURE-----

iQEzBAEBCAAdFiEEhrpukzGPukRmQqkK24/THMrX1ywFAlqZploACgkQ24/THMrX
1yxIcgf/bGmsjrWg7jtfma61PIRzl7s7UxS1oz62rWrFoyhbHBepQmalq8KZSeFa
MDn23u4P0eCBzYrEIckF3cOIjV0qVT3+5c35FVhaa2F1kRLKt6jkpLeBExLqDREr
ppPTY9Oy4jXVoO7LWt8GUmfKwCViQBaZrEr4nKhJtPy6WISxIwYX/7KuOvC8V7j5
T5BK21U1DaepTGNF7V/atnJuOMdhc701puWRTaLRFViwIKqm7FlHAmr80xEOx+8W
8WVn5HhZ4wlN4LvR40Pbk6VkcW4LEwIE+DTPuef4wmuXrS0AyAkn0uL9dubzX5gq
VhNPhxhcg6dntNM+fZ9aPS6r/0to2Q==
=BQl5
-----END PGP SIGNATURE-----

Holger Levsen

unread,
Mar 11, 2018, 8:56:55 AM3/11/18
to Unman, haaber, qubes...@googlegroups.com
so thanks, Unman and haaber, I now have this as my .bashrc and it works
nicely:


# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions

_qvm()
{ local cur
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
VMS=`qvm-ls --raw-list`
COMPREPLY=( $(compgen -W "${VMS}" -- ${cur}) )
}

complete -F _qvm qvm-appmenus
complete -F _qvm qvm-clone
complete -F _qvm qvm-firewall
complete -F _qvm qvm-move-to-vm
complete -F _qvm qvm-remove
complete -F _qvm qvm-start-gui
complete -F _qvm qvm-unpause
complete -F _qvm qvm-backup
complete -F _qvm qvm-copy-to-vm
complete -F _qvm qvm-pause
complete -F _qvm qvm-run
complete -F _qvm qvm-usb
complete -F _qvm qvm-backup-restore
complete -F _qvm qvm-service
complete -F _qvm qvm-kill
complete -F _qvm qvm-shutdown
complete -F _qvm qvm-tags
complete -F _qvm qvm-check
complete -F _qvm qvm-features
complete -F _qvm qvm-prefs
complete -F _qvm qvm-start
complete -F _qvm qm

For your convinience I've also attached this file. (you might want to comment
out the last line...)


--
cheers,
Holger
.bashrc
signature.asc

haaber

unread,
Mar 11, 2018, 9:11:07 AM3/11/18
to Unman, qubes...@googlegroups.com
Thank you Holger,
I don't know what this 3D-thing, is I'll learn it. I have, in the
meanwhile, tested the attached file, that distinguishes also running,
paused and halted VM's. For the moment this is completely sufficient for
me. Maybe I'll add the completion "root" when I complete "qvm-run -u",
since this is what I need for updating sudo-less minimal templates :)

I put the file it in /etc/bash_completion.d/ within dom0, and source it
in .bashrc. Bernhard
qvm

Holger Levsen

unread,
Mar 11, 2018, 9:52:40 AM3/11/18
to haaber, Unman, qubes...@googlegroups.com
awesome, thanks for sharing (again)! :)


--
cheers,
Holger
signature.asc

brenda...@gmail.com

unread,
Mar 12, 2018, 6:27:17 AM3/12/18
to qubes-users

FYI, I noticed a qvmhall instead of a qvmall on one of the lines in your script, just FYI.

Brendan

eva...@protonmail.com

unread,
Mar 21, 2018, 9:53:47 AM3/21/18
to qubes...@googlegroups.com
Can this be included at Qubes Q4 or at some Qubes repo with tools for quick installation?



Ivan Mitev

unread,
Apr 27, 2018, 3:55:17 PM4/27/18
to qubes...@googlegroups.com
[ reviving an old thread :) ]
FWIW I've written a new bash autocomplete script based on your original
PoC; it's available at:

https://github.com/Qubes-Community/Contents/blob/master/code/productivity/qvm-cmds-bash-completion.bash

Additional features:
- completes the 2d arg of qvm-prefs, qvm-features, qvm-tags
- completes filenames of qvm-{copy,move}-to-vm
- also lists transient VMs for qvm-kill

Main limitation: parsing won't work properly when using option arguments
(eg. -s blah). Unfortunately there is no way to solve this except
parsing every known option for a given qvm-* command, which would be a
pain to maintain.

I can submit a PR for inclusion in one of dom0's qubes packages (don't
know which) if there's sufficient interest and enough people test it...

Cheers,
Ivan
Reply all
Reply to author
Forward
0 new messages