# Pro-tip: Creating aliases in Bash with autocompletion enabled

**URL:** <https://community.learnlinux.tv/t/pro-tip-creating-aliases-in-bash-with-autocompletion-enabled/1016>\
**Category:** Ubuntu\
**Created:** [October 22, 2021, 2:50pm UTC](https://community.learnlinux.tv/t/pro-tip-creating-aliases-in-bash-with-autocompletion-enabled/1016 "2021-10-22T14:50:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ameinild](https://community.learnlinux.tv/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ameinild](https://community.learnlinux.tv/u/ameinild)\
**Post date:** [October 22, 2021, 2:50pm UTC](https://community.learnlinux.tv/t/pro-tip-creating-aliases-in-bash-with-autocompletion-enabled/1016/1 "2021-10-22T14:50:58Z")

</div>

Hello.

I recently stumbled upon a really nice post, detailing how you can make a Bash alias, and at the same time ensure autocompletion works as per the original command.

The example here will be `systemctl` (and related commands) on Ubuntu - but I imagine this works with other commands and distributions as well.

This is my `~/.bash_aliases`, defining shorter commands for `systemctl` etc.:

```auto
alias gctl='loginctl'
alias hctl='hostnamectl'
alias jctl='journalctl'
alias lctl='localectl'
alias sctl='systemctl'
alias tctl='timedatectl'

```

Now, to make autocompletion work for all these commands, I create the following file: `/etc/bash_completion.d/bash_completion` with these contents:

```auto
# Bash completion for "gctl", "hctl", "jctl", "lctl", "sctl" and "tctl",
if [[-r /usr/share/bash-completion/completions/loginctl]]; then
    . /usr/share/bash-completion/completions/loginctl && complete -F _loginctl loginctl gctl
fi

if [[-r /usr/share/bash-completion/completions/hostnamectl]]; then
    . /usr/share/bash-completion/completions/hostnamectl && complete -F _hostnamectl hostnamectl hctl
fi

if [[-r /usr/share/bash-completion/completions/journalctl]]; then
    . /usr/share/bash-completion/completions/journalctl && complete -F _journalctl journalctl jctl
fi

if [[-r /usr/share/bash-completion/completions/localectl]]; then
    . /usr/share/bash-completion/completions/localectl && complete -F _localectl localectl lctl
fi

if [[-r /usr/share/bash-completion/completions/systemctl]]; then
    . /usr/share/bash-completion/completions/systemctl && complete -F _systemctl systemctl sctl
fi

if [[-r /usr/share/bash-completion/completions/timedatectl]]; then
    . /usr/share/bash-completion/completions/timedatectl && complete -F _timedatectl timedatectl tctl
fi

```

Next time you login (or run `source /etc/bash_completion`), autocompletion now works for the aliases, exactly as for the original commands.

The original post is [here on Ask Ubuntu](https://askubuntu.com/questions/1370840/how-to-make-alias-to-systemctl-with-autocomplete) for reference

Have a nice weekend. 😃 🕶

---

<div class="post-metadata">

**Author:** ![Buffy](https://community.learnlinux.tv/user_avatar/community.learnlinux.tv/buffy/32/497_2.png) [@Buffy](https://community.learnlinux.tv/u/Buffy)\
**Post date:** [October 22, 2021, 2:58pm UTC](https://community.learnlinux.tv/t/pro-tip-creating-aliases-in-bash-with-autocompletion-enabled/1016/2 "2021-10-22T14:58:20Z")

</div>

Neat! 🐧

You should be able to make a `~/.bash_completion` file, too, and have it sourced using the same little code block in your `~/.bashrc` that it uses to source your `~/.bash_aliases`. 😸

---

<div class="post-metadata">

**Author:** ![ameinild](https://community.learnlinux.tv/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ameinild](https://community.learnlinux.tv/u/ameinild)\
**Post date:** [October 22, 2021, 4:02pm UTC](https://community.learnlinux.tv/t/pro-tip-creating-aliases-in-bash-with-autocompletion-enabled/1016/3 "2021-10-22T16:02:43Z")

</div>

Yeah, I think it’s up to your own taste if you want the autocompletion file sourced from `/etc/bash_completion.d` automatically, or from `~/.bashrc` by yourself. I guess this will depend on whether this should be a systemwide or user-only setting.

---

<div class="post-metadata">

**Author:** ![Buffy](https://community.learnlinux.tv/user_avatar/community.learnlinux.tv/buffy/32/497_2.png) [@Buffy](https://community.learnlinux.tv/u/Buffy)\
**Post date:** [October 22, 2021, 4:06pm UTC](https://community.learnlinux.tv/t/pro-tip-creating-aliases-in-bash-with-autocompletion-enabled/1016/4 "2021-10-22T16:06:50Z")

</div>

Well, I think generally if you make the aliases per-user, the completions for them should be too?

---

<div class="post-metadata">

**Author:** ![ameinild](https://community.learnlinux.tv/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ameinild](https://community.learnlinux.tv/u/ameinild)\
**Post date:** [October 22, 2021, 4:09pm UTC](https://community.learnlinux.tv/t/pro-tip-creating-aliases-in-bash-with-autocompletion-enabled/1016/5 "2021-10-22T16:09:16Z")

</div>

That makes sense. But since it’s my own homelab, I’m the only “real” user, so it doesn’t make any difference in my case. 😉
