ShellGPT
A command-line productivity tool powered by AI large language models (LLM). This command-line tool offers streamlined generation of shell commands, code snippets, documentation, eliminating the need for external resources (like Google search). Supports Linux, macOS, Windows and compatible with all major Shells like PowerShell, CMD, Bash, Zsh, etc.
Installation
pip install shell-gpt
By default, ShellGPT uses OpenAI’s API and GPT-4 model. You’ll need an API key, you can generate one here. You will be prompted for your key which will then be stored in ~/.config/shell_gpt/.sgptrc
. OpenAI API is not free of charge, please refer to the OpenAI pricing for more information.
Tip
Alternatively, you can use locally hosted open source models which are available for free. To use local models, you will need to run your own LLM backend server such as Ollama. To set up ShellGPT with Ollama, please follow this comprehensive guide.
Note that ShellGPT is not optimized for local models and may not work as expected.
Usage
ShellGPT is designed to quickly analyse and retrieve information. It’s useful for straightforward requests ranging from technical configurations to general knowledge.
sgpt “What is the fibonacci sequence” # → The Fibonacci sequence is a series of numbers where each number …
ShellGPT accepts prompt from both stdin and command line argument. Whether you prefer piping input through the terminal or specifying it directly as arguments, sgpt
got you covered. For example, you can easily generate a git commit message based on a diff:
git diff | sgpt “Generate git commit message, for my changes” # → Added main feature details into README.md
You can analyze logs from various sources by passing them using stdin, along with a prompt. For instance, we can use it to quickly analyze logs, identify errors and get suggestions for possible solutions:
docker logs -n 20 my_app | sgpt “check logs, find errors, provide possible solutions”
Error Detected: Connection timeout at line 7.
Possible Solution: Check network connectivity and firewall settings.
Error Detected: Memory allocation failed at line 12.
Possible Solution: Consider increasing memory allocation or optimizing application memory usage.
You can also use all kind of redirection operators to pass input:
sgpt “summarise” < document.txt # → The document discusses the impact… sgpt << EOF What is the best way to lear Golang? Provide simple hello world example. EOF # → The best way to learn Golang… sgpt <<< “What is the best way to learn shell redirects?” # → The best way to learn shell redirects is through…
Shell commands
Have you ever found yourself forgetting common shell commands, such as find
, and needing to look up the syntax online? With --shell
or shortcut -s
option, you can quickly generate and execute the commands you need right in the terminal.
sgpt --shell “find all json files in current folder” # → find . -type f -name “*.json” # → [E]xecute, [D]escribe, [A]bort: e
Shell GPT is aware of OS and $SHELL
you are using, it will provide shell command for specific system you have. For instance, if you ask sgpt
to update your system, it will return a command based on your OS. Here’s an example using macOS:
sgpt -s “update my system” # → sudo softwareupdate -i -a # → [E]xecute, [D]escribe, [A]bort: e
The same prompt, when used on Ubuntu, will generate a different suggestion:
sgpt -s “update my system” # → sudo apt update && sudo apt upgrade -y # → [E]xecute, [D]escribe, [A]bort: e
Let’s try it with Docker:
sgpt -s “start nginx container, mount ./index.html” # → docker run -d -p 80:80 -v $(pwd)/index.html:/usr/share/nginx/html/index.html nginx # → [E]xecute, [D]escribe, [A]bort: e
We can still use pipes to pass input to sgpt
and generate shell commands:
sgpt -s “POST localhost with” < data.json # → curl -X POST -H “Content-Type: application/json” -d ‘{“a”: 1, “b”: 2}’ http://localhost # → [E]xecute, [D]escribe, [A]bort: e
Applying additional shell magic in our prompt, in this example passing file names to ffmpeg
:
ls # → 1.mp4 2.mp4 3.mp4 sgpt -s “ffmpeg combine $(ls -m) into one video file without audio.” # → ffmpeg -i 1.mp4 -i 2.mp4 -i 3.mp4 -filter_complex “[0:v] [1:v] [2:v] concat=n=3:v=1 [v]” -map “[v]” out.mp4 # → [E]xecute, [D]escribe, [A]bort: e
If you would like to pass generated shell command using pipe, you can use --no-interaction
option. This will disable interactive mode and will print generated command to stdout. In this example we are using pbcopy
to copy generated command to clipboard:
sgpt -s “find all json files in current folder” --no-interaction | pbcopy
Shell integration
This is a very handy feature, which allows you to use sgpt
shell completions directly in your terminal, without the need to type sgpt
with prompt and arguments. Shell integration enables the use of ShellGPT with hotkeys in your terminal, supported by both Bash and ZSH shells. This feature puts sgpt
completions directly into terminal buffer (input line), allowing for immediate editing of suggested commands.
Shell_GPT_Integration.mp4
To install shell integration, run sgpt --install-integration
and restart your terminal to apply changes. This will add few lines to your .bashrc
or .zshrc
file. After that, you can use Ctrl+l
(by default) to invoke ShellGPT. When you press Ctrl+l
it will replace you current input line (buffer) with suggested command. You can then edit it and just press Enter
to execute.
To start a conversation, use the --chat
option followed by a unique session name and a prompt.