10

If I type in a command for a program that is not installed I might get a message like this:

[user]~$ program
The program 'program' is currently not installed.  You can install it by typing:
sudo apt-get install program
[user]~$ 

Is there a keyboard shortcut or command I can type that will execute sudo apt-get install program without me having to type it out myself?

3 Answers 3

6

Create an alias as appropriate for your shell. In bash, add this line to ~/.bashrc:

alias sagi="sudo apt-get install"

Then, source ~/.bashrc and execute alias to verify the alias shows up in your list. After that, just type

sagi <package>

to invoke the alias. For other shells, the format of the alias command and the name of the rc file will be different, but the source and alias should be the same.

1
  • This seems like a simple and effective solution, cheers!
    – Nick Brunt
    Oct 30, 2012 at 8:48
5

If you are in a graphical terminal, triple-click in the line starting with sudo apt-get to mark it entirely and then middle-click to paste it into the terminal. Alternatively, judging from the output, typing something like

# $(program | tail -n 1)

to execute the output of tail -n 1 command after being fed the output of program. This is probably not shorter. However, this should theoretically allow for the definition of an alias like the following

# alias ii='$( $(history -p !!) | tail -n 1)'

which should take the last command in the history, execute it, get its last line and execute it again. Unfortunately, this doesn’t work. This discusses a similar problem but I didn’t quite manage to adapt it to this one, bash usually complains that it didn’t find "".

4

UPDATE: It will now try to install /bin/program, /usr/bin/program, /sbin/program, or /usr/sbin/program.
This is totally automatic...
Say you type in g++ main.cpp and you do not have g++ installed, it will ask if you want to install package g++, and if you answer yes, it will install it and then run g++ main.cpp.

(This is a one-time setup... Do this and you are good to go)

  1. Install apt-file... sudo apt-get install apt-file
  2. Update its cache... sudo apt-file update
  3. Edit ~/.bashrc and add this to it:

    command_not_found_handle () {
    ask () {
      echo -ne "$1 (Y/n) ";
      read -N1 YES_ANSWER;
      echo "";
      YES_ANSWER=`echo $YES_ANSWER | sed 's/Y/y/; s/N/n/;'`;
      if [ "x$YES_ANSWER" == "xy" ]; then
        unset YES_ANSWER;
        return 0;
      elif [ "x$YES_ANSWER" == "xn" ]; then
        unset YES_ANSWER;
        return 1;
      elif [ "x$YES_ANSWER" == "x" ]; then
        unset YES_ANSWER;
        return 0;
      else
        ask_yes "$1";
        unset YES_ANSWER;
        return $?;
      fi;
    }
    if [ "x$1" == "x" ]; then
      echo "File name not provided.";
      return 1;
    fi;
    echo "Command not found: $1";
    echo "Searching for file in database...";
    FILE="`apt-file search /bin/$1 | sed 's/: /_/'`";
    if [ "x$FILE" != "x" ]; then
      FILE="`apt-file search /usr/bin/$1 | sed 's/: /_/'`";
    fi;
    if [ "x$FILE" != "x" ]; then
      FILE="`apt-file search /sbin/$1 | sed 's/: /_/'`";
    fi;
    if [ "x$FILE" != "x" ]; then
      FILE="`apt-file search /usr/sbin/$1 | sed 's/: /_/'`";
    fi;
    if [ "x$FILE" != "x" ]; then
      for f in "$FILE"; do
        if ask "\nDo you want to install package `echo $f | sed 's/_.*$//'` which provides `echo $f | sed 's/^.*_//'`?"; then
          sudo apt-get install `echo $f | sed 's/_.*$//'`;
          "$@";
          return $?;
        fi;
      done;
    else
      echo "Could not find file $1.";
      return 1;
    fi;
    }
    

I am still testing this, but in the preliminary test it seems to work. Every now and then it would be good to run apt-file update to refresh the list of available files.

To make the edited ~/.bashrc become effective, do one of the following: log out and log back in again, or run . ~/.bashrc.

5
  • Yum is only available for RPM-based linux.
    – dafydd
    Oct 29, 2012 at 18:21
  • @dafydd I realize that. I was just wishing that they did have yum, because of yum provides. It would make things easier.
    – BenjiWiebe
    Oct 29, 2012 at 18:24
  • If anybody sees something wrong with the above command, please leave a comment here telling me what is wrong. I just wrote the above a few minutes ago... Fixed about 4 bugs already... There is probably more yet... :(
    – BenjiWiebe
    Oct 29, 2012 at 18:39
  • +1 for the idea of replacing the command_not_found_handle handler.
    – Zoredache
    Oct 29, 2012 at 19:17
  • @Zoredache The idea comes from Fedora... I did not like it, though, so I figured out what was happening and then disabled it. Now that knowledge came in handy to write some new code for Debian to do the same thing.
    – BenjiWiebe
    Oct 29, 2012 at 19:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .