Monday, May 6, 2024
6
rated 0 times [  6] [ 0]  / answers: 1 / hits: 2737  / 2 Years ago, thu, march 3, 2022, 3:25:41

I have a whole bunch of aliased commands in .bash_aliases



The most generic examples are



alias apt-update="sudo apt-get update"  
alias apt-upgrade="sudo apt-get dist-upgrade"


but then I also have



alias apt-install="sudo apt-get install "
alias a2avail="/etc/apache2/sites-available"


The first two are no issue,
The second two I want to make the more powerful;



Specifically for apt-install I want autocomplete to work.



for a2avail I want to be able to type something like sudo cp a2avail/default a2avail/new and have it run as sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/new



are either of these two things possible?


More From » command-line

 Answers
0

Adding the following to your ~/.bashrc should complete packagenames for your apt-install alias



_apt_install_complete() { 
mapfile -t COMPREPLY < <(apt-cache --no-generate pkgnames "$2");
}
complete -F _apt_install_complete apt-install


As for a2avail, an alias will only work as the first word of a command, but you can use a variable.



a2avail=/etc/apache2/sites-available   # in ~/.bashrc

# then...
sudo cp "$a2avail"/{default,new}

[#36223] Friday, March 4, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
confiorc

Total Points: 42
Total Questions: 121
Total Answers: 123

Location: India
Member since Wed, Aug 26, 2020
4 Years ago
;