Improving ZSH Suggestions: Making Recent Commands Logical
ZSH is a powerful and customizable shell that offers many features to improve productivity and ease of use. One of these features is the ability to suggest commands based on recent command history. However, this feature can sometimes be frustrating to use, as it may not always suggest the most logical or relevant commands. In this article, we will explore how to improve ZSH suggestions for recent commands, making them more logical and useful.
Understanding ZSH Suggestions
ZSH suggestions are based on the command history, which is a record of all the commands that have been executed in the current session. By default, ZSH keeps track of the last 500 commands in the history. When you start typing a command, ZSH will suggest completions based on the commands in the history that match the characters you have typed so far.
For example, if you have previously run the command cd /usr/local/bin, and you start typing cd /u, ZSH will suggest completing the command with /usr/local/bin.
The Problem with Recent Commands
While ZSH suggestions based on recent commands can be helpful, they can also be frustrating. For example, if you have run the command cd app/two/sub-folder1, and then run cd app/two/sub-folder2, ZSH will suggest completing the command with cd app/two/sub-folder1 instead of cd app/two or cd app.
This behavior can be frustrating because it may not always suggest the most logical or relevant command. In the example above, it would be more useful if ZSH suggested completing the command with cd app/two, as this would allow you to navigate to any sub-folder under app/two without having to type the entire path.
Improving ZSH Suggestions
To improve ZSH suggestions for recent commands, we can use a combination of configuration options and custom scripts. Here are some steps you can take to make ZSH suggestions more logical:
Set the
HISTFILEoption to a file that is not deleted when the shell exits. This will ensure that your command history is persisted between sessions, allowing ZSH to suggest commands based on your entire command history, not just the commands from the current session.Set the
HISTSIZEoption to a large value, such as 10000. This will increase the number of commands that ZSH keeps in the history, allowing it to suggest more commands.Use the
zsh-autosuggestionsplugin, which provides more intelligent suggestions based on the command history. This plugin can be installed using theantigencommand line tool.Create a custom script that modifies the ZSH suggestion algorithm to suggest more logical completions. For example, the following script suggests completing the command with the parent directory if the current directory is a sub-directory:
ZSH suggestions based on recent commands can be frustrating to use, but with some configuration and customization, they can be made more logical and useful. By persisting the command history, increasing the history size, using the zsh-autosuggestions plugin, and creating custom scripts, you can improve ZSH suggestions and make your shell more productive.