Shell autocompletion

Shell autocompletion #

For comfort, your shell should allow you to autocomplete commands, in (at least) two ways: Tab-completion and history-based completion.

Tab completion #

This feature suggests “smart” completions of the current command, like possible arguments, options, paths, etc.

Example. On most operating systems, the command cd (like “Change Directory”) allows you to change the current directory. E.g.:

cd workspace

allows you to navigate (from the current folder) to the subfolder called workspace (if it exists).

You can use an arbitrary path as argument of the cd command. For instance:

cd ../myCourses/pp/assignments

or on Windows (with backslashes):

cd ..\myCourses\pp\assignments

For the command cd, Tab-completion suggests possible paths. For instance:

  • typing cd followed by a whitespace and pressing the ‘Tab’ key will suggest the directories contained in the current folder (if any),
  • typing cd ../ (or cd ..\ on Windows) and pressing the Tab key will suggest the directories contained in the parent folder (including the current folder),
  • typing cd wor and pressing the Tab key will suggest the directories contained in the current folder and whose name starts with wor (if any).

History-based completion #

This feature suggests completions of the current command based on the user’s command history. The default key used for history-based completion may vary, depending on the shell: up and down arrow keys for zsh (Linux/macOS), right arrow for the Windows PowerShell.

Example. The command

java -version

displays the version of the default Java Runtime Environment (JRE) and Java Development Kit (JDK) on your machine (if any).

If you want to execute this command again, you may type

ja

and then the history completion key.

This will suggest you the latest command that you typed and started with ja. If you press the key again, then the previous of these commands will be suggested, etc.

Activation #

Windows #

Tab-completion #

The Windows PowerShell should now have Tab completion activated by default. If this is not the case on your machine, you may activate it by editing your Powershell profile, as explained here.

History-based completion #

History-based completion may also be activated by default (this feature is called “Predictive IntelliSense”), but only since 2023, and under certain conditions.

If Predictive IntelliSense it is not activated on your machine, then you may need to install a more recent version of the module called “PSReadLine”, following these instructions.

Warning. You may get an error message when trying to install or upgrade this module. Read it carefully and follow the instructions. These may suggest you to:

  • open the Microsoft PowerShell with administrator rights (select “Run as administrator” when launching the Microsoft PowerShell), or
  • add a flag to your command: either install for the current user only, or force installation for all users.

Linux and macOS #

On most Linux distributions, the default terminal shell is bash, which is not very customizable. Instead, you can install zsh, which is a modern take on bash.

On macOS, zsh is already the default shell, so you can skip the next step and install oh-my-zsh.

Make zsh your default shell (Linux only) #

Use your package manager to install zsh. For instance, on Debian-based distributions (like Ubuntu, Mint, etc.), your package manager is apt, so you can install zsh with:

sudo apt-install zsh

Then to use zsh, execute:

zsh

If this is the first time, then follow the configuration instructions.

To make zsh your default shell, execute:

chsh -s $(which zsh)

You will need to logout and login once for this latter change to take effect.

Install oh-my-zsh #

Oh-my-zsh is a collection of plugins for zsh that enhances the default experience (autocompletion, colors, etc.). To install oh-my-zsh, you can execute the following command:

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

and follow the instructions.