Results 1 to 5 of 5

Thread: HOWTO: apt-get / apt-cache alias:es with Package name Tab Completion in Bash

  1. #1
    Join Date
    Nov 2006
    Beans
    25

    HOWTO: apt-get / apt-cache alias:es with Package name Tab Completion in Bash

    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

    1. Save 'apt_completion.txt' as 'apt_completion' to your home directory (/home/martin in my case)
    2. Add these lines to the end of ~/.bashrc.
      Code:
      # For apt alias:es TAB completion
      source ~/apt_completion
    3. To activate, either restart the Terminal, or reload ~/.bashrc by issuing the command
      Code:
      $ source ~/.bashrc


    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
    Attached Files Attached Files

  2. #2
    Join Date
    Jun 2006
    Beans
    156
    Distro
    Kubuntu 8.10 Intrepid Ibex

    Re: HOWTO: apt-get / apt-cache alias:es with Package name Tab Completion in Bash

    Thanks! This is nicer than the scripts I'd come up with (http://ubuntuforums.org/showthread.php?t=525170). I found this after searching on tab completion. Pretty complicated script - thanks for sharing it! I'm happy to have tab completion again. The only thing I changed was the names: I call them appget, appgone, appshow, and appgrep.

  3. #3
    Join Date
    Apr 2008
    Location
    Ohio
    Beans
    202

    Re: HOWTO: apt-get / apt-cache alias:es with Package name Tab Completion in Bash

    I don't like filling up my home directory, so I renamed the file ".apt" (without the quotes) and changed
    Code:
    # For apt alias:es TAB completion
    source ~/apt_completion
    to
    Code:
    # For apt alias:es TAB completion
    source ~/.apt

  4. #4

    Re: HOWTO: apt-get / apt-cache alias:es with Package name Tab Completion in Bash

    For any other readers who come across this post via google, here's a link to the txt file on pastebin:

    http://pastebin.com/HVYNwCPX

    The URL ends in HVYNwCPX

    Ubuntuforums.org requires registration to view attachments, which is total and utter ****.

  5. #5
    Join Date
    Feb 2007
    Beans
    24,961
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: HOWTO: apt-get / apt-cache alias:es with Package name Tab Completion in Bash


    From the Ubuntu Forums Code of Conduct.
    If a post is older than a year or so and hasn't had a new reply in that time, instead of replying to it, create a new thread. In the software world, a lot can change in a very short time, and doing things this way makes it more likely that you will find the best information. You may link to the original discussion in the new thread if you think it may be helpful.
    Thread closed.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •