Two tools that are fun and productive on the command line: zoxide and shell_gpt

I've been using zoxide and sgpt for about a year, and they now as standard in my stack as tools like docker or git. In this post I will quickly demonstrate both of them and how you can start using them today 😎

zoxide

TLDR: It makes jumping between specific folders super easy ✨

The problem

Do you have to jump around different directories a lot during your work day?

In this example, it may get tiring after a while to remember the directory paths. In the past, I specifically created a bash alias so I can just type "cdepic" and jump right into my project no matter where I am:

alias cdepic="cd ~/PhpStormProjects/epic-scrolls-64"

I could create more aliases for more folders... But this gets old after a while.

What zoxide does

After you installed it, you will have access to the z command which is your new cd. (Don't worry, you can still use cd afterwards).

z keeps a local cache of directories you have visisted, and afterwards applies a smart rule-based system to figure out where you want to go.

In my case, this could look something like this:

First I jump all the way from my home directory into a super specific subfolder of a scala project just by typing z scal. zoxide matches the rightmost directoy first, that's why it didn't jump into scalascape. It also ranks directories based on how many times you have visited them lately and when you visited it the last time.

zoxide is available in most package managers, however if you are using Ubuntu, the repository is awfully outdated (as usual), and it is recommended to run an install script - as always, review it before running it. You can find the installation instructions and more useful tips here: https://github.com/ajeetdsouza/zoxide

shell_gpt

So I heard you like ChatGPT and you like the command line? I present to you: shell_gpt

After you have installed it, you can let your computer tell you jokes:

But more importantly: it is also aware of some environment parameters, namely which operating system you are running on. So if you ask something like "how do I install php8?", it will recommend you a fitting command for the respective OS you're on. You can even let it suggest, describe and execute a command by adding the --shell flag. I marked my inputs in green.

In the above screenshot, I entered sgpt --shell "give me the current directory size (including all sub folders) into the command line.

sgpt then suggested the command du -sh . and gave me the choice: E to execute, D to describe, A to abort.

I chose d and it explained all parts of the command. (caution: since it is an LLM, you have to apply some common sense about how risky the operation is and how much you trust the explanation).

Then I chose e because I was confident this is the correct command. As a result the command was executed and I received information that my PhpStormProjects folder has a size of 945 MiB. Darn composer packages adding up.

If you want a conversation that lasts longer than a single request/response, you need to enter --repl mode, followed by the name of the thread. Here is an example of a repl conversation where I asked a few questions about how to get hard drive usage information. I marked my queries in green for better readability.

Here is a more advanced example from the sgpt maintainer:

sgpt --chat conversation_3 --shell "what is in current folder"
# -> ls
sgpt --chat conversation_3 "Sort by name"
# -> ls | sort
sgpt --chat conversation_3 "Concatenate them using FFMPEG"
# -> ffmpeg -i "concat:$(ls | sort | tr '\n' '|')" -codec copy output.mp4
sgpt --chat conversation_3 "Convert the resulting file into an MP3"
# -> ffmpeg -i output.mp4 -vn -acodec libmp3lame -ac 2 -ab 160k -ar 48000 final_output.mp3

Installation

To get started with shell_gpt, you need an openai api key. You can create one here: https://platform.openai.com/api-keys. If you don't have an openai platform account yet, you have to create one. You will also need to add some prepaid credits to your account before you can start using the api. Prices can be found here: https://openai.com/api/pricing/. As a rule of thumb, you can spend around 8 million tokens with a budget of $1 - that will last for a while.

You might have to install pip (python package manager) first. On Ubuntu it would be something like apt install pip.

Finally, you can install shell_gpt like this: pip install shell-gpt. It will ask for an api key on the first run, so make sure to have it ready.

For more detailed instructions and tips, check out the repo: https://github.com/TheR1D/shell_gpt

Summary

In this post I told you about two tools you probably didn't know you need: zoxide, a smarter cd command, and sgpt, gpt in your shell.

Read more