Abstract
In this little howto I will provide a script that enables the use of bash alias:es for common apt commands, like 'sudo apt-get install', while still keeping that tab completion for package names.
Introduction/The Problem
If you've discovered that the command line interface for the packaging system that Ubuntu uses (APT) is quicker to work with than the GUIs available for it, then you've probably been tempted to add aliases to your ~/.bashrc, e.g.
Code:
# [...]
# At the end of ~/.bashrc
alias sagi='sudo apt-get install'
You probably then also noted that TAB completion didn't work with the alias, and TAB completion is really nice to have at times, for example when hunting lib*:s for use when building software.
The Solution
- Save 'apt_completion.txt' as 'apt_completion' to your home directory (/home/martin in my case)
- Add these lines to the end of ~/.bashrc.
Code:
# For apt alias:es TAB completion
source ~/apt_completion
- To activate, either restart the Terminal, or reload ~/.bashrc by issuing the command
You can now use common apt-commands with aliases, with TAB completion for package names (in a smart way; sagr will only list installed packages, etc). Supported commands are:
sudo apt-get install, aliased by default with sagi
sudo apt-get remove, aliased by default with sagr
apt-cache show, aliased by default with acsh
The script also aliases
apt-cache search, aliased by default with acs
because it is very useful to have aliased.
Customization
If you want other aliases, you can manually edit the 'SETUP' portion of apt_completion, which is in the beginning of the file and looks like this:
Code:
############################################################
######################## SETUP #######################
# These variables specifies what aliases you want
SUDO_APT_GET_INSTALL_ALIAS=sagi
SUDO_APT_GET_REMOVE_ALIAS=sagr
APT_CACHE_SHOW_ALIAS=acsh
APT_CACHE_SEARCH_ALIAS=acs
######################## END SETUP #######################
############################################################
Simply change e.g. 'sagi' to what you prefer, then save your changes.
How it works
The tab completion feature available when using the full command isn't automatically transfered to an aliased command. This is because of how tab completion works within bash. Whenever TAB is pressed, what bash does is basically to see if completion has been registered with the command that precedes the cursor. To get TAB completion to work with the alias:es, one have to register a function that provides the possible completions for that particular command.
I have based apt_completion, which registers these function with the provided aliases, on bash_completion (written by Ian Macdonald) that comes with Ubuntu.
Disclaimer
The script is probably improvable, and if you look into it you will note that there are som RPM stuff in it, which I have not tested. I left it there since the original bash_completion on which I based the script on had it, and anyone using an RPM system might find those parts useful. Any pointers on the script are appreciated.
Tested On
This script works on at least Ubuntu 6.10 and 7.04 Beta, but I assume it works on all Ubuntu versions. If you don't get it to work, let me know!
Uninstallation
Remove apt_completion and the lines you added to ~/.bashrc, namely
Code:
# For apt alias:es TAB completion
source ~/apt_completion
Bookmarks