Auto-complete And View Previous Commands (history) in The Terminal


This article could be helpful for those new to Linux who find the terminal confusing or complex. Previously typed commands are stored up to 500 lines and can be increased in the .bash_profile.

Here are some ways to make command writing in the Linux terminal easier:

1. Using History

View previous commands in the terminal by pressing the up arrow key on the keyboard. Just press Enter after finding the previous command. This helps speed up command writing. However, it can be cumbersome if there are over 100 history entries and the desired command is in the middle or beginning. You can use the command:

history

This will display a list of previously used commands.

2. Using "reverse-i-search"

This feature comes built-in. To use it, press CTRL+R, and "reverse-i-search" will appear in the terminal. Type in a word or part of a command. It will display previous commands containing that word or letters. For example, if you previously used the command sudo apt install, typing apt in "reverse-i-search" will bring up sudo apt install.
 

To see the next matching command, press CTRL+R again. For example, if you've typed sudo apt install and sudo apt upgrade before and you use "reverse-i-search" with the keyword apt, if the first result is sudo apt install, pressing CTRL+R again will show sudo apt upgrade.

3. Using OhMyZsh and the zsh-autosuggestions plugin

There's another way to autocomplete besides "reverse-i-search." You can use OhMyZsh and the zsh-autosuggestions plugin. However, you must install them first to use them. Here's how:

First, install OhMyZsh. You can choose one of the following methods: curl, wget, or fetch:
curlsh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
wgetsh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
fetchsh -c "$(fetch -o - https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

If you encounter the issue "Zsh not installed. Please install zsh first" as shown in the image below, install Zsh first.

Here’s how to install Zsh:

sudo apt install zsh

Then, check if Zsh is installed by running:

zsh --version

The version of Zsh will appear.

If there are any updates for the installation and more detailed instructions, you can check the developer's GitHub through the following link: https://github.com/ohmyzsh/ohmyzsh#basic-installation.

Secondly, add the zsh-autosuggestions plugin using the complete instructions on GitHub: https://github.com/zsh-users/zsh-autosuggestions/blob/master/INSTALL.md#oh-my-zsh.

And here’s how it looks.

Here's an example of autocomplete when typing the command "cd D":




Source:
https://towardsdev.com/3-simple-ways-to-set-up-autocompletion-in-the-terminal-cf4915d29281
Next Post Previous Post
No Comment
Add Comment
comment url