Is there a shortcut to run a suggested command in a terminal?
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.
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)
- Install apt-file...
sudo apt-get install apt-file
- Update its cache...
sudo apt-file update
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
.
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 ""
.