For many developers and system administrators, the zsh (Z Shell) has become a powerful alternative to more traditional Unix shells, like Bash. It offers a wide range of customizations, plugins, and enhancements that make it an attractive choice for many power users. Two features that can significantly improve your command line experience are the ability to share your shell history across several devices and to have smart autosuggestions based on that history.
In this post, we'll explore how to set up these features using zsh-histdb and zsh-autosuggestions plugins.
The first step is to install the zsh-histdb plugin, which replaces the default Zsh command history file with a SQLite database. This database not only makes it possible to retrieve and search your history across devices, but also offers performance improvements when dealing with large history files. To install zsh-histdb, follow the instructions below:
The next step is to create a remote SQLite database on a server you have access to, which will store your Zsh history. This can be any remote server that you have SSH access to and supports SQLite.
The zsh-autosuggestions plugin will offer smart auto-completion based on your command history. To install it, follow these steps:
By combining the power of zsh-histdb and zsh-autosuggestions, you can manage a more robust and efficient shell history that syncs across devices, along with context-aware autosuggestions - significantly improving your command line experience.
git clone <https://github.com/larkery/zsh-histdb.git> ~/.zsh-histdb
source ~/.zsh-histdb/zsh-histdb.zsh
sqlite3 ~/zsh_history.db "CREATE TABLE IF NOT EXISTS history (start_time INTEGER PRIMARY KEY, duration INTEGER, exit_code INTEGER, command TEXT, argc INTEGER);"
This command creates a file called zsh_history.db in your server's home directory with the schema required by zsh-histdb.
For Debian/Ubuntu:
sudo apt-get install sshfs
For macOS:
brew install sshfs
mkdir ~/remote-zsh-history sshfs user@yourserver.com:~/ ~/remote-zsh-history
Make sure to replace "user" with your server's username and "yourserver.com" with your server's address.
export HISTDB_FILE=~/remote-zsh-history/zsh_history.db
git clone <https://github.com/zsh-users/zsh-autosuggestions> ~/.zsh-autosuggestions
source ~/.zsh-autosuggestions/zsh-autosuggestions.zsh